2013-04-16 112 views
57

我想发送一封电子邮件使用Gmail SMTP服务器通过PHP邮件程序通过PHP发送邮件使用Gmail SMTP服务器通过PHP邮件程序

这是我的代码

<?php 
require_once('class.phpmailer.php'); 

$mail = new PHPMailer(); 
$mail->IsSMTP(); 
$mail->CharSet="UTF-8"; 
$mail->SMTPSecure = 'tls'; 
$mail->Host = 'smtp.gmail.com'; 
$mail->Port = 587; 
$mail->Username = '[email protected]'; 
$mail->Password = 'valid password'; 
$mail->SMTPAuth = true; 

$mail->From = '[email protected]'; 
$mail->FromName = 'Mohammad Masoudian'; 
$mail->AddAddress('[email protected]'); 
$mail->AddReplyTo('[email protected]', 'Information'); 

$mail->IsHTML(true); 
$mail->Subject = "PHPMailer Test Subject via Sendmail, basic"; 
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; 
$mail->Body = "Hello"; 

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

,但我收到此以下错误

Mailer Error: SMTP Error: The following recipients failed: [email protected] 

SMTP server error: SMTP AUTH is required for message submission on port 587 

我的域名是vatandesign.ir

+0

您可能希望看看Jorj的答案在这个线程: http://stackoverflow.com/questions/5759419/using-gmail-smtp-to-send-email-with-php –

回答

103
$mail = new PHPMailer(); // create a new object 
$mail->IsSMTP(); // enable SMTP 
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only 
$mail->SMTPAuth = true; // authentication enabled 
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail 
$mail->Host = "smtp.gmail.com"; 
$mail->Port = 465; // or 587 
$mail->IsHTML(true); 
$mail->Username = "[email protected]"; 
$mail->Password = "password"; 
$mail->SetFrom("[email protected]"); 
$mail->Subject = "Test"; 
$mail->Body = "hello"; 
$mail->AddAddress("[email protected]"); 

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

这上面的代码已经过测试,并为我工作。

这可能是因为你需要$mail->SMTPSecure = 'ssl';

另外,还要确保你没有两步验证开启该帐户可能会导致问题也。

更新

你可以尝试改变$ MAIL-> SMTP到:

$mail->SMTPSecure = 'tls'; 

这是值得注意的是,某些SMTP服务器阻塞连接。 某些SMTP服务器不支持SSL(或TLS)连接。

+0

我收到这个错误,当我使用你的代码 邮件程序错误:以下发件人地址失败: [email protected]:被称为邮件()而不被连接 –

+2

更新的答案和进一步的建议。无论如何,代码无疑是很好的。你可以检查这两个来源http://phpmailer.worxware.com/index.php?pg=exampleagmail和https://github.com/Synchro/PHPMailer/blob/master/examples/test_smtp_gmail_advanced.php – Anderscc

+0

我越来越错误为:CLIENT - > SMTP:EHLO localhost SMTP - >错误:EHLO未被服务器接受:CLIENT - > SMTP:HELO localhost 注意:fwrite():发送16字节失败,errno = 10054现有连接被强制关闭由远程主机。在线1023上的C:\ xampp \ htdocs \ program \ mailsending1 \ mailsending_v1 \ PHPMailer-master \ class.smtp.php SMTP - >错误:HELO未被服务器接受:SMTP - >注意:EOF在检查连接时是否被捕获SMTP连接()失败。邮件程序错误:SMTP连接()失败。 –

5

看来您的服务器无法建立与Gmail SMTP服务器的连接。 这里有一些提示来解决这个问题: 1)检查你的PHP上是否正确配置了SSL(处理它的模块默认没有安装在PHP上,你必须在phph.ini中检查你的配置)。 2)检查你的防火墙是否允许传出呼叫到所需的端口(这里是465或587)。使用telnet来做到这一点。如果端口未打开,则需要sysdmin提供一些支持来设置配置。 我希望你能快点解决这个问题!

3

打开此Link并选择按照说明Google服务器模块来自未知服务器的任何企图所以一旦你点击验证码检查每一件事情都会好起来的

+1

我使用了与接受的答案相同的代码,但仍会得到SMTP_connect()失败。我尝试了这个链接中的所有内容,并且最终以正确的方式运行 – Ivan

+1

我也遇到了SMTP_connect()失败的问题。我遵循上述步骤,仍然出现错误。我在两个不同的gmail地址上执行了两次这两个步骤,而且两次都好像我需要等一个小时才能看到变化。然后,所有事情都按预期工作。 – cwal

+0

访问https://www.google.com/accounts/DisplayUnlockCaptcha,然后运行我的代码是它为我工作。 +1 –

7

另外值得一提的是,如果你启用了双因素身份认证,你”您需要设置一个特定于应用程序的密码来代替您的电子邮件帐户的密码。

您可以通过以下说明生成应用专用密码: https://support.google.com/accounts/answer/185833

然后设置$mail->Password您的应用程序特定密码。

0

我认为它是连接问题,你可以得到代码在这里http://skillrow.com/sending-mail-using-smtp-and-php/

include(“smtpfile.php“); 
include(“saslfile.php“); // for SASL authentication $from=”[email protected]“; //from mail id 

$smtp=new smtp_class; 

$smtp->host_name=”www.abc.com“; // name of host 
$smtp->host_port=25;//port of host 

$smtp->timeout=10; 
$smtp->data_timeout=0; 
$smtp->debug=1; 
$smtp->html_debug=1; 
$smtp->pop3_auth_host=””; 
$smtp->ssl=0; 
$smtp->start_tls=0; 
$smtp->localhost=”localhost“; 
$smtp->direct_delivery=0; 

$smtp->user=”smtp username”; 
$smtp->realm=””; 
$smtp->password=”smtp password“; 

$smtp->workstation=””; 
$smtp->authentication_mechanism=””; 

$mail=$smtp->SendMessage($from,array($to),array(“From:$from”,”To: $to”,”Subject: $subject”,”Date: ”.strftime(“%a, %d %b %Y %H:%M:%S %Z”)),”$message”); 

if($mail){ 
    echo “Mail sent“; 
}else{ 
    echo $smtp->error; 
} 
+0

仅限链接回答通常是不鼓励的。你可以把它扩展成独立的东西吗? – Werner

24

所以我只是解决了我自己的“SMTP连接失败”的错误,我想后的解决方案,以防万一它可以帮助别人。

我使用PHPMailer示例gmail.phps文件中给出的EXACT代码。它只是在我使用MAMP的时候工作,然后当我将它移动到我的个人服务器时,我得到了SMTP连接错误。

我读过所有堆栈溢出的答案,PHPMailer的所有疑难解答文档都表示这不是PHPMailer的问题。这是服务器端的设置问题。我尝试了不同的端口(587,465,25),我尝试了'SSL'和'TLS'加密。我检查了我的php.ini文件中启用了openssl。我检查了没有防火墙问题。一切都检出了,仍然没有。

的解决方案是,我不得不删除这条线:

$mail->isSMTP(); 

现在所有的作品。我不知道为什么,但它有效。我的代码的其余部分从PHPMailer示例文件复制并粘贴。

+4

此解决方案的工作原理是完全关闭SMTP。我不推荐这种方法。我从来没有解决过这个问题,现在我转而使用Mandrill API。 –

+0

只要使用Mandrill,它会好得多,而且实际上有一些分析。 –

+0

这是为什么这样工作? – ykay

1

Google根据可用的用户信息对待Gmail帐户的方式有所不同,可能会阻止垃圾邮件发送者。

我无法使用SMTP,直到我进行了电话验证。做了另一个帐户来复查,我能够确认它。

2

不能发表评论,但肯定的,除去

$mail->isSMTP(); 

,你会被罚款!

+0

我不知道为什么,但这对我有效。非常感谢。 –

+1

关闭SMTP,这会破坏此问题的整个目的。 – Paulo

0

此代码工作正常,我

$mail = new PHPMailer; 
    //Enable SMTP debugging. 
    $mail->SMTPDebug = 0; 
    //Set PHPMailer to use SMTP. 
    $mail->isSMTP(); 
    //Set SMTP host name       
    $mail->Host = $hostname; 
    //Set this to true if SMTP host requires authentication to send email 
    $mail->SMTPAuth = true; 
    //Provide username and password  
    $mail->Username = $sender; 
    $mail->Password = $mail_password; 
    //If SMTP requires TLS encryption then set it 
    $mail->SMTPSecure = "ssl"; 
    //Set TCP port to connect to 
    $mail->Port = 465; 
    $mail->From = $sender; 
    $mail->FromName = $sender_name; 
    $mail->addAddress($to); 
    $mail->isHTML(true); 
    $mail->Subject = $Subject; 
    $mail->Body = $Body; 
    $mail->AltBody = "This is the plain text version of the email content"; 
    if (!$mail->send()) { 
     echo "Mailer Error: " . $mail->ErrorInfo; 
    } 
    else { 
      echo 'Mail Sent Successfully'; 
    } 
0

Anderscc已经得到了它正确。谢谢。它为我工作,但不是100%。

我必须设置

$ mail-> SMTPDebug = 0; 将它设置为1可能会导致错误,特别是如果您将某些数据作为json传递到下一页。示例 - 如果使用json通过ajax传递数据,则发送邮件时执行验证。

我不得不降低我的Gmail帐户的安全设置,以摆脱错误的: “SMTP连接()失败”和“SMTP错误:密码命令失败”

解决方案: 这个问题可以通过引起'不太安全'的应用程序试图使用电子邮件帐户(这是根据谷歌的帮助,不知道他们如何判断什么是安全的,什么不是)或者如果你想连续登录几次或如果你改变国家(例如使用VPN,将代码移到不同的服务器或实际尝试从世界的不同地区登录)。

链接,解决这个问题(你必须登录到谷歌帐户):

注: 你可以到下面的计算器答案链接了解更多详细的参考。

https://stackoverflow.com/a/25175234

相关问题