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

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

这是我的代码

<?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!"; } ?> 

但我收到以下错误

 Mailer Error: SMTP Error: The following recipients failed: anotherValidGmail@gmail.com SMTP server error: SMTP AUTH is required for message submission on port 587 

我的域名是vatandesign.ir

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

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

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

另外,请确保您没有为该帐户启用两步验证功能,因为这也会导致问题。

更新

你可以尝试改变$ mail-> SMTP来:

 $mail->SMTPSecure = 'tls'; 

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

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

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

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

解决办法是我必须删除这一行:

 $mail->isSMTP(); 

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

另外值得注意的是,如果您启用了两个因子认证,则需要设置一个特定于应用程序的密码来代替您的电子邮件帐户的密码。

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

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

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

打开这个链接,并选择按照说明谷歌服务器阻止未知服务器的任何企图,所以一旦你点击验证码检查每一件事情都会好起来的

不能评论,但是,删除

 $mail->isSMTP(); 

你会没事的!

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

在进行电话验证之前,我无法使用SMTP。 做了另一个帐户,仔细检查,我能够确认它。

我认为这是连接问题,你可以在这里得到代码http://skillrow.com/sending-mail-using-smtp-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; } 

这段代码对我来说工作得很好

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

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

我必须设置

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

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

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

解决问题的链接(您必须登录到Google帐户):

注意:您可以转到下面的计算器答案链接以获取更详细的参考信息。

https://stackoverflow.com/a/25175234

如果您使用的是cPanel,则只需单击允许您通过SMTP发送到外部服务器的wee box。

登录到CPanel>调整设置>全部>“限制传出SMTP到根,exim和mailman(FKA SMTP Tweak)”

这里回答如下:

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