Reputation: 545
I'm aware that there are lot of similar questions on stackoverflow about about this problem and I have tried all of them but none seem to work .
I am trying to send an email with a csv attachment from a cron function. My email is getting sent but there is no attachment . Here is my code
$email = new CakeEmail('mandrillSmtp');
$encodedCsv = base64_encode(implode(",",$headers));
//please note that i have debugged encodedCsv. No errors here
$email->subject('Report Test');
$email->from('[email protected]');
$email->to('[email protected]');
$email->emailFormat('html');
$email->attachments=array(
array(
'content' => $encodedCsv,
'type' => "text/csv",
'name' => 'report.csv'
)
);
$email->send();
Upvotes: 1
Views: 415
Reputation: 545
Hey guys thanks for all the help . I finally got my code working . thanks @ndm for your reminder that the mandrill api is completely different from CakeEmail(brief moment of dumbness for me). I solved my issue of not being able to attach a file written to the php://temp output stream too by using tmpfile() instead of fopen('php://temp') . for some reason i cant access files written to the temp stream even if i have their uri's Anyway heres my code. hope that it helps someone
$email = new CakeEmail('mandrillSmtp');
if($handle =tmpfile()){
fputcsv($handle, $headers);
fputcsv($data) ;
}
$fileMeta = stream_get_meta_data($handle);
$uri = $fileMeta['uri'];
$email->subject('Report Test');
$email->from('[email protected]');
$email->to('[email protected]');
$email->emailFormat('html');
$email->attachments(array('test.csv'=>array('mimetype'=>'text/csv','file' => $uri)));
$email->send();
fclose($handle);
Upvotes: 1
Reputation: 223
If you are trying to attach the CSV file, then you need the path to the CSV file and I suggest you make the attachment array as follows:
$email->attachments = array(
array('file' => 'pathToCSVFIle/myCSvFile.csv')
);
Upvotes: 0