Swift Mailer вложения - PullRequest
       10

Swift Mailer вложения

4 голосов
/ 12 января 2011

Я создаю CSV на лету с помощью PHP, затем мне нужно прикрепить этот CSV-файл к сообщению Swift Mailer. Я попытался использовать file_get_content для созданного файла, а также использовать chunk_split (base64_encode (file_get_contents ()) для созданного файла, а также прикрепить файл перед записью его на диск. Без записи на диск я получаю Rescource # 183 в CSV с подключением это с 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
      }
      }

Ответы [ 2 ]

7 голосов
/ 12 января 2011

Вложение файла в быстрый почтовик:

$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'));

только для примера конечно:)

0 голосов
/ 31 августа 2016
//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();
...