2012-06-11 59 views
2

我正在嘗試使用PHPMailer在我的網站上創建一個聯繫表單。我在設置時遇到了一些麻煩。我正在嘗試使用G-mail作爲我的smtp主機。我想知道是否有人可以幫助解決這個問題?PHPMailer無法正常工作:無法發送消息

這是我的郵件代碼:

<?php 
require("class.phpmailer.php"); 
require("class.smtp.php"); 

$mail = new PHPMailer(); 

$mail->IsSMTP(); 
$mail->SMTPAuth = true;  // turn on SMTP authentication  
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail   
$mail->Host = 'smtp.gmail.com'; 
$mail->Port = 467; 

$mail->Username = "[email protected]"; // SMTP username 
$mail->Password = "workingpassword"; // SMTP password 

$mail->From = "[email protected]"; 
$mail->FromName = "Mailer"; 
$mail->AddAddress("[email protected]", "Josh Adams"); 
$mail->AddAddress("[email protected]");     // name is optional 
$mail->AddReplyTo("[email protected]", "Information"); 

$mail->WordWrap = 50;         // set word wrap to 50 characters 


// $mail->AddAttachment("/var/tmp/file.tar.gz");   // add attachments 
    // $mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // optional name 
    $mail->IsHTML(true);         // set email format to HTML 

$mail->Subject = "Here is the subject"; 
$mail->Body = "This is the HTML message body <b>in bold!</b>"; 
$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; 

if(!$mail->Send()) 
{ 
    echo "Message could not be sent. <p>"; 
    echo "Mailer Error: " . $mail->ErrorInfo; 
    exit; 
} 

echo "Message has been sent"; 
?> 

錯誤消息:

Message could not be sent. 
Mailer Error: The following From address failed: [email protected] 
+2

很難說它爲什麼不起作用。你嘗試過'$ mail-> SMTPDebug = 1;' –

+0

是的,我有。這是它顯示的錯誤:SMTP - >錯誤:無法連接到服務器:無法找到套接字傳輸「ssl」 - 您忘記配置PHP時啓用它嗎? (12640394) – AnchovyLegend

回答

7

你看,並試圖從這種問答的信息?

PHPMailer: SMTP Error: Could not connect to SMTP host

特別是,這是否提供任何額外的信息?

$mail->SMTPDebug = 1; 
+0

感謝您的迴應,dubugging肯定幫助了一些人。這是smtp調試代碼添加到屏幕上的錯誤:SMTP - >錯誤:無法連接到服務器:無法找到套接字傳輸「ssl」 - 您忘記在配置PHP時啓用它嗎? (12640394) – AnchovyLegend

+5

根據其他Q,這聽起來像你必須啓用 extension = php_openssl.dll。 – technoTarek

1

得到了同樣的錯誤,問題是,我試圖發送電子郵件從「boy333 @在**」賬戶「在** girl333 @」假裝的。我只是改變了

$mail->From = '[email protected]**' 

以我實際連接的用戶名。所以我改爲:

$mail->From = '[email protected]**' 

想法是,這兩個字段是用相同的用戶名。

$mail->Username = "boy333"; 
$mail->From = '[email protected]**'; 
1
<?php 

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


    $nameField = $_POST['name']; 
    $emailField = $_POST['email']; 
    $messageField = $_POST['message']; 
    $phoneField = $_POST['contactno']; 
    $cityField = $_POST['city']; 

$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch 

$mail->IsSMTP(); // telling the class to use SMTP 

$body .= $nameField; 

try { 
    //$mail->Host  = "mail.gmail.com"; // SMTP server 
     $mail->SMTPDebug = 2;      // enables SMTP debug information (for testing) 
     $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  = 465; // set the SMTP port for the GMAIL server 
     $mail->SMTPKeepAlive = true; 
     $mail->Mailer = "smtp"; 
     $mail->Username = "[email protected]"; // GMAIL username 
     $mail->Password = "********";   // GMAIL password 
     $mail->AddAddress('[email protected]', 'abc'); 
     $mail->SetFrom('[email protected]', 'def'); 
     $mail->Subject = 'PHPMailer Test Subject via mail(), advanced'; 
     $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically 
     $mail->MsgHTML($body); 
     $mail->Send(); 
     echo "Message Sent OK</p>\n"; 
     header("location: ../test.html"); 
} catch (phpmailerException $e) { 
     echo $e->errorMessage(); //Pretty error messages from PHPMailer 
} catch (Exception $e) { 
     echo $e->getMessage(); //Boring error messages from anything else! 
} 

?> 

去谷歌設置並啓用 '不太安全' 的應用程序。它爲我工作。

0
ping smtp.gmail.com 

出於某種原因,谷歌重定向SMTP請求gmail-smtp-msa.l.google.com

PING gmail-smtp-msa.l.google.com (74.125.133.108): 56 data bytes 
64 bytes from 74.125.133.108: icmp_seq=0 ttl=43 time=72.636 ms 
64 bytes from 74.125.133.108: icmp_seq=1 ttl=43 time=68.841 ms 
64 bytes from 74.125.133.108: icmp_seq=2 ttl=43 time=68.500 ms 

所以你應該把最終目的地在你的配置,如PHPMailer的不跟隨重定向。您也可以嘗試通過將該欄位留爲空白來發送沒有SMTPSecure的電子郵件。

$mail->Host = 'gmail-smtp-msa.l.google.com'; 
$mail->SMTPAuth = true;        
$mail->Username = 'email'; 
$mail->Password = 'password';  
$mail->SMTPSecure = ''; 
$mail->Port = 25; 
0

如果您的電子郵件中gsuite檢查兩個步驟: https://support.google.com/a/answer/6260879?hl=en https://accounts.google.com/DisplayUnlockCaptcha

PHP的郵件設置; PHPMailer 5.2.22版 檢查class.phpmailer。PHP,

var $Host = 'smtp.gmail.com'; 
    var $Port = 465; 
    var $Helo ='domain.net'; 
    public $SMTPSecure = 'ssl'; 
    public $SMTPAutoTLS = true; 
    public $SMTPAuth = true; 
    public $SMTPOptions = array(); 
    public $Username = 'gmail-username';//or domain credentials on google mail 
    public $Password = 'gmail-password'; 
    public $Timeout = 10; 
    public $SMTPDebug = true; 

郵件發送PHP

<?php 
require('PHPMailer/class.phpmailer.php'); 
require('PHPMailer/class.smtp.php'); 

$headers = "Content-Type: text/html; charset=UTF-8";  
$headers = 'From: '.$email_from."\r\n". 
'Reply-To: '.$email_from."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
$mail = new PHPMailer(); 
$mail->IsSMTP(); 
$mail->CharSet="SET NAMES UTF8"; 
$mail->SMTPDebug = 0; 
$mail->CharSet="utf8"; 
$mail->Debugoutput = 'html'; 
$mail->host  = "smtp.gmail.com"; 
$mail->port  = 465; 
$mail->smtpauth = true; 
$mail->smtpsecure = 'ssl'; 
$mail->username = "gmail-username"; //or domain credentials on google mail 
$mail->password = "gmail-password"; 
$mail->SetFrom('from-email', 'from-name'); 
$mail->AddAddress('to-address', 'to-address'); 
$mail->Body = 'your-text'; //emailbody 
if(!$mail->Send()) 
     { 
      mysql_close(); 
     echo "<script>alert('Error: " . $mail->ErrorInfo."');</script>"; 
     } 
     else 
     { 
     mysql_close(); 
     echo "<script>alert('success');</script>"; 
     } 

?> 

這是爲我工作。希望能幫助到你。