1
發送CSV文件中的附件,我需要CSV文件發送給每一個用戶與他們的交易細節,而在symfony中運行的cron生成並用symfony
我的代碼如下生成CSV數據,並通過電子郵件發送
陣列的用戶:
$array = array('name','ticket','history','date','flag','created by','transition');
$f = fopen('php://temp', 'w');
$line = $array;
fputcsv($f, $line, ',');
rewind($f);
$stream_get_contents = stream_get_contents($f);
$sentmail = $this->sendEmail($stream_get_contents);
if($sentmail){
return true;
}else{
return false;
}
fclose($f);
unset($f);
public function sendEmail($csvData){
UserId='1';
$em = $this->getContainer()->get('doctrine')->getManager();
$getstaffuser = $em->getRepository('AdminBundle:User')->find($UserId);
if ($getstaffuser) {
if ($getstaffuser->getEmail()) {
$objEmailTemplate = $em->getRepository('BviTemplateBundle:Emailtemplate')->findOneBy(array('emailkey' => 'KEYFIND', 'status' => 'Active'));
if (!$objEmailTemplate) {
$output->writeln("\n####### Email template not present #######\n");
$logger->info("\n####### Email template not present #######\n");
return "NoEmailTemplate";
}
$attachment = chunk_split($csvData);
$multipartSep = '-----' . md5(time()) . '-----';
$bodydata = $objEmailTemplate->getBody();
$body = "--$multipartSep\r\n"
. "Content-Type: text/plain; charset=ISO-8859-1; format=flowed\r\n"
. "Content-Transfer-Encoding: 7bit\r\n"
. "\r\n"
. "$bodydata\r\n"
. "--$multipartSep\r\n"
. "Content-Type: text/csv\r\n"
. "Content-Transfer-Encoding: base64\r\n"
. "Content-Disposition: attachment; filename=\"Website-Report-" . date("F-j-Y") . ".csv\"\r\n"
. "\r\n"
. "$attachment\r\n"
. "--$multipartSep--";
$to = $getstaffuser->getEmail();
$subject = $objEmailTemplate->getSubject();
$mail = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($this->getContainer()->getParameter('fos_user.registration.confirmation.from_email'))
->setTo($to);
$mail->setBody($body)
->setContentType('multipart/mixed');
$issent = $this->getContainer()->get('mailer')->send($mail);
if ($issent) {
return 'EmailSent';
} else {
return 'EmailNotSent';
}
} else {
return "NoEmailAddress";
}
} else {
return "NoUser";
}
}
但它不工作,我收到電子郵件作爲只NONAME作爲文件名中的附件,這是不csv文件,並沒有得到體內容文件。
請任何機構可以幫我要this.I將欣賞最好的答案
感謝您的幫助,但我需要將生成的csv數據作爲附件發送爲csv文件,生成的數據存儲在$ csvData –
hi @HetalGohel嘗試檢查答案中的編輯部分是否滿足你需要的。讓我知道 – Matteo
謝謝,是的,它爲我工作如下代碼 - >附加(\ Swift_Attachment :: newInstance($ csvData) - > setFilename('filename.csv') - > setContentType('application/csv') ); –