【发布时间】:2017-03-23 00:38:49
【问题描述】:
当我从服务器发送附件大于约 2.5Mb 的邮件时,我遇到了一个问题。发送带有较小附件的电子邮件是可行的,但一旦达到大约 2 或 2.5Mb 的临界大小,就不再发送邮件了。
无论大小如何,PDF 文件和合并的目标 PDF 都可以毫无问题地创建。但只有较小的 PDF 文件通过邮件发送。当附件太大时,甚至不会发送空邮件。
流程如下:
1) php 脚本创建了几个 PDF 文件。
2) 这些文件通过 gs 合并
$finCmd = 'gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile='.$pathDest.$pdfFilename.' input1.pdf input2.pdf input3.pdf';
// Create PDF
$execResult = exec($finCmd);
3) 电子邮件正文已创建
protected function setBodyHtmlpart($content, $pdfFilepath = null, $pdfFilename = null) {
$content="<p><span style='font-size:10.0pt;font-family:\"Arial\",\"sans-serif\";color:black;'>".$content.'</span></p>';
$html = new MimePart($content.$this->getSignature());
$html->type = "text/html";
$body = new MimeMessage();
if ($pdfFilename != '') {
$pdfAttach = new MimePart(file_get_contents($pdfFilepath.$pdfFilename));
$pdfAttach->type = 'application/pdf';
$pdfAttach->filename = $pdfFilename;
$pdfAttach->encoding = \Zend\Mime\Mime::ENCODING_BASE64;
$pdfAttach->disposition = \Zend\Mime\Mime::DISPOSITION_ATTACHMENT;
$body->setParts(array($html, $pdfAttach));
} else {
$body->setParts(array($html));
}
return $body;
}
4) 发送电子邮件:
protected function send($fromAddress, $fromName, $toAddress, $toName, $subject, $bodyParts)
{
// setup SMTP options
$options = new SmtpOptions(array(
'name' => 'XServer',
'host' => 'xServer',
'port' => 25,
'connection_class' => 'plain',
'connection_config' => array(
'username' => 'Xusername',
'password' => 'Xpassword',
),
));
$mail = new Message();
$mail->setBody($bodyParts);
$mail->setFrom($fromAddress, $fromName);
$mail->setTo($toAddress, $toName);
$mail->setSubject($subject);
$transport = new SmtpTransport($options);
$transport->send($mail);
}
欢迎任何提示,因为我完全迷路了。
我认为可能存在竞争问题:exec 未完成,但脚本已尝试发送邮件并取消。但我至少会收到一封空邮件。
编辑: 更改然后 Mime\Mime::ENCODING_BASE64 传递邮件,但 PDF 文件已损坏。
【问题讨论】:
-
你确定生成的 PDF 真的也很好吗?您是否生成并打开了一个?
-
是的。我打开了生成的文件,它们是正确的。而不是那些解码不正确的,请参阅我的答案。
标签: php email zend-framework2 email-attachments