Php sends an email with a pdf attachment. a long string is displayed. how can this problem be solved?

Source: Internet
Author: User
Tags eol php email
Php sends a long string of emails with pdf attachments. how can I solve this problem? php email pdf

RT: The email attachment is not a pdf file, but a long string,

The code for sending an email is as follows:

Function mail ($ mailto, $ userid, $ data ){
Global $ dsql, $ pai_adminemail, $ pai_webname, $ pai_basehost, $ pai_memberurl;
$ Mailtitle = $ pai_webname. ": Notification ";
$ File = fopen ("D:/xampp/htdocs/empty temp/dsfasdf.pdf", "r ");
Invalid filename=$data='oid ';
$ MimeType = "application/pdf ";
$ Boundary = "nextpart _". uniqid (""); // delimiter 1
$ Boundary2 = "nextpart2 _". uniqid (""); // delimiter 2

// Email header
$ Headers = "From:". $ pai_adminemail. "\ r \ nReply-To: $ pai_adminemail ";
$ Headers. = "Content-type: multipart/mixed; boundary = \" $ boundary1 \ "\ r \ n ";

// Read the file
$ Read = fread ($ file, filesize ("D:/xampp/htdocs/sharetemp/dsfasdf.pdf "));
// We use base64 to encode it.
$ Read = base64_encode ($ read );
// Cut the long string into small pieces consisting of 76 characters in each line
$ Read = chunk_split ($ read );

// The following is the mailbody body.
$ Mailbody = "-- $ boundary1 ---- $ boundary2
Content-type: text/plain; charsets = ISO-8859-1;
Content-transfer-encoding: 8bit ";
$ Mailbody. = 'to:'. $ userid .',



This is mailbody -- $ boundary2 --';

// The Attachment section is as follows:
$ Mailbody. = "-- $ boundary1
Content-type: text/html; name = $ fileName
Content-disposition: inline; filename = $ fileName
Content-transfer-encoding: 8bit
$ Read
-- $ Boundary1 --";


$ Status = smail ($ mailto, $ mailtitle, $ mailbody, $ headers );
SetMail ($ mailto, $ mailtitle, $ mailbody, $ headers, $ status );
Return $ status;
}


Reply to discussion (solution)

You use phpmailer

Maybe you have not specified the attachment type?

// 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."--";


Recommended 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 may be because the boundary settings are incorrect, resulting in a parsing error.

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.