PHP sends an email with a PDF attachment appears a long string of strings how can i solve

Source: Internet
Author: User
Tags eol php email
PHP Email PDF

RT, the email attachment is not a PDF file, it becomes a long string of strings,

The code for my email is as follows:

function mail ($mailto, $userid, $data) {
Global $dsql, $cfg _adminemail, $cfg _webname, $cfg _basehost, $cfg _memberurl;
$mailtitle = $cfg _webname. ": Notice";
$file =fopen ("D:/xampp/htdocs/pdftemp/dsfasdf.pdf", "R");
$fileName = $data [' oid ']. PDF ';
$mimeType = "Application/pdf";
$boundary = "Nextpart_". Uniqid (""); Delimiter 1
$boundary 2 = "nextpart2_". Uniqid (""); Delimiter 2

Email header
$headers = "From:". $cfg _adminemail. " \r\nreply-to: $cfg _adminemail ";
$headers. = "content-type:multipart/mixed; Boundary=\ "$boundary 1\" \ r \ n ";

Read file
$read = Fread ($file, FileSize ("d:/xampp/htdocs/pdftemp/dsfasdf.pdf"));
We use the Base64 method to encode it.
$read = Base64_encode ($read);
Cut this long string into small chunks of 76 characters per line
$read = Chunk_split ($read);

The following is the Mailbody body
$mailbody = "--$boundary 1----$boundary 2
content-type:text/plain;charset=iso-8859-1;
Content-transfer-encoding:8bit ";
$mailbody. = ' to: '. $userid. ',



This is mailbody--$boundary 2--';

Here is the attachment section
$mailbody. = "--$boundary 1
content-type:text/html; Name= $fileName
Content-disposition:inline; Filename= $fileName
Content-transfer-encoding:8bit
$read
-$boundary ";


$status = Smail ($mailto, $mailtitle, $mailbody, $headers);
Setmail ($mailto, $mailtitle, $mailbody, $headers, $status);
return $status;
}


Reply to discussion (solution)

You use Phpmailer to

Is this probably not the type of attachment you are specifying?

Attachment$body. = "--". $separator. $eol; $body. = "Content-type:application/octet-stream; Name=\ "". $filename. " \ "". $eol; $body. = "Content-transfer-encoding:base64". $eol; $body. = "Content-disposition:attachment". $eol. $eol; $body. = $ Attachment. $eol; $body. = "--". $separator. " --";


Recommend the upstairs
$email = new Phpmailer (); $email->from      = ' you@example.com '; $email->fromname  = ' Your Name '; $email Subject   = ' Message Subject '; $email->body      = $bodytext; $email->addaddress (' Destinationaddress@example.com '); $file _to_attach = ' path_of_your_file_here '; $email->addattachment ($file _to_ Attach, ' nameoffile.pdf '); return $email->send ();

It is possible that the boundary settings are incorrect, resulting in parsing errors.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.