Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php : email sending failed with more than one attachment

I am trying to implement mail functionality in php, its working fine with single attachment, but problem is when I am trying to send more than one attachment, its not working. I am using php mail() function for sending emails, I am trying to attach a PDF and an Image file. If PDF attach then Image won't attach, if Image attach then PDF won't attach. Any thoughts where I'm doing wrong?

$header       .= 'From: test <[email protected]>' . "\r\n";       
$header       .= "MIME-Version: 1.0\r\n";
$file           = '1.png'
$displayname    = '1.png';
$file_size     = filesize($file);
$handle        = fopen($file, "r");
$content       = fread($handle, $file_size);
fclose($handle);
$content       = chunk_split(base64_encode($content));
$uid           = md5(uniqid(time()));
$name          = basename($file);

$filepdf        = '1.pdf'
$displaynamepdf= '1.pdf';
$file_sizepdf  = filesize($filepdf);
$handlepdf     = fopen($filepdf, "r");
$contentpdf    = fread($handlepdf, $file_sizepdf);
fclose($handlepdf);
$contentpdf    = chunk_split(base64_encode($contentpdf));
$name          = basename($file);

$header       .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header       .= "This is a multi-part message in MIME format.\r\n";
$header       .= "--".$uid."\r\n";
$header       .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header       .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header       .= $message."\r\n\r\n";
$header       .= "--".$uid."\r\n";
$header       .= "Content-Type: application/octet-stream; name=\"".$displayname."\"\r\n"; // use different content types here
$header       .= "Content-Transfer-Encoding: base64\r\n";
$header       .= "Content-Disposition: attachment; filename=\"".$displayname."\"\r\n\r\n";
$header       .= $content."\r\n\r\n";

$header       .= "Content-Type: application/octet-stream; name=\"".$displaynamepdf."\"\r\n"; // use different contentpdf types here
$header       .= "Content-Transfer-Encoding: base64\r\n";
$header       .= "Content-Disposition: attachment; filename=\"".$displaynamepdf."\"\r\n\r\n";
$header       .= $contentpdf."\r\n\r\n";
$header       .= "--".$uid."--";

if (mail($to, $subject, "", $header)) {
   return 'sent';
} else {
    return 'not sent';
}
like image 647
user2172726 Avatar asked Mar 15 '13 06:03

user2172726


1 Answers

PHPMailer is the best to use for email.

checkout some below links which will help you in future also:

  1. File Attachments in PHP Mail with PHPMailer
  2. php mailer attachments
  3. attachments using phpMailer
  4. PHPMailer Tutorial
  5. A Simple Example : Sending Email with Attachment Using Phpmailer
  6. PHP send email with multiple attachments with PHPMailer with SMTP Authentication
  7. Sending email with multiple attachments with PHP

may this help you.

like image 52
Tony Stark Avatar answered Sep 23 '22 02:09

Tony Stark