2012-12-11 34 views
3

我正在嘗試爲PHPMailer工作的基本示例。PHPMailer郵件錯誤:郵件正文爲空

我所做的全部都是上傳整個PHPMailer_5.2.2文件夾,根據你看到的代碼配置下面的頁面,我不斷得到Mailer Error: Message body empty,但我可以清楚地看到contents.html文件中有html,並且不是空。這是我從PHPMailer的PHPMailer_5.2.2使用示例文件/例子/ test_smtp_gmail_basic.php

我用我在Outlook中的Gmail是工作方式的設置試過,我知道我的用戶名和密碼,SMTP端口爲587,它被設置爲TLS,我試圖在下面的代碼中用TLS代替SSL,但我仍然得到相同的錯誤。

我也嘗試下面的代碼,它已建議:

改變了這一點:

$mail->MsgHTML($body); 

這個

$mail->body = $body; 

我還是得到了同樣的錯誤,有沒有別的東西,我需要配置?這是我第一次使用PHPMailer,我可以使用標準的PHP郵件工作,但我想試試這個,因爲我要發郵件的頁面有很多html,我不想通過所有的html並輸入字符轉義,所以有人推薦使用這個。

<?php 

//error_reporting(E_ALL); 
error_reporting(E_STRICT); 

date_default_timezone_set('America/Toronto'); 

require_once('../class.phpmailer.php'); 
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded 

$mail    = new PHPMailer(); 

$body    = file_get_contents('contents.html'); 
$body    = preg_replace('/[\]/','',$body); 

$mail->IsSMTP(); // telling the class to use SMTP 
$mail->Host  = "smtp.gmail.com"; // SMTP server 
$mail->SMTPDebug = 1;      // enables SMTP debug information (for testing) 
              // 1 = errors and messages 
              // 2 = messages only 
$mail->SMTPAuth = true;     // enable SMTP authentication 
$mail->SMTPSecure = "ssl";     // sets the prefix to the servier 
$mail->Host  = "smtp.gmail.com";  // sets GMAIL as the SMTP server 
$mail->Port  = 587;     // set the SMTP port for the GMAIL server 
$mail->Username = "[email protected]"; // GMAIL username 
$mail->Password = "xxx";   // GMAIL password 

$mail->SetFrom('[email protected]', 'First Last'); 

$mail->AddReplyTo("xxx","First Last"); 

$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic"; 

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 

$mail->MsgHTML($body); 

$address = "xxx.net"; 
$mail->AddAddress($address, "John Doe"); 

//$mail->AddAttachment("images/phpmailer.gif");  // attachment 
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment 

if(!$mail->Send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message sent!"; 
} 

?> 
+0

你可以在'body'變量上粘貼'var_dump'的結果嗎? $ mail = new PHPMailer(); $ body = file_get_contents('contents.html'); $ body = preg_replace('/ [\] /','',$ body);後續代碼var_dump($機構); $ MAIL-> IsSMTP(); //告訴班級使用SMTP –

+0

當我運行該代碼時,它只是出現爲空,我也嘗試將完整的URL www.mysite.com/PHPMailer_5.2。2/examples/contents.html 當我訪問該URL我可以看到HTML – user1547410

+0

我試過$ body =「這是一個測試」,它第一次拒絕它,我收到一封來自谷歌的電子郵件,說它是可疑和阻止所以我去了一些驗證步驟,現在我可以發送它,如果身體是純文本,但一旦我放回: $ body = file_get_contents('contents.html'); $ body = preg_replace('/ [\] /','',$ body); 我仍然得到空的消息,並運行您的代碼我得到NULL 另一件事是即使它發送純文本消息,我沒有收到電子郵件 – user1547410

回答

0

首先,我不得不啓用了ssl擴展(這是我從另一個職位上這個網站獲得)。通過改變

;extension=php_openssl.dll 

做到這一點,打開php.ini,使php_openssl.dll到

extension=php_openssl.dll 

現在,啓用TLS並使用端口587然後註釋掉preg_replace

最後,我能夠在我的gmail「發送」文件中顯示它,儘管我期待在我的「收件箱」中看到它。

這個網站很棒!謝謝。

0

PHPMailer-> MsgHTML()試圖做一些聰明的事情,修復非絕對URL,但對我來說,它只是返回空。

$mail->IsHTML(true); 
$mail->Body=$body; 
7

我有同樣的問題,我解決只是改變:

此:

$mail->body = 'Hello, this is my message.'; 

進入這個:

$mail->Body = 'Hello, this is my message.'; 

bodyBody什麼小錯誤!

相關問題