I am looking to send an email with php that will have several attachments. I have done plenty of googling and tried many different scripts, but no matter what I do the email either comes in completely garbeled with no attachments, or the first attachment is included and the rest are thrown away. Here is the code I have so far.

$to = '...';
$subject = 'afdgafg';
$message = "check it";
$filename = "updates";
$filename2= "updates2";
$filename3 = "updates3";

$content = chunk_split(base64_encode("womp womp womp"));
$content1 = chunk_split(base64_encode("wadfgafdgomp womp womp"));
$content2 = chunk_split(base64_encode("womp wompadfgafgafdg womp"));

$uid = md5(uniqid(time()));
$from = "tearsheet reports";
$from = str_replace(array("\r", "\n"), '', $from); // to prevent email injection
$header = "From: ".$from."\r\n"
  ."MIME-Version: 1.0\r\n"
  ."Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"
  ."This is a multi-part message in MIME format.\r\n"
  ."--".$uid."\r\n"
  ."Content-type:text/plain; charset=iso-8859-1\r\n"
  ."Content-Transfer-Encoding: 7bit\r\n\r\n"
  .$message."\r\n\r\n"
  ."--".$uid."\r\n"
  ."Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"
  ."Content-Transfer-Encoding: base64\r\n"
  ."Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"
  .$content."\r\n\r\n"

  ."Content-Type: application/octet-stream; name=\"".$filename2."\"\r\n"
  ."Content-Transfer-Encoding: base64\r\n"
  ."Content-Disposition: attachment; filename=\"".$filename2."\"\r\n\r\n"
  .$content1."\r\n\r\n"

  ."Content-Type: application/octet-stream; name=\"".$filename3."\"\r\n"
  ."Content-Transfer-Encoding: base64\r\n"
  ."Content-Disposition: attachment; filename=\"".$filename3."\"\r\n\r\n"
  .$content2."\r\n\r\n"
  ."--".$uid."--";
mail($to, $subject, $message, $header);

I am able to receive the email, but it only has 1 attachment, not the three I am intending. I do not want to use a library like phpmailer as this is going to go on production servers, so the less dependencies the better. Any suggestions?

没有正确的解决方案

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top