简体   繁体   English

用Php发送带有pdf附件的邮件

[英]Sending mail with a Php with a pdf attachment

I'm trying to send an email from the php mail command. 我正在尝试从php mail命令发送电子邮件。 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" .
"Content-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)" “ Adob​​e Reader无法打开June_flyer.pdf,因为它不是受支持的文件类型,或者因为文件已损坏(例如,它作为电子邮件附件发送并且未正确解码)”

Any ideas of how to deal with this? 关于如何处理的任何想法?

Thanks, 谢谢,

JB 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. 处理邮件和php的最好方法是使用经过良好测试的可靠库-带有附件的电子邮件很容易变得非常讨厌。 I personally recommend SwiftMailer . 我个人推荐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. 如果您想学习困难的方法,那么请弄清楚如何使用标头配置不同的mimetypes并完成这些工作。

or else easy way is use PHPmailer or other email libraries which will do the hard part for you. 否则,简单的方法是使用PHPmailer或其他电子邮件库,这些将为您完成困难的工作。

One process to learn the correct email format for sending attachments is to try sending yourself an email (with attachment) using Thunderbird, Outlook, etc. 学习正确的电子邮件格式以发送附件的过程之一是尝试使用Thunderbird,Outlook等向自己发送电子邮件(带有附件)。

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. 尝试将消息源复制并粘贴到您的PHP代码中(对mail()函数已经处理过的To和From和Subject等标题进行一些修饰)和bada-bing,您就可以拥有所需的一切。

You can make it dynamic by replacing the chunks of stuff (HTML part, Text part, attachment) with your unique chunks or variables. 您可以通过用唯一的块或变量替换部分内容(HTML部分,文本部分,附件)来使其动态。

Then no fancy library is needed. 那么就不需要花哨的图书馆了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM