2011-06-08 96 views
15

有人可以幫我弄清楚爲什麼這會一直返回false?PHP郵件()附件問題

$to = "[email protected]"; 
$from = "Website <[email protected]>"; 
$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'」幾天前。你們從哪裏複製粘貼它? – 2011-06-08 06:42:35

+0

此手冊標題操作是一個骯髒的解決方案。就我個人而言,我是Zend_Mail的粉絲,儘管我想知道其他任何「智能」方法。 – 2012-08-03 19:18:52

+0

@MattH,Zend開發人員不必也這樣做「髒解決方案」呢? – 2013-09-30 18:50:09

回答

29
$to = "[email protected]"; 
$from = "Website <[email protected]>"; 
$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"; 
} 

我所做的:

  • 分居身體從頭部
  • 去除多餘EOLS分離之前把
+0

感謝您花時間做到這一點。有用! – HWD 2011-06-08 19:05:31

+0

非常清楚,儘管有其他建議的解決方案,這工作! – 2012-10-11 14:39:06

+0

沒有爲我工作。我只是改變'$ to'和'$ filename'變量。電子郵件被髮送,正文消息和附件在那裏,但文件被破壞。我嘗試了一個「.zip」和「.txt」。 – 2013-09-27 16:34:31

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

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

希望它有幫助。

+0

它工作完美。謝謝! – 2013-10-14 19:00:06

+0

看起來很像Brian的解決方案http://stackoverflow.com/questions/9519588/send-php-html-mail-with-attachments – 2016-06-21 16:43:10

0

就像我在@ GovindTotla的回答中評論過的,他的解決方案對我有用。爲了完整起見,下面是帶有兩個附件的$header的輸出代碼。考慮它是一個反向工程的答案:

From: [email protected] 
Reply-To: [email protected] 
Cc: [email protected] 
Bcc: [email protected] 
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--