2013-08-23 304 views
1

試圖與PDF附件的電子郵件,利用swiftmailer嘗試和沒有工作,這個代碼拉鍊但不PDF :(工作PHP電子郵件PDF附件發送

$attachment = chunk_split(base64_encode(file_get_contents($filename))); 
ob_start(); //Turn on output buffering 
?> 
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

<?php echo $message."<br /><br />"; 
?> 

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?> 


Content-Type: application/octet-stream; name="<?php echo $filename?>" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; filename="<?php echo $filename?>" 

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail($to, $subject, $message, $headers); 

郵件被髮送和罰款我收到郵件:但附件不在那裏,並在meial所有的電子郵件中編碼的base64:

ontent-Type:application/octet-stream; name =「media.pdf」Content-Transfer- Encoding:base64 Content-Disposition:attachment; filename =「media.pdf」JVBERi0xLjMKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZwovT3V0bGluZXMgMiAwIFIKL1BhZ2Vz IDMgMCBSID4 + CmVuZG9iagoyIDAgb2JqCjw8IC9UeXBlIC9Pd XRsaW5lcyAvQ291bnQgMCA + PGPL bmRvYmoKMyAwIG9iago8PCAvVHlwZSAvUGFnZXMKL0tpZHMgWzYgMCBSCjE2IDAgUgpdCi9Db3Vu dCAyCi9SZXNvdXJjZXMgPDwKL1Byb2NTZXQgNCAwIFIKL0ZvbnQgPDwgCi9GMSA4IDAgUgovRjIg OSAwIFIKPj4KL1hPYmplY3QgPDwgCi9JMSAxMiAwIFIKL0kyIDE1IDAgUgovSTMgMjAgMCBSCi9J NCAyMyAwIFIKPj4KPj4KL01lZGlhQm94IFswLjAwMCAwLjAwMCA2MTIuMDAwIDc5Mi4wMDBdCiA + PgplbmRvYmoKNCAwIG9iagpbL1BERiAvVGV4dCAvSW1hZ2VDIF0KZW5kb2JqCjUgMCBvYmoKPDwK L0NyZWF0b3IgKERPTVBERikKL0NyZWF0aW9uRGF0ZSAoRDoyMDEzMDgyMzAyMDA0NCswMCcwMCcp Ci9Nb2REYXRlIChEOjIwMTMwODIzMDIwMDQ0KzAwJzAwJykKPj4KZW5kb2JqCjYgMCBvYmoKPDwg L1R5cGUgL1BhZ2UKL1BhcmVudCAzIDAgUgovQW5ub3RzIFsgMTAgMCBSIDEzIDAgUiBdCi9Db250 ZW50cyA3IDAgUgo + PgplbmRvYmoKNyAwIG9iago8PCAvRmlsdGVyIC9GbGF0ZURlY29kZQovTGVu Z3RoIDI3OCA + PgpzdHJlYW0KeJyFkb1Ow0AQhHs/xZRQsNm9/2tRAnJEA1wXpUBJSIOFIAWvz9ox FwMKyNLJmtuZb3evYWJmTM/3fXNdYJIljhbRGnIxoWwxuzEQT4zyDKwuSlvuFpdrlCUWpfmd83Cr YvT4AGOJFbDWn21Tg00mYYsO1iTKJlXlBY/fnYaMH90uE0tAZNVSVrMPhnJyVfnL3NcGzkNtCG50

+0

標題行在做什麼?看看這個swift郵件的例子。你的混合發送電子郵件與強制文件下載。 – 2013-08-23 03:19:27

回答

0

根據部分消息(ontent-Type: ...),我猜測輸出緩衝區已經被填滿並且被自動刷新,只留下之後的輸出將刷新分配給$message

--PHP-mixed-<?php echo $random_hash; ?>Content-Type: application/octet-stream; ...之間還有兩個額外的空白行可能會導致麻煩。

依靠輸出緩衝來構造一個字符串既容易出錯又完全不必要。這是更好地使用PHP的HEREDOC syntax代替:

$message = <<<MSG 
--PHP-mixed-$random_hash 
Content-Type: multipart/alternative; boundary="PHP-alt-$random_hash" 

--PHP-alt-$random_hash 
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

$message<br /><br /> 


--PHP-alt-$random_hash-- 

--PHP-mixed-$random_hash 
Content-Type: application/octet-stream; name="$filename" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; filename="$filename" 

$attachment; 
--PHP-mixed-$random_hash-- 

MSG; 

$mail_sent = @mail($to, $subject, $message, $headers); 

注意,在郵件的行尾必須是CRLF(如\ r \ n)。如果上述方法無效,您可能需要創建一個字符串有明確的行結尾:

$message = "--PHP-mixed-$random_hash\r\n" 
     . "Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n" 
     . "\r\n" 
     /* ... */ 
     . $attachment 
     . "\r\n--PHP-mixed-$random_hash--" 
     . "\r\n"; 

見PHP的mail() manual page一些進一步的細節。