1

我正在发送带有 doc 文件附件的电子邮件。我正在收到邮件,但没有任何附件。

PHP

$file_resume = '';
if (!empty($_FILES['attachment_file_name']['tmp_name'])) {
$file = $_FILES['attachment_file_name']['name'];
$attachment= file_get_contents($file);
$attachment= chunk_split(base64_encode($attachment));

}

$uid = md5(uniqid(time()));

$headers= "From: no-reply@edu.in"."\r\n";
$headers.= "Reply-To: no-reply@edu.in"."\r\n";
$headers.= "X-Mailer: PHP/" . phpversion()."\r\n Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$uid."\"\r\n";
$headers.= "MIME-Version: 1.0" . "\r\n";
$headers.= "Content-type: text/html; charset=utf-8"."\r\n Content-Disposition: attachment; filename=\"".$attachment."\"\r\n";

$message = $_POST['person_name'];

mail($to,$subject,$message,$headers);

html

    <form id="attachment" action='mailer.php' method='POST' name="attachment" enctype="multipart/form-data">

    person name: <input type="text" name="person_name" >
<br />
   Attachment : <input id="attachment_file" class="field" style="height: 25px;" type="file" name="attachment_file_name" />

    <input id="submit_button"  type="submit" value="Send" />
    </form>
4

2 回答 2

2

您需要将附件添加到电子邮件正文中。尝试这样的事情:

$random_hash = md5(time());

$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";

$headers .= "\r\nMIME-Version 1.0";

$attachment = chunk_split(base64_encode(file_get_contents($filename)));

$message = 
"--PHP-alt-$random_hash
Content-Type: text/plain

Dear Same,

We would like to thank you for your registration to be held on Saturday August 25, 2012 at the....

--PHP-alt-$random_hash
Content-Type: application/pdf; name=$filename
Content-Transfer-Encoding: base64
Content-Disposition: attachment

$attachment
--PHP-alt-$random_hash--";

@mail($to, $subject, $message, $headers);
于 2013-03-21T05:45:09.837 回答
2

如果您要尝试手动发送带有附件的电子邮件,您需要了解底层邮件文本数据包,该数据包实际上代表您使用mail()参数构建的内容。除非您理解理解这里发生了什么,否则您将很难收到带有附件的电子邮件以发送。锋利的边缘太多了。

Gmail 有一个非常棒的电子邮件功能,可以以纯文本形式查看原始消息包。它被称为Show Original。在测试您的电子邮件脚本时,如果可以,请使用 Gmail 帐户,以便检查实际的电子邮件明文数据包。要显示原始内容,请转到由电子邮件回复箭头旁边的向下箭头触发的顶部左侧下拉菜单。

因此,向自己发送一封附有图片的测试电子邮件,我们有以下内容。本质上,您的邮件呼叫必须转换为或多或少类似于此示例的内容。

特别注意标题是如何开始的(下面的前五个标题你不会处理,除了可能MIME-Version)。在该块的末尾,您有:

Content-Type: multipart/mixed; boundary=089e0118416874703004d86a5106

后面的部分=消息部分边界,它允许您添加多部分消息块,包括已编码的附件文件内容并提供适当的子部分标头。

所以有:

--089e0118416874703004d86a5106

... message block(s) ...

--089e0118416874703004d86a5106--

实际上声明了另一个边界,其中:

Content-Type: multipart/alternative; boundary=089e0118416874702b04d86a5104

其中涉及到text/plaintext/html双重消息格式。这不是必需的,但许多邮件客户端天生就是这样做的。您可以选择text/plaintext/html,这取决于您和您的评论的格式。

接下来我们看到:

--089e0118416874703004d86a5106
Content-Type: image/jpeg; name="opinion (2).jpg"
Content-Disposition: attachment; filename="opinion (2).jpg"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_hejlmnuz0

/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAIEBAYIBggICAgICAgICAgKCgoKCgoKCgoKCgoKCgoK
... lots of lines for the encoded file block ...
W5R3W8ajLwrgUJEDGKWN2kWvO5iB7qdKwB6MwwfqVrQKcObveZxZBtQNCp0vc//Z
--089e0118416874703004d86a5106--

请注意,边界--在其块(不是每个块)的末尾都有一个尾随。

看看下面,并将其与 luk3thomas 在他的答案中的代码进行比较。尝试向自己发送一封非常简单的电子邮件,并将其与附件格式的电子邮件进行比较:

mail('your@email.com', 'Simple mail test', 'Test message body content.');`

您应该开始了解发送包含附件的电子邮件需要什么。问题是,格式化发送的电子邮件数据包非常特殊,很少有事情会导致它失败。所以你必须注意。

MIME-Version: 1.0
Received: by 10.50.40.164 with HTTP; Thu, 21 Mar 2013 00:30:51 -0700 (PDT)
Date: Thu, 21 Mar 2013 02:30:51 -0500
Delivered-To: [redacted]@gmail.com
Message-ID: <CAKJE7RAH3+ZgN+86xykJrrzVaHK3waPD-a-OXbSDe3FGgcQrMw@mail.gmail.com>
Subject: Test of email with attachment for plaintext
From: Jared Farrish <[redacted]@gmail.com>
To: Jared Farrish <[redacted]@gmail.com>
Content-Type: multipart/mixed; boundary=089e0118416874703004d86a5106

--089e0118416874703004d86a5106
Content-Type: multipart/alternative; boundary=089e0118416874702b04d86a5104

--089e0118416874702b04d86a5104
Content-Type: text/plain; charset=UTF-8

This is the body of the email message.

--089e0118416874702b04d86a5104
Content-Type: text/html; charset=UTF-8

<div dir="ltr">This is the body of the email message.<br></div>

--089e0118416874702b04d86a5104--
--089e0118416874703004d86a5106
Content-Type: image/jpeg; name="opinion (2).jpg"
Content-Disposition: attachment; filename="opinion (2).jpg"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_hejlmnuz0

/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAIEBAYIBggICAgICAgICAgKCgoKCgoKCgoKCgoKCgoK
CgoKCgwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/2wBDAQIICBAQEBAQEBAgICAgIEBAQEBA
QEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQED/wgARCAEsASwDASIA
AhEBAxEB/8QAHQAAAgIDAQEBAAAAAAAAAAAABgcFCAIDBAkAAf/EABsBAAMBAQEBAQAAAAAAAAAA
[-- snip many lines of base64 file contents --]
yGrjmAV5jlw7j5RFebDQu8RG2rLa/up9x/xHOU5eppGo7lm3uDTz6w5XxBYgB7ruYJFwW/2PiD9q
XjDEturp55IeLGM36+Tlz8JY6v8AXGhta9/4Ih62UvuWHuP3DfOg/wAGFg/g0mNKnahdifSKKua+
h0likOlp+yFa2FgP0rtOReVitRTIiUuJSjw6JpN4thVyhRdZ8EE/aYzn523NZeJYu7judhllHo0g
W5R3W8ajLwrgUJEDGKWN2kWvO5iB7qdKwB6MwwfqVrQKcObveZxZBtQNCp0vc//Z
--089e0118416874703004d86a5106--
于 2013-03-21T07:54:03.323 回答