2013-03-21 118 views
1

當我使用PHP發送郵件時,它在Gmail中顯示正確,但在Outlook中顯示正確。這是一封帶有PDF附件和一些文本的郵件。 PDF使用fpdf創建並作爲附件發送,這在Gmail和Outlook中運行良好。PHP郵件在Gmail中打開,但不在Outlook中

唯一的問題是,在Gmail中電子郵件的文本顯示正確,在Outlook中它只是一個空白頁面,文本是一個額外的附件。

這裏是我的代碼:

// encode data 
$pdfdoc = $pdf->Output("", "S"); 
$attachment = chunk_split(base64_encode($pdfdoc)); 

// email stuff (change data below) 
$to = $contactEmail; 
$from = $myEmail; 
$subject = "MySubjectHere"; 
$message = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
      "http://www.w3.org/TR/html4/loose.dtd"> 
      <html><head></head><body> 
      <p>Hi '$contactName.',</p><br> 
      <p>Please find your pdf attached per your request.</p> 
      <p>Feel free to call or email if you have any questions.</p> 
      <p>Kind regards,</p><br> 
      <p>MyNameHere</p> 
      <p><strong>Manager</strong></p> 
      <img alt="image" src="http://LinkToImage.png"/> 
      <p style="color:#76923c;"> 
      <strong>P:</strong> 01 2345 6789| 
      <strong>W:</strong> <a href="http://www.example.com" target="_blank">www.example.com</a></p> 
      <p style="color:#76923c;"> 
      <strong>A:</strong> 94 Test Street. 
      </p></body></html>'; 

// a random hash will be necessary to send mixed content 
$separator = md5(time()); 

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

// attachment name 
$filename = $id."_".str_replace(" ","_",$Name).'.pdf'; 

// main header 
$headers = "From: ".$from.$eol; 
$headers .= "Bcc: ".$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; 

// 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 
mail($to, $subject, $body, $headers); 
+0

我有問題。你將如何使用PHP發送和接收電子郵件給Outlook?謝謝 – User014019 2014-11-13 07:09:41

+0

是的,你會的。你只需要確保頭部是正確的,並且在消息結尾處有兩行結束符。 如果你的結構是正確的,你將能夠在所有客戶端查看郵件。 – 2014-11-13 13:42:05

+0

如何?我嘗試發送電子郵件到Outlook,但我沒有收到任何東西。我應該把SMTP服務器和端口? – User014019 2014-11-14 04:00:39

回答

2

我發現哪裏出了問題:

$body .= $message.$eol; 

應該

$body .= $message.$eol.$eol; 

終於找到了後3天!
感謝您的迴應!

0
$to = $contactEmail; 
$from = $myEmail; 
$boundary = md5(date('r', time())); 
$msg = "This is a multi-part message in MIME format.\n\n"; 
$msg .= "--$boundary\n"; 
$msg .= "Content-Type: text/html; charset=utf-8; format=flowed; delsp=yes\n"; 
$msg .= "Content-Transfer-Encoding: 7bit\n\n"; 
$msg .= "<h1>Website Enquiry</h1> 
      <p><strong>Name:</strong> $name<br /> 
      <strong>Email:</strong> $email<br /> 
      <strong>Phone:</strong> $phone<br /> 
      <strong>Subject Name:</strong> $subject<br /> 
      <strong>Question:</strong> $question</p>"; 

$e_headers = "From: " . $from . "\n"; 
$e_headers .= "MIME-Version: 1.0\n"; 
$e_headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\n"; 

mail($to, $subject, $msg, $e_headers); 

這是代碼,我會用一個HTML聯繫表。 你不應該需要通過PHP_EOL的內容傳遞,只是附件

+0

謝謝,但它沒有工作,仍然是同樣的問題。 – 2013-03-21 08:20:16

0
$separator = md5(time()); 
$eol = "\r\n"; 
$message=""; // Always empty 
$mailmessage="<h1>Yes got it</h1>"; //html content 

$headers = "From: ".$email.$eol; 
$headers .= "Bcc:". $Bcc. "\n"; 
$headers .= "Cc:". $cc. "\n"; 
$headers .= "Return-Path: <".$email.">\n"; 
$headers .= "MIME-Version: 1.0" . $eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol; 
$headers .= "Content-Transfer-Encoding: 7bit" . $eol; 

$body .= "--" . $separator . "\r\n"; 
$body .= "Content-type:text/plain; charset=iso-8859-1\r\n"; 
$body .= "Content-Transfer-Encoding: 7bit\r\n"; 
$body .= $message . "\r\n"; 

$body = "--" . $separator . $eol; 
$body .= "Content-type: text/html; charset=\"utf-8\"\r\n"; 
$body .= "Content-Transfer-Encoding: 8bit\r\n\r\n"; 
$body .= $mailmessage . $eol.$eol; 

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

mail($to, $subject, $body, $headers); 
相關問題