通过PHP邮件发送器使用Gmail SMTP服务器发送电子邮件

时间:2022-08-12 18:14:14

I would like to send an email using Gmail SMTP server through PHP Mailer.

我想通过PHP Mailer使用Gmail SMTP服务器发送邮件。

this is my code

这是我的代码

<?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 = 'MyUsername@gmail.com';
$mail->Password = 'valid password';
$mail->SMTPAuth = true;

$mail->From = 'MyUsername@gmail.com';
$mail->FromName = 'Mohammad Masoudian';
$mail->AddAddress('anotherValidGmail@gmail.com');
$mail->AddReplyTo('phoenixd110@gmail.com', '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!";
}
?>

but i recieve this following error

但我有以下错误

Mailer Error: SMTP Error: The following recipients failed: anotherValidGmail@gmail.com

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

my domain is vatandesign.ir

我的域名是vatandesign.ir

12 个解决方案

#1


105  

$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@gmail.com";
$mail->Password = "password";
$mail->SetFrom("example@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email@gmail.com");

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

This code above has been tested and worked for me.

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

It could be that you needed $mail->SMTPSecure = 'ssl';

可能需要$mail->SMTPSecure = 'ssl';

Also make sure you don't have two step verification switched on for that account as that can cause problems also.

还要确保你没有为那个帐户开启两个步骤的验证,因为那也会引起问题。

UPDATED

更新

You could try changing $mail->SMTP to:

您可以尝试将$mail->SMTP更改为:

$mail->SMTPSecure = 'tls';

It's worth noting that some SMTP servers block connections. Some SMTP servers don't support SSL (or TLS) connections.

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

#2


24  

So I just solved my own "SMTP connection failure" error and I wanted to post the solution just in case it helps anyone else.

所以我解决了我自己的“SMTP连接失败”错误,我想发布这个解决方案,以防它对其他人有帮助。

I used the EXACT code given in the PHPMailer example gmail.phps file. It worked simply while I was using MAMP and then I got the SMTP connection error once I moved it on to my personal server.

我使用了PHPMailer示例gmail中给出的确切代码。php文件。它在我使用MAMP时工作得很简单,然后当我将它转移到我的个人服务器时,就会出现SMTP连接错误。

All of the Stack Overflow answers I read, and all of the troubleshooting documentation from PHPMailer said that it wasn't an issue with PHPMailer. That it was a settings issue on the server side. I tried different ports (587, 465, 25), I tried 'SSL' and 'TLS' encryption. I checked that openssl was enabled in my php.ini file. I checked that there wasn't a firewall issue. Everything checked out, and still nothing.

我读过的所有堆栈溢出回答,以及PHPMailer的所有故障排除文档都说PHPMailer没有问题。这是服务器端的设置问题。我尝试了不同的端口(587,465,25),我尝试了“SSL”和“TLS”加密。我检查了我的php中是否启用了openssl。ini文件。我检查过没有防火墙问题。一切都办妥了,还是什么都没有。

The solution was that I had to remove this line:

解决办法是我必须去掉这条线:

$mail->isSMTP();

Now it all works. I don't know why, but it works. The rest of my code is copied and pasted from the PHPMailer example file.

现在一切顺利。我不知道为什么,但它确实有效。我的其余代码是从PHPMailer示例文件中复制和粘贴的。

#3


7  

Also worth noting that if you have two factor authentication enabled, you'll need to setup an application specific password to use in place of your email account's password.

同样值得注意的是,如果启用了双因素身份验证,则需要设置一个应用程序特定的密码,以替代电子邮件帐户的密码。

You can generate an application specific password by following these instructions: https://support.google.com/accounts/answer/185833

您可以按照以下指示生成应用程序特定的密码:https://support.google.com/accounts/answer/185833。

Then set $mail->Password to your application specific password.

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

#4


5  

It seems that your server fails to establish a connection to Gmail SMTP server. Here are some hints to troubleshoot this: 1) check if SSL correctly configured on your PHP (module that handle it isn't installed by default on PHP. You have to check your configuration in phph.ini). 2) check if your firewall let outgoing calls to the required port (here 465 or 587). Use telnet to do so. If the port isn't opened, you'll then require some support from sysdmin to setup the config. I hope you'll sort this out quickly!

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

#5


3  

Open this Link and select follow the instructions google servers blocks any attempts from unknown servers so once you click on captcha check every thing will be fine

打开这个链接并选择遵循指令谷歌服务器阻止来自未知服务器的任何尝试,所以一旦您点击captcha检查,一切都会好起来

#6


2  

Can't comment but yes, remove

不能评论但是可以,删除吗

$mail->isSMTP();

and you will be fine!

你会没事的!

#7


1  

Google treat Gmail accounts differently depending on the available user information, probably to curb spammers.

谷歌根据可用的用户信息不同对待Gmail账户,可能是为了遏制垃圾邮件发送者。

I couldn't use SMTP until I did the phone verification. Made another account to double check and I was able to confirm it.

在进行电话验证之前,我不能使用SMTP。我又做了一个账户复查,我确认了。

#8


0  

I think it is connection issue you can get code here http://skillrow.com/sending-mail-using-smtp-and-php/

我认为这是连接问题,你可以在http://skillrow.com/sending-mail- usingsmtp -and-php/中找到代码

include(“smtpfile.php“);
include(“saslfile.php“); // for SASL authentication $from=”my@website.com“; //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;
}

#9


0  

this code working fine for me

这个代码对我来说很好用

    $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';
    }

#10


0  

Anderscc has got it correct. Thanks. It worked for me but not 100%.

Anderscc的说法是正确的。谢谢。它对我有效,但不是100%有效。

I had to set

我已经设置

$mail->SMTPDebug = 0; Setting it to 1, can cause errors especially if you are passing some data as json to next page. Example - Performing verification if mail is sent, using json to pass data through ajax.

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

I had to lower my gmail account security settings to get rid of errors: " SMTP connect() failed " and " SMTP ERROR: Password command failed "

我不得不降低gmail帐户的安全设置,以消除错误:“SMTP connect() failed”和“SMTP ERROR: Password命令failed”

Solution: This problem can be caused by either 'less secure' applications trying to use the email account (this is according to google help, not sure how they judge what is secure and what is not) OR if you are trying to login several time in a row OR if you change countries (for example use VPN, move code to different server or actually try to login from different part of the world).

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

Links that fix the problem (you must be logged into google account):

修复问题的链接(您必须登录到谷歌帐户):

  • view recent attempts to use the account and accept suspicious access.

    查看最近使用该帐户的尝试并接受可疑访问。

  • link to disable the feature of blocking suspicious apps/technologies:

    链接以禁用屏蔽可疑应用程序/技术的功能:

    https://www.google.com/settings/u/1/security/lesssecureapps

    https://www.google.com/settings/u/1/security/lesssecureapps

Note: You can go to the following * answer link for more detailed reference.

注意:您可以访问下面的*答案链接以获得更详细的参考。

https://*.com/a/25175234

https://*.com/a/25175234

#11


0  

If you are using cPanel you should just click the wee box that allows you to send to external servers by SMTP.

如果您正在使用cPanel,您应该单击允许您通过SMTP发送到外部服务器的wee框。

Login to CPanel > Tweak Settings > All> "Restrict outgoing SMTP to root, exim, and mailman (FKA SMTP Tweak)"

登录以限制CPanel >微调设置>所有>“输出SMTP到根、exim和mailman (FKA SMTP微调)”

As answered here:

作为回答:

"Password not accepted from server: 535 Incorrect authentication data" when sending with GMail and phpMailer

使用GMail和phpMailer发送时,“服务器不接受密码:535不正确的身份验证数据”

#12


0  

 $mail->SMTPOptions = array(
                'ssl' => array(
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                )
            );

#1


105  

$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@gmail.com";
$mail->Password = "password";
$mail->SetFrom("example@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email@gmail.com");

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

This code above has been tested and worked for me.

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

It could be that you needed $mail->SMTPSecure = 'ssl';

可能需要$mail->SMTPSecure = 'ssl';

Also make sure you don't have two step verification switched on for that account as that can cause problems also.

还要确保你没有为那个帐户开启两个步骤的验证,因为那也会引起问题。

UPDATED

更新

You could try changing $mail->SMTP to:

您可以尝试将$mail->SMTP更改为:

$mail->SMTPSecure = 'tls';

It's worth noting that some SMTP servers block connections. Some SMTP servers don't support SSL (or TLS) connections.

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

#2


24  

So I just solved my own "SMTP connection failure" error and I wanted to post the solution just in case it helps anyone else.

所以我解决了我自己的“SMTP连接失败”错误,我想发布这个解决方案,以防它对其他人有帮助。

I used the EXACT code given in the PHPMailer example gmail.phps file. It worked simply while I was using MAMP and then I got the SMTP connection error once I moved it on to my personal server.

我使用了PHPMailer示例gmail中给出的确切代码。php文件。它在我使用MAMP时工作得很简单,然后当我将它转移到我的个人服务器时,就会出现SMTP连接错误。

All of the Stack Overflow answers I read, and all of the troubleshooting documentation from PHPMailer said that it wasn't an issue with PHPMailer. That it was a settings issue on the server side. I tried different ports (587, 465, 25), I tried 'SSL' and 'TLS' encryption. I checked that openssl was enabled in my php.ini file. I checked that there wasn't a firewall issue. Everything checked out, and still nothing.

我读过的所有堆栈溢出回答,以及PHPMailer的所有故障排除文档都说PHPMailer没有问题。这是服务器端的设置问题。我尝试了不同的端口(587,465,25),我尝试了“SSL”和“TLS”加密。我检查了我的php中是否启用了openssl。ini文件。我检查过没有防火墙问题。一切都办妥了,还是什么都没有。

The solution was that I had to remove this line:

解决办法是我必须去掉这条线:

$mail->isSMTP();

Now it all works. I don't know why, but it works. The rest of my code is copied and pasted from the PHPMailer example file.

现在一切顺利。我不知道为什么,但它确实有效。我的其余代码是从PHPMailer示例文件中复制和粘贴的。

#3


7  

Also worth noting that if you have two factor authentication enabled, you'll need to setup an application specific password to use in place of your email account's password.

同样值得注意的是,如果启用了双因素身份验证,则需要设置一个应用程序特定的密码,以替代电子邮件帐户的密码。

You can generate an application specific password by following these instructions: https://support.google.com/accounts/answer/185833

您可以按照以下指示生成应用程序特定的密码:https://support.google.com/accounts/answer/185833。

Then set $mail->Password to your application specific password.

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

#4


5  

It seems that your server fails to establish a connection to Gmail SMTP server. Here are some hints to troubleshoot this: 1) check if SSL correctly configured on your PHP (module that handle it isn't installed by default on PHP. You have to check your configuration in phph.ini). 2) check if your firewall let outgoing calls to the required port (here 465 or 587). Use telnet to do so. If the port isn't opened, you'll then require some support from sysdmin to setup the config. I hope you'll sort this out quickly!

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

#5


3  

Open this Link and select follow the instructions google servers blocks any attempts from unknown servers so once you click on captcha check every thing will be fine

打开这个链接并选择遵循指令谷歌服务器阻止来自未知服务器的任何尝试,所以一旦您点击captcha检查,一切都会好起来

#6


2  

Can't comment but yes, remove

不能评论但是可以,删除吗

$mail->isSMTP();

and you will be fine!

你会没事的!

#7


1  

Google treat Gmail accounts differently depending on the available user information, probably to curb spammers.

谷歌根据可用的用户信息不同对待Gmail账户,可能是为了遏制垃圾邮件发送者。

I couldn't use SMTP until I did the phone verification. Made another account to double check and I was able to confirm it.

在进行电话验证之前,我不能使用SMTP。我又做了一个账户复查,我确认了。

#8


0  

I think it is connection issue you can get code here http://skillrow.com/sending-mail-using-smtp-and-php/

我认为这是连接问题,你可以在http://skillrow.com/sending-mail- usingsmtp -and-php/中找到代码

include(“smtpfile.php“);
include(“saslfile.php“); // for SASL authentication $from=”my@website.com“; //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;
}

#9


0  

this code working fine for me

这个代码对我来说很好用

    $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';
    }

#10


0  

Anderscc has got it correct. Thanks. It worked for me but not 100%.

Anderscc的说法是正确的。谢谢。它对我有效,但不是100%有效。

I had to set

我已经设置

$mail->SMTPDebug = 0; Setting it to 1, can cause errors especially if you are passing some data as json to next page. Example - Performing verification if mail is sent, using json to pass data through ajax.

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

I had to lower my gmail account security settings to get rid of errors: " SMTP connect() failed " and " SMTP ERROR: Password command failed "

我不得不降低gmail帐户的安全设置,以消除错误:“SMTP connect() failed”和“SMTP ERROR: Password命令failed”

Solution: This problem can be caused by either 'less secure' applications trying to use the email account (this is according to google help, not sure how they judge what is secure and what is not) OR if you are trying to login several time in a row OR if you change countries (for example use VPN, move code to different server or actually try to login from different part of the world).

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

Links that fix the problem (you must be logged into google account):

修复问题的链接(您必须登录到谷歌帐户):

  • view recent attempts to use the account and accept suspicious access.

    查看最近使用该帐户的尝试并接受可疑访问。

  • link to disable the feature of blocking suspicious apps/technologies:

    链接以禁用屏蔽可疑应用程序/技术的功能:

    https://www.google.com/settings/u/1/security/lesssecureapps

    https://www.google.com/settings/u/1/security/lesssecureapps

Note: You can go to the following * answer link for more detailed reference.

注意:您可以访问下面的*答案链接以获得更详细的参考。

https://*.com/a/25175234

https://*.com/a/25175234

#11


0  

If you are using cPanel you should just click the wee box that allows you to send to external servers by SMTP.

如果您正在使用cPanel,您应该单击允许您通过SMTP发送到外部服务器的wee框。

Login to CPanel > Tweak Settings > All> "Restrict outgoing SMTP to root, exim, and mailman (FKA SMTP Tweak)"

登录以限制CPanel >微调设置>所有>“输出SMTP到根、exim和mailman (FKA SMTP微调)”

As answered here:

作为回答:

"Password not accepted from server: 535 Incorrect authentication data" when sending with GMail and phpMailer

使用GMail和phpMailer发送时,“服务器不接受密码:535不正确的身份验证数据”

#12


0  

 $mail->SMTPOptions = array(
                'ssl' => array(
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                )
            );