PHP邮件(mail())附件问题

16

有谁能帮我弄清楚为什么这个代码一直返回false吗?

$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";
}

3
我几天前看到过这个“把所有东西放在$headers里”的说法。你们是从哪里复制粘贴的? - Alin Purcaru
这种手动头部操作是一种不太优雅的解决方案。个人而言,我喜欢Zend_Mail,但我想知道是否还有其他“智能”方法。 - Mr Griever
@MattH.,Zend 开发人员也不需要做这个“肮脏的解决方案”吗? - Rafael Barros
4个回答

29
$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";
}

我所做的:

  • 将正文与标题分离
  • 删除在分隔符之前添加的额外行尾符

非常清晰,尽管有其他提出的解决方案,但这个可行! - Joaquín L. Robles
@Alin Purcaru:上面的代码对我来说不起作用,它只是发送一个带有文件名的文件,文件内容丢失了。 - WEshruth
我尝试了这段代码,一切都正常工作,但是当我从电子邮件中下载附加的文件时,它变成了空文件。 - Moaz Ateeq
@MoazAteeq 请定义 $pdfdoc 变量的绝对路径。这对我来说很有效。 - Chinmay235
非常感谢,这个运作得很好。我还有一个问题。你可以帮帮我吗——我的文件路径是$filename = "http://www.example.com/csv/document.csv";。这个运作得很好。但是在 Gmail 中,我想显示 测试文件 而不是 http://www.example.com/csv/document.csv。这可能吗? - Chinmay235
显示剩余2条评论

3

我也遇到了问题,并找到了一个简洁的答案。您可以使用此函数来处理多个附件。

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

希望这能有所帮助。

看起来很像Brian在https://dev59.com/smkw5IYBdhLWcg3w_Pbn上的解决方案。 - Nicolas Giszpenc

0

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

$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


0

就像我在@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--

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接