2013-05-14 97 views
0

我遇到了PHPMailer的一些奇怪問題。我試圖發送一些我用PHP和HTML生成的內容,但是正文會被截斷。更奇怪的是,這隻發生在我生成的電子郵件上,如果我在那裏放置一些更長的通用內容,它會被正確發送。我還必須提及,我確實迴應了$content$nohtmlcontent變量的內容,並且所有內容都像應該出現的那樣,但是當我收到我的郵箱中的電子郵件時,它會被截斷。PHPMailer正文被截斷

$content="<BODY bgColor=\"#ffffff\"><FONT face=\"Verdana\" size=\"2\">"; 
$content.="Hello $name.<br /><br />Administrator of <a href=\"http://$url\">$url</a> has created a new account for you.<br /><br />Your new account details:<br />"; 
$content.=$message."<br /><br />"; 
$content.="If you see something wrong, please reply with correct details and we will update your account.<br /><br />"; 
$content.="Have a nice day,<br />$url</FONT></FONT></BODY>"; 

$nohtmlcontent="Hello $name.\n\nAdministrator of $url has created a new account for you.\n\nYour new account details:\n\n"; 
$nohtmlcontent.=$usrEmail."\n\n"; 
$nohtmlcontent.="If you see something wrong, please reply with correct details and we will update your account.\n\n"; 
$nohtmlcontent.="Have a nice day,\n$url"; 

所有變量都具有適當的數據填充:創建純文本和HTML電子郵件正文

我的PHP代碼。用於發送電子郵件

我PHPMailer的代碼:

require_once("class.phpmailer.php"); 
$mail=new PHPMailer(true); 
try { 
    $mail->AddAddress($email); 
    $mail->SetFrom('[email protected]', 'example.com'); 
    $mail->CharSet = 'UTF-8'; 
    $mail->Subject = "New account for you"; 
    $mail->IsHTML(true); 
    $mail->AltBody = $nohtmlcontent; 
    $mail->Body = $content; 
    $mail->Send(); 
    return true; 
}catch(phpmailerException $e){ 
    trigger_error("PHPMailer failed: ".$e->errorMessage()); 
    return false; 
} 

結果:

Hello 12 23. 

Administrator of admin.localhost.dev has created a new account for you. 

Your new account details: 
Username: user1 
Password: 123456 
E-Mail Address: [email protected] 
Subscription Status: Not Verified (you must verify your email address before you can use your account) 
Package: Free (limitations: 1 tour, 5 items) 
First Name: 12 
Last Name: 23 
City 34 
Country 45 

Your verification link: http://admin.localhost.dev/verify-account/882672636ce2ad8c498f75a9b836ff055aecf573/ 

If you see something wrong, please reply with correct details and we will update you 

預期結果:

Hello 12 23. 

Administrator of admin.localhost.dev has created a new account for you. 

Your new account details: 
Username: user1 
Password: 123456 
E-Mail Address: [email protected] 
Subscription Status: Not Verified (you must verify your email address before you can use your account) 
Package: Free (limitations: 1 tour, 5 items) 
First Name: 12 
Last Name: 23 
City 34 
Country 45 

Your verification link: http://admin.localhost.dev/verify-account/882672636ce2ad8c498f75a9b836ff055aecf573/ 

If you see something wrong, please reply with correct details and we will update your account. 

Have a nice day, 
admin.localhost.dev 

請注意到底是額外的內容。

我也嘗試使用PHP的函數mail()發送相同的內容,它也被截斷。

任何想法?

解決方案: PHP代碼生成很長的一行,添加了幾個換行符後,完整的內容通過。

回答

1

而且這可能不相關,但你用肢體而不是MsgHTML

$mail->Body = $content; 

但它看起來使用HTML表現在你的內容你喜歡。

我是相當新的,但這個從我讀過的PHPMailer的使用HTML,你應該使用

$mail->MsgHTML = $content; 

雖然你的文字看起來很喜歡它顯示的是罰款和你解決你的問題。但我認爲我會分享它的幫助。

這裏一些有用的信息 https://phpbestpractices.org/ (向下滾動到電子郵件信息)

這裏: https://github.com/PHPMailer/PHPMailer

+0

我從來沒有注意到,有一個'$ MAIL-> MsgHTML'可用,在所有的例子(和所以我總是注意'$ mail-> Body',但是必須提供'$ mail-> IsHTML(true)',否則PHPMailer會假定內容是純文本,錯誤的Content-Type '發送。 – 2014-04-05 12:34:46