首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift Mailer附件

Swift Mailer附件
EN

Stack Overflow用户
提问于 2011-01-12 18:41:27
回答 2查看 12K关注 0票数 4

我正在用PHP动态创建一个CSV,然后我需要将这个CSV文件附加到Swift Mailer消息中。我尝试在创建的文件上使用file_get_content,在创建的文件上使用chunk_split(base64_encode(file_get_contents()),并在将文件写入磁盘之前附加该文件。在没有写入磁盘的情况下,我在CSV中获得了资源#183,通过使用file_get_content附加它,我在CSV文件的每一行中只得到了一个字符串,有人知道我做错了什么吗?

代码语言:javascript
复制
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
      }
      }
EN

回答 2

Stack Overflow用户

发布于 2011-01-13 01:12:39

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

代码语言:javascript
复制
$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);

在您的示例中,附加内容为:

代码语言:javascript
复制
$message->attach(new Swift_Message_Attachment(file_get_contents($file), $order.'order.csv', 'application/vnd.ms-excel'));

当然,只是举个例子:)

票数 7
EN

Stack Overflow用户

发布于 2016-08-31 21:30:46

代码语言:javascript
复制
//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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4667822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档