【问题标题】:Generate and Send CSV file in attachment with symfony使用 symfony 在附件中生成和发送 CSV 文件
【发布时间】:2017-02-06 11:07:56
【问题描述】:

在 symfony 中运行 cron 时,我需要将 CSV 文件及其交易详细信息发送给每个用户

我的代码如下生成 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";
        }
}

但它不起作用,我收到的电子邮件只是作为附件中的无名文件名的文件,它不是 csv 文件,也没有获取正文内容。

请任何人都可以帮助我。我将感谢最佳答案

【问题讨论】:

    标签: php symfony swiftmailer


    【解决方案1】:

    尝试使用 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') 
            );
    

    希望有帮助

    【讨论】:

    • 感谢您的帮助,但我需要将生成的 csv 数据作为附件发送为 csv 文件,生成的数据存储在 $csvData 中
    • 嗨@HetalGohel 如果满足您的需要,请尝试检查答案中的编辑部分。让我知道
    • 谢谢,是的,它对我有用,如下代码 ->attach(\Swift_Attachment::newInstance($csvData) ->setFilename('filename.csv') ->setContentType('application/csv) '));
    【解决方案2】:

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

    【讨论】:

    • 您能否添加一个简短的说明来解释为什么 OP 的解决方案不起作用而您的解决方案起作用?
    • Cubo78,答案有效,它只是从 3.4 版及更高版本 Swift_Message::newInstance() 更改为 (new \Swift_Message($subject)),与 Swift_Attachment 相同。
    • 我不信任你。添加简短描述可以简单地帮助读者理解(工作)解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多