4

I'm creating a CSV on the fly with PHP, I then need to attach this CSV file to the the Swift Mailer Message. I have tried using file_get_content on the created file aswell as using chunk_split(base64_encode(file_get_contents()) on the created file aswell as attaching the file before writing it to disk. Without writing to disk I get Rescource #183 in the CSV, with attaching it with file_get_content I get just a string in each row of the CSV file, anyone know what I'm doing wrong?

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
      }
      }
koolhead17
  • 1,944
  • 1
  • 12
  • 20
Dan
  • 73
  • 1
  • 2
  • 8
  • Can you show the code you use to attach the attachment? – Pekka Jan 12 '11 at 10:43
  • What do you mean by "a string in each row of the CSV file"? – Pekka Jan 12 '11 at 10:53
  • I can create the file fine but just have problems sending it. What I mean by that is that instead of each item in the array being assigned to a seperate cell in the row, the first row e.g A1 will have all the values from the array seperated by commas – Dan Jan 12 '11 at 10:58
  • you might want to fclose the $file and unset it... and just file_get_contents(_PS_ORDERS_DIR_.$orderDate.'/'.$file_name.'.csv') instead of file_get_contents($file); ? – Gekkie Mar 12 '14 at 10:40

2 Answers2

7

Attaching a file into a swift mailer:

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

in your case the attaching would be:

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

just for example ofcourse :)

Gekkie
  • 996
  • 9
  • 24
0
//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();
Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87