使用php-mail()发送多个附件

  • 本文关键字:php-mail 使用 php
  • 更新时间 :
  • 英文 :


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

最新更新