Send attachments with PHP Mail()?

前端 未结 14 2449
北恋
北恋 2020-11-21 04:46

I need to send a pdf with mail, is it possible?

$to = \"xxx\";
$subject = \"Subject\" ;
$message = \'Example message with html\';
$header         


        
14条回答
  •  迷失自我
    2020-11-21 05:15

    None of the above answers worked for me due to their specified attachment format (application/octet-stream). Use application/pdf for best results with PDF files.

    The PDF is attached.

    "; // email body $pdfLocation = "./your-pdf.pdf"; // file location $pdfName = "pdf-file.pdf"; // pdf file name recipient will get $filetype = "application/pdf"; // type // creates headers and mime boundary $eol = PHP_EOL; $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_$semi_rand"; $headers = "From: $from$eolMIME-Version: 1.0$eol" . "Content-Type: multipart/mixed;$eol boundary=\"$mime_boundary\""; // add html message body $message = "--$mime_boundary$eol" . "Content-Type: text/html; charset=\"iso-8859-1\"$eol" . "Content-Transfer-Encoding: 7bit$eol$eol$body$eol"; // fetches pdf $file = fopen($pdfLocation, 'rb'); $data = fread($file, filesize($pdfLocation)); fclose($file); $pdf = chunk_split(base64_encode($data)); // attaches pdf to email $message .= "--$mime_boundary$eol" . "Content-Type: $filetype;$eol name=\"$pdfName\"$eol" . "Content-Disposition: attachment;$eol filename=\"$pdfName\"$eol" . "Content-Transfer-Encoding: base64$eol$eol$pdf$eol--$mime_boundary--"; // Sends the email if(mail($to, $subject, $message, $headers)) { echo "The email was sent."; } else { echo "There was an error sending the mail."; }

提交回复
热议问题