【问题标题】:Swift Mailer attachmentsSwift Mailer 附件
【发布时间】:2011-06-07 18:11:40
【问题描述】:

我正在使用 PHP 动态创建 CSV,然后我需要将此 CSV 文件附加到 Swift Mailer Message。我尝试在创建的文件上使用 file_get_content 以及在创建的文件上使用 chunk_split(base64_encode(file_get_contents()) 以及在将文件写入磁盘之前附加文件。不写入磁盘我在 CSV 中得到 Resource #183,并附加使用 file_get_content 我在 CSV 文件的每一行中只得到一个字符串,有人知道我在做什么错吗?

if(!file_exists(_PS_ORDERS_DIR_.$orderDate.'/'.$file_name.'.csv'))
 {
  if($file = fopen (_PS_ORDERS_DIR_.$orderDate.'/'.$file_name.'.csv', 'x+'))
   {
   foreach ($list as $fields)
    {
     fputcsv($file, $fields);
    }



    $attachment['mime'] = 'application/vnd.ms-excel';
    $attachment['content'] = file_get_contents($file);
    $attachment['name'] = $order.'order';
  EDIT            
 Mail::Send(1, 'order_conf', 'Order CSV Attachment', $success, 'test@email.com', Address, NULL, NULL, $attachment); // attach and send
      }
      }

【问题讨论】:

  • 你能显示你用来附加附件的代码吗?
  • “CSV 文件的每一行中都有一个字符串”是什么意思?
  • 我可以很好地创建文件,但发送它时遇到问题。我的意思是,不是将数组中的每个项目分配给行中的单独单元格,而是第一行(例如 A1)将包含数组中的所有值,用逗号分隔
  • 您可能想要关闭 $file 并取消设置它...只需 file_get_contents(PS_ORDERS_DIR.$orderDate.'/'.$file_name.'.csv')而不是 file_get_contents($file); ?

标签: php csv attachment swiftmailer mailer


【解决方案1】:
//to set headers of csv file(first row)
$content = "user_id,first_name,last_name,phone,gender,pan_number\r\n";

//this can be dynamic data from database or from anywhere can loop this for multiple rows
$content .= "1,test_fn,test_ln,8888999900,M,ASDD3333\r\n";

//include swiftmailer library in order to run the below code
Yii::$app->mailer->compose()
->setFrom(array('test@test.com'=>'test'))
->setTo('testto@testto.com')
->setSubject('your subject' )
->setHtmlBody('<table><tr><td>your email body message here</td></tr>       </table>')
->attachContent($content, ['fileName' => 'user.csv', 'contentType' => 'application/vnd.ms-excel'])->send();

【讨论】:

    【解决方案2】:

    将文件附加到快速邮件中:

    $swift =& new Swift(new Swift_Connection_SMTP(MAIL_SMTP_URL, MAIL_SMTP_PORT));
    $message =& new Swift_Message($subject);
    $recpients =& new Swift_RecipientList();
    $sender =& new Swift_Address('info@example.com', 'example.com');
    $recpients->addTo('info@example.com', 'www.example.com');
    
    $message->attach(new Swift_Message_Part('this is my body'));
    $message->attach(new Swift_Message_Attachment($binarycontents, $fileTitle, $mimeType));
    $swift->send($message, $recpients, $sender);
    

    在你的情况下,附件是:

    $message->attach(new Swift_Message_Attachment(file_get_contents($file), $order.'order.csv', 'application/vnd.ms-excel'));
    

    当然只是举例:)

    【讨论】:

      猜你喜欢
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多