2012-11-09 153 views
5

我已經嘗試了很多不同的方法,但無法使用mail()函數在PHP中通過SMTP成功發送電子郵件。使用Wordpress發送電子郵件

<?php 
require_once ABSPATH . WPINC . '/class-phpmailer.php'; 
require_once ABSPATH . WPINC . '/class-smtp.php'; 
$phpmailer = new PHPMailer(); 
$phpmailer->SMTPAuth = true; 
$phpmailer->Username = '[email protected]'; 
$phpmailer->Password = 'password01'; 
  
$phpmailer->IsSMTP(); // telling the class to use SMTP 
$phpmailer->Host       = "mail.asselsolutions.com"; // SMTP server 
$phpmailer->FromName   = $_POST[your_email]; 
$phpmailer->Subject    = $_POST[your_subject]; 
$phpmailer->Body       = $_POST[your_message];                      //HTML Body 
$phpmailer->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 
$phpmailer->WordWrap   = 50; // set word wrap 
$phpmailer->MsgHTML($_POST[your_message]); 
$phpmailer->AddAddress('[email protected]/files/', 'Wordpress support'); 
//$phpmailer->AddAttachment("images/phpmailer.gif");             // attachment 
if(!$phpmailer->Send()) { 
 echo "Mailer Error: " . $phpmailer->ErrorInfo; 
} else { 
 echo "Message sent!"; 
} 
$to = $_REQUEST['to']; 
$subject = $_REQUEST['subject']; 
$message = $_REQUEST['message']; 
$from = $_REQUEST['from']; 
$headers = "From:" . $from; 

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

echo "Mail Sent."; 
?> 

我在做什麼錯?我收到以下錯誤:

Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\wp-vtr\wp-content\themes\twentyeleven\content.php on line 8

$phpmailer->IsSMTP(); // telling the class to use SMTP" 
+1

您的錯誤信息不完整,行號缺失。轉到該行號,並告訴我們前面幾行代碼是什麼。錯誤在那裏。 – Jocelyn

+0

我已更新我的代碼,錯誤是在行否8 plz CHK它現在 – user1811549

+0

即使您發佈的代碼不完美,我沒有看到任何可能導致解析錯誤的嚴重錯誤。它是否真的是你發佈的'content.php'的代碼? – Jocelyn

回答

1

此:

$phpmailer->FromName = $_POST[your_email]; 
$phpmailer->Subject = $_POST[your_subject]; 
$phpmailer->Body  = $_POST[your_message]; 

$phpmailer->MsgHTML($_POST[your_message]); 

應該是這樣的:

$phpmailer->FromName = $_POST['your_email']; 
$phpmailer->Subject = $_POST['your_subject']; 
$phpmailer->Body  = $_POST['your_message']; 

$phpmailer->MsgHTML($_POST['your_message']); 

無論如何,看來你要發送的電子郵件都通過PHPMailer類和mail()原生PHP函數。你可能只是測試,但我不確定你想做什麼。

+0

在your_message中應用單引號之後,與之前發生的錯誤相同 – user1811549

+0

plz告訴我如何通過PHPMailer類或郵件()函數使用smtp發送郵件 – user1811549

+0

@digitalis你對錯過的單引號是正確的,但這實際上不是解析錯誤的原因。關於數組鍵的缺少單引號會導致通知或警告,而不是錯誤。 – Jocelyn