I'm trying to send two attachments in an email using mail() function in PHP, but only attach one.
The problem is in the attachment time, I only receive in the email the first attachment.
Can you help?
This is the code:
$from = "postmaster@localhost";
// $to = "[email protected]";
$content = file_get_contents($file1);
$content2 = file_get_contents($file2);
$content = chunk_split(base64_encode($content));
$content2 = chunk_split(base64_encode($content2));
// a random hash will be necessary to send mixed content
$semi_rand = md5(time());
$separator = "==Multipart_Boundary_x{$semi_rand}x";
// carriage return type
$eol = "\r\n";
// main header (multipart mandatory)
$headers = "From: Name<" . $from . ">" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/html;" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $message . $eol;
// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"factura.pdf\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment; filename=\"factura.pdf\"" . $eol;
$body .= "X-Attachment-Id: " . rand(1000, 99999) . $eol;
$body .= $content . $eol;
$body .= "Content-Type: application/octet-stream; name=\"factura.xml\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment; filename=\"factura.xml\"" . $eol;
$body .= "X-Attachment-Id: " . rand(1000, 99999) . $eol;
$body .= $content2 . $eol;
// attachment
$body .= "--" . $separator . "--";
mail($to, $subject, $body, $headers))
My Answer:
"sender@example.com";
$to = "recipient@example.com";
$subject = "Email with two attachments";
$message = "This email contains two attachments.";
$file1 = "attachment1.pdf";
$file2 = "attachment2.pdf";
$attachment1 = chunk_split(base64_encode(file_get_contents($file1)));
$attachment2 = chunk_split(base64_encode(file_get_contents($file2)));
$boundary = md5(time());
$headers = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$body .= $message."\r\n";
$body .= "--$boundary\r\n";
$body .= "Content-Type: application/pdf; name=\"$file1\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"$file1\"\r\n\r\n";
$body .= $attachment1."\r\n";
$body .= "--$boundary\r\n";
$body .= "Content-Type: application/pdf; name=\"$file2\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Disposition: attachment; filename=\"$file2\"\r\n\r\n";
$body .= $attachment2."\r\n";
$body .= "--$boundary--";
if (mail($to, $subject, $body, $headers)) {
echo "Email sent successfully with two attachments.";
} else {
echo "Failed to send email with two attachments.";
}
Rate this post
3 of 5 based on 7847 votesComments