PHP mail() 附件问题

Posted

技术标签:

【中文标题】PHP mail() 附件问题【英文标题】:PHP mail() attachment problems 【发布时间】:2011-09-10 15:07:45 【问题描述】:

谁能帮我弄清楚为什么这总是返回错误?

$to = "myemail@mydomain.com";
$from = "Website <website@mydomain.com>";
$subject = "Test Attachment Email";

$separator = md5(time());

// carriage return type (we use a php end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "document.pdf";

//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));

// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol; 
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;

// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;

// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";

// send message
if (mail($to, $subject, "", $headers)) 
    echo "mail send ... OK";
 else 
    echo "mail send ... ERROR";

【问题讨论】:

几天前我看到了“将所有内容放入$headers”。你们是从哪里复制粘贴的? 这种手动标题操作是一个肮脏的解决方案。我个人是 Zend_Mail 的粉丝,虽然我想知道任何其他“智能”方法。 @MattH.,Zend 开发人员也不必做这种“肮脏的解决方案”吗? 【参考方案1】:

就像我在@GovindTotla 的回答中评论的那样,他的解决方案对我有用。为了完整起见,下面是$header 的输出代码,带有两个附件。将其视为逆向工程答案:

From: from@domain.com
Reply-To: reply@domain.com
Cc: cc@domain.com
Bcc: bcc@domain.com
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="2527518bb813d2f2847a2adba8b8b47f"

This is a multi-part message in MIME format.
--2527518bb813d2f2847a2adba8b8b47f
Content-type:text/html; charset=iso-8859-1
Content-Transfer-Encoding: 7bit

Here comes the message.

--2527518bb813d2f2847a2adba8b8b47f
Content-Type: application/octet-stream; name="test.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="test.txt"

U29tZXRoaW5nIGVsc2UuIFNvbWV0aGluZyBlbHNlLiBTb21ldGhpbmcgZWxzZS4gU29tZXRoaW5n
IGVsc2UuIFNvbWV0aGluZyBlbHNlLiBTb21ldGhpbmcgZWxzZS4gU29tZXRoaW5nIGVsc2UuIFNv
bWV0aGluZyBlbHNlLiBTb21ldGhpbmcgZWxzZS4gU29tZXRoaW5nIGVsc2UuIFNvbWV0aGluZyBl
bHNlLiBTb21ldGhpbmcgZWxzZS4gU29tZXRoaW5nIGVsc2UuIFNvbWV0aGluZyBlbHNlLiBTb21l
dGhpbmcgZWxzZS4gU29tZXRoaW5nIGVsc2UuIFNvbWV0aGluZyBlbHNlLiBTb21ldGhpbmcgZWxz
ZS4gU29tZXRoaW5nIGVsc2UuIFNvbWV0aGluZyBlbHNlLgo=


--2527518bb813d2f2847a2adba8b8b47f
Content-Type: application/octet-stream; name="test2.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="test2.txt"

VGhpcyBpcyB0aGUgY29udGVudCBvZiB0aGUgc2Vjb25kIGF0dGFjaG1lbnQuIFRoaXMgaXMgdGhl
IGNvbnRlbnQgb2YgdGhlIHNlY29uZCBhdHRhY2htZW50LiBUaGlzIGlzIHRoZSBjb250ZW50IG9m
IHRoZSBzZWNvbmQgYXR0YWNobWVudC4gVGhpcyBpcyB0aGUgY29udGVudCBvZiB0aGUgc2Vjb25k
IGF0dGFjaG1lbnQuIFRoaXMgaXMgdGhlIGNvbnRlbnQgb2YgdGhlIHNlY29uZCBhdHRhY2htZW50
LiBUaGlzIGlzIHRoZSBjb250ZW50IG9mIHRoZSBzZWNvbmQgYXR0YWNobWVudC4gVGhpcyBpcyB0
aGUgY29udGVudCBvZiB0aGUgc2Vjb25kIGF0dGFjaG1lbnQuCg==


--2527518bb813d2f2847a2adba8b8b47f--

【讨论】:

【参考方案2】:

我也遇到了问题,并找到了一个简洁的答案。您可以将此功能用于多个附件。

function mail_attachment($files, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message, $cc, $bcc) 

    $uid = md5(uniqid(time()));
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "cc : < $cc > \r\n" ;  // comma saparated emails
    $header .= "Bcc :  < $bcc >\r\n"; // comma saparated emails
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";

    foreach ($files as $filename) 
     
        $file = $path.$filename; // path should be document root path.
        $name = basename($file);
        $file_size = filesize($file);
        $handle = fopen($file, "r");
        $content = fread($handle, $file_size);
        fclose($handle);
        $content = chunk_split(base64_encode($content));

        $header .= "--".$uid."\r\n";
        $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
        $header .= "Content-Transfer-Encoding: base64\r\n";
        $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
        $header .= $content."\r\n\r\n";
    
    $header .= "--".$uid."--";
    return mail($mailto, $subject, "", $header);

希望对你有帮助。

【讨论】:

看起来很像***.com/questions/9519588/…Brian 的解决方案【参考方案3】:

这些是我在脚本中使用的标题

$base = basename($_FILES['upload']['name']);
$file = fopen($randname_path,'rb');
$size = filesize($randname_path);
$data = fread($file,$size);
fclose($file);
$data = chunk_split(base64_encode($data));

//boundary
$div = "==Multipart_Boundary_x".md5(time())."x";
//headers
$head = "From: $from\n".
        "MIME-Version: 1.0\n".
        "Content-Type: multipart/mixed;\n".
        " boundary=\"$div\"";
//message
$mess = "--$div\n".
        "Content-Type: text/plain; charset=\"iso-8859-1\"\n".
        "Content-Transfer-Encoding: 7bit\n\n".
        "$message\n\n".
        "--$div\n".
        "Content-Type: application/octet-stream; name=\"$base\"\n".
        "Content-Description: $base\n".
        "Content-Disposition: attachment;\n".
        " filename=\"$base\"; size=$size;\n".
        "Content-Transfer-Encoding: base64\n\n".
        "$data\n\n".
        "--$div\n";
$return = "-f$from";

http://asdlog.com/Create_form_to_send_email_with_attachment

【讨论】:

【参考方案4】:
$to = "myemail@mydomain.com";
$from = "Website <website@mydomain.com>";
$subject = "Test Attachment Email";

$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "document.pdf";

//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));

// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //

$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

// send message
if (mail($to, $subject, $body, $headers)) 
    echo "mail send ... OK";
 else 
    echo "mail send ... ERROR";

我做了什么:

将正文与标题分开 删除了放在分隔符之前的额外 EOL

【讨论】:

很清楚,尽管有其他建议的解决方案,这很有效! 对我不起作用。我只是更改了$to$filename 变量。电子邮件已发送,正文和附件都在那里,但文件损坏了。我尝试使用“.zip”和“.txt”。 @Alin Purcaru:上面的代码对我不起作用,它只是发送一个带有文件名的文件,该文件中缺少内容。 我试过这段代码一切正常,但是当我从电子邮件下载时附件变成空 @MoazAteeq 请定义$pdfdoc 变量绝对路径。这对我来说很好用。

以上是关于PHP mail() 附件问题的主要内容,如果未能解决你的问题,请参考以下文章

使用 PHP Mail() 发送附件?

如何使用带有附件的 PEAR Mail 包使用 PHP 发送电子邮件

PHP通过SMTP实现发送邮件_包括附件

基于PHP自带的mail函数实现发送邮件以及带有附件的邮件功能

带有附件的梨邮件在 $mime = new Mail_mime 处失败

我可以防止 MIME 嵌入的 html 图像显示为附件吗?