发送带有doc文件附件的邮件

时间:2013-03-21 05:32:31

标签: attachment php

我发送的是一封带有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>

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:2)

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

Gmail在电子邮件中提供了一个非常棒的功能,可以用纯文本格式查看原始邮件数据包。它被称为显示原始。在测试您的电子邮件脚本时,如果可以,请使用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--

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

看看下面的内容,并将其与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--