2011-04-19 93 views
0

我有問題,而我嘗試發送帶附件的電子郵件,我只是得到空白附件的消息,我怎麼能修復它。這裏是我的代碼:電子郵件問題,同時添加附件文件

$html2pdf = new HTML2PDF("P","A4", "fr"); 
$html2pdf->writeHTML($content, isset($_GET['vuehtml'])); 
$to = "[email protected]"; 
$subject = "Voucher Mediskon"; 
$random_hash = md5(date("r", time())); 
$headers = "From: Admin Mediskon<[email protected]" . $domain . ">\r\nReply-To: [email protected]" . $domain; 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
$attachment = chunk_split(base64_encode($html2pdf->Output("", "S"))); 
ob_start(); 
?> 
--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/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

<h2>Terimakasih telah membeli voucher kami. Mediskon</h2> 
<p>Silakan <b>unduh (download)</b> voucher pada <b>attachment</b> yang kami kirim.</p> 
--PHP-alt-<?php echo $random_hash; ?>-- 
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/pdf; name="voucher.pdf" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 
<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 
<?php 
$message = ob_get_clean(); 
$mail_sent = @mail($to, $subject, $message, $headers); 
+0

也許清理你的文章一點點,這是很難看現在。 – 2011-04-19 03:58:24

+0

如果這不是爲了學習MIME郵件的工作方式,請考慮使用庫。 – 2011-04-19 09:38:07

回答

1

不要直接使用PHP的郵件功能。這是不安全和僵化的。另外你正在重新發明輪子。下面是支持HTML和文件附件兩大電子郵件庫:

+0

我不同意,它非常靈活 - 因此允許人們使用它編寫不安全的代碼。當然,如果你不知道你在做什麼,這些包裝紙可能會讓生活變得不那麼危險。 – symcbean 2011-04-19 09:32:15

+0

嗯。通過僵化,我的意思是API不適合添加附件等。是的,你可以將任何你喜歡的東西放入'mail()'中,但是很容易被絆住。 – Treffynnon 2011-04-19 09:35:01

+0

也許我會使用郵件功能簡單的電子郵件,這是很好的瞭解它.. – 2011-04-19 11:54:50

1

我剛剛得到消息,空白的附件

所以,問題不與電子郵件,也沒有將文件附加到電子郵件的過程。

您是否查看了消息中的原始數據以查看附件是否爲空長度?

您是否嘗試將生成的PDF文件寫入文件以檢查其內容?

+0

是的,我做到了,但有時我不不會得到附件...... 所以我得出的結論是我的代碼有錯誤... 你能解決它嗎或我的代碼在我的代碼中是正確的或沒有錯誤... – 2011-04-19 11:52:28