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

Moh*_*ian 75 php gmail smtp phpmailer

我想通过PHP邮件程序使用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!";
}
?>
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误

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

SMTP server error: SMTP AUTH is required for message submission on port 587
Run Code Online (Sandbox Code Playgroud)

我的域名是vatandesign.ir

and*_*eld 136

$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";
 }
Run Code Online (Sandbox Code Playgroud)

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

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

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

更新

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

$mail->SMTPSecure = 'tls';
Run Code Online (Sandbox Code Playgroud)

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

  • 更新的答案有进一步的建议.无论如何,代码绝对是好的.您可以查看这两个来源http://phpmailer.worxware.com/index.php?pg=exampleagmail和https://github.com/Synchro/PHPMailer/blob/master/examples/test_smtp_gmail_advanced.php (3认同)
  • 请注意,如果指定"ssl"(强烈建议使用),则PHP安装需要使用您喜欢的PHP扩展机制加载(或编译)"openssl"扩展(我碰巧使用OS X Macports,为此这是sudo端口安装php5-openssl) (2认同)

Eva*_*ler 27

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

我使用了PHPMailer示例gmail.phps文件中给出的EXACT代码.它在我使用MAMP时工作很简单,然后一旦我将其移动到我的个人服务器上,我就收到了SMTP连接错误.

我读过的所有Stack Overflow答案,以及PHPMailer的所有故障排除文档都表示这不是PHPMailer的问题.这是服务器端的设置问题.我尝试了不同的端口(587,465,25),我尝试了'SSL'和'TLS'加密.我检查了我的php.ini文件中是否启用了openssl.我检查了没有防火墙问题.一切都检查出来,但仍然没有.

解决方案是我必须删除这一行:

$mail->isSMTP();
Run Code Online (Sandbox Code Playgroud)

现在一切正常.我不知道为什么,但它确实有效.我的其余代码将从PHPMailer示例文件中复制并粘贴.

  • 此解决方案有效,因为它完全关闭了SMTP.我不推荐这种方法.我从来没有解决过这个问题,我现在转而使用Mandrill API. (6认同)

小智 8

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

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

然后设置$mail->Password为您的应用程序专用密码.


Pr *_*oko 5

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