php - 使用 php mail() 发送多个附件
问题描述
我尝试使用 PHP 邮件功能将 FPDF 类生成的 2 个或 3 个 PDF 文件作为附件发送到同一封电子邮件中,但每次只发送一个附件。我尝试多次编辑我的代码,但仍然无法正常工作。我想知道我的代码是否与第一个附件一起正常工作而我的代码与其他附件不一起工作有什么问题?
// attachment
// $files is an array
$separator = md5(time());
$eol = PHP_EOL;
for ($i = 0; $i < count($files); $i++)
{
$filename = $files[$i].".pdf";
$filepath = 'https://www.mywebsite.com/pdf/index.php?tcode='.$tcode[$i];
// to specify the attachment file path to send */
$content = file_get_contents($filepath);
$attachment = chunk_split(base64_encode($content));
$reply_message .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$reply_message .= "Content-Transfer-Encoding: base64".$eol;
$reply_message .= "Content-Disposition: attachment".$eol.$eol;
$reply_message .= $attachment.$eol;
$reply_message .= "--".$separator."--";
}
mail($email_reply, $reply_subject, $reply_message, $headers1);
解决方案
更新的答案
问题是我必须在循环的开头(内部)插入分隔分隔符,并在循环之后(一个外部)添加一个闭合分隔符。
// attachment
// $files is an array
$separator = md5(time());
$eol = PHP_EOL;
for ($i = 0; $i < count($files); $i++)
{
$filename = $files[$i].".pdf";
$filepath = 'https://www.mywebsite.com/pdf/index.php?tcode='.$tcode[$i];
// to specify the attachment file path to send */
$content = file_get_contents($filepath);
$attachment = chunk_split(base64_encode($content));
$reply_message .= "--".$separator.$eol;
$reply_message .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$reply_message .= "Content-Transfer-Encoding: base64".$eol;
$reply_message .= "Content-Disposition: attachment".$eol.$eol;
$reply_message .= $attachment.$eol;
}
$reply_message .= "--".$separator."--";
mail($email_reply, $reply_subject, $reply_message, $headers1);
推荐阅读
- vb.net - 带有从左到右滑动文本的 VB.net 标签
- solr - Solr 6.3 将 rawquerystring 拆分为两个字母对
- java - React 登录页面(客户端)和 spring boot/security(服务器端)
- javascript - 当 appendChild 在特定 div 上时,eventListener 不起作用
- angular - Angular:在数据源中使用对象时,MatSort 不起作用
- javascript - 计算javascript对象数组中重复项的平均值
- .htaccess - 如何正确重定向到子文件夹
- spring-boot - Google Cloud Kafka 实例和 Spring Boot Appengine 应用程序连接错误:TimeoutException: Timeout expired while fetching topic metadata
- c# - 初学者问题 - 如何在这段代码中四舍五入到小数点后 3 位?
- c++ - 在 wxWidgets 中使用模板类作为 bind() 的输入