2012-06-11 93 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>"; 
     } 

?> 

这是为我工作。希望能帮助到你。