使用 symfony 生成并发送附件中的 CSV 文件
Generate and Send CSV file in attachment with symfony
我需要将 CSV 文件连同他们的交易细节发送给每个用户,同时 运行 在 symfony 中执行 cron
我的代码如下生成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 将感谢最佳答案
尝试将文件直接附加到邮件中,例如 Swift:
$mail = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from)
->setTo($recipient)
->setBody($bodyText)
->attach(\Swift_Attachment::fromPath($absolutePathOfTheAttachedFile));
编辑:
或者您可以附加生成的数据,例如:
->attach(
\Swift_Attachment::newInstance($csvData)
->setFilename('filename.csv')
->setContentType('application/csv')
);
希望对您有所帮助
从 symfony 3.4 开始使用以下代码:
$message = (new \Swift_Message($subject))
->setFrom($from)
->setTo($recipient)
->setBody($bodyText)
->attach(
(new \Swift_Attachment($csvData))
->setFilename('filename.csv')
->setContentType('application/csv')
);
我需要将 CSV 文件连同他们的交易细节发送给每个用户,同时 运行 在 symfony 中执行 cron
我的代码如下生成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 将感谢最佳答案
尝试将文件直接附加到邮件中,例如 Swift:
$mail = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from)
->setTo($recipient)
->setBody($bodyText)
->attach(\Swift_Attachment::fromPath($absolutePathOfTheAttachedFile));
编辑:
或者您可以附加生成的数据,例如:
->attach(
\Swift_Attachment::newInstance($csvData)
->setFilename('filename.csv')
->setContentType('application/csv')
);
希望对您有所帮助
从 symfony 3.4 开始使用以下代码:
$message = (new \Swift_Message($subject))
->setFrom($from)
->setTo($recipient)
->setBody($bodyText)
->attach(
(new \Swift_Attachment($csvData))
->setFilename('filename.csv')
->setContentType('application/csv')
);