开发者

Sending mail with a Php with a pdf attachment

I'm trying to send an email from the php mail command. I've been able to what I've tried so far, but can't seem to get it to work with an attachment. I've looked around the web and the best code I've found led me to this:

$fileatt_name = 'JuneFlyer.pdf';
$fileatt_type = 'application/pdf';
$fileatt = 'JuneFlyer.pdf';
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
$data = chunk_split(base64_encode($data));
$MAEmail = "myemail@sbcglobal.net";

mail("$email_address", "$subject", "$message",
"From: ".$MAEmail."\n".
"MIME-Version: 1.0\n".
"Content-type: text/html; charset=iso-8859-1".
"--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Con开发者_如何学运维tent-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .$data. "\n\n" ); 

There are two problems when I do this. First, the contents of the email dissappear.

Second, there is an error on the attachment. "Adobe Reader could not open June_flyer.pdf because it is either not a supported file type or because the file has been damaged (for example it was sent as an email attachment and wasn't correctly decoded)"

Any ideas of how to deal with this?

Thanks,

JB


The very best way how to deal with mail and php is use a reliable well tested library - email with attachments can easily get very nasty. I personally recommend SwiftMailer.


may be problem is within header. if you want to learn the hard way then figure out how you can configure diffrent mimetypes with headers and do the stuff.

or else easy way is use PHPmailer or other email libraries which will do the hard part for you.


One process to learn the correct email format for sending attachments is to try sending yourself an email (with attachment) using Thunderbird, Outlook, etc.

Then view the source of that email. Try copying and pasting that message source into your PHP code (with a little trimming of headers like To and From and Subject that the mail() function already handles) and bada-bing, you have all you need right in front of you.

You can make it dynamic by replacing the chunks of stuff (HTML part, Text part, attachment) with your unique chunks or variables.

Then no fancy library is needed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜