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

发布于 2024-09-01 16:49:50 字数 1029 浏览 6 评论 0原文

我正在尝试从 php mail 命令发送电子邮件。到目前为止我已经尝试过,但似乎无法让它与附件一起工作。我浏览了网络,找到的最好的代码让我发现了这一点:

$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 = "[email protected]";

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" ); 

当我这样做时,有两个问题。首先,电子邮件的内容消失了。

其次,附件有错误。 “Adobe Reader 无法打开 June_flyer.pdf,因为它不是受支持的文件类型,或者因为文件已损坏(例如,它作为电子邮件附件发送且未正确解码)”

有关如何处理的任何想法这?

谢谢,

杰比

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 = "[email protected]";

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)"

Any ideas of how to deal with this?

Thanks,

JB

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉殇 2024-09-08 16:49:50

处理邮件和 php 的最佳方法是使用可靠且经过测试的库 - 带附件的电子邮件很容易变得非常令人讨厌。我个人推荐 SwiftMailer

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.

肩上的翅膀 2024-09-08 16:49:50

问题可能出在标头内。如果你想学习困难的方法,那么弄清楚如何使用标头配置不同的 mimetypes 并执行这些操作。

或者简单的方法是使用 PHPmailer 或其他电子邮件库,它们将为您完成困难的部分。

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.

守不住的情 2024-09-08 16:49:50

了解发送附件的正确电子邮件格式的一个过程是尝试使用 Thunderbird、Outlook 等向自己发送一封电子邮件(带附件)。

然后查看该电子邮件的来源。尝试将该消息源复制并粘贴到您的 PHP 代码中(稍微修剪一下 header,例如 mail() 函数已经处理的“To”、“From”和“Subject”),然后 bada-bing,您所需要的一切就在您面前。

您可以通过用您独特的块或变量替换内容块(HTML 部分、文本部分、附件)来使其动态化。

那么就不需要花哨的图书馆了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文