PHPMailer本地工作但不在生产(Azure主机)

时间:2018-02-25 11:57:46

标签: php azure phpmailer

我正在尝试使用PHPMailer发送电子邮件,它在开发环境(本地 - 我的私有leptop)上工作正常,但在生产环境(Azure主机)上根本不起作用。

这是我正在使用的代码:

require 'PHPMailer-master/PHPMailerAutoload.php';

$mail = new PHPMailer(true); // 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 465-for gmail
$mail->CharSet = 'UTF-8';
$mail->IsHTML(false);
$mail->Username = '***@gmail.com';
$mail->Password = '*****';
$mail->SetFrom('****@gmail.com');
$mail->Subject =  'test phpmailer';
$mail->Body = 'body without html';
$mail->AddAddress('****@gmail.com');
if(!$mail->Send()) {    
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message has been sent";
}

我试图在他们的网站以及一般在线检查解决方案,但我发现没有任何帮助:Troubleshooting PHPMailer Problems

我尝试过的和我看到的内容:

关于CMD:

ping smtp.gmail.com

Pinging gmail-smtp-msa.l.google.com [108.177.126.108] with 32 bytes of data:
Reply from 108.177.126.108: bytes=32 time=5ms TTL=40
Reply from 108.177.126.108: bytes=32 time=5ms TTL=40
Reply from 108.177.126.108: bytes=32 time=5ms TTL=40
Reply from 108.177.126.108: bytes=32 time=5ms TTL=40

Ping statistics for 108.177.126.108:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 5ms, Maximum = 5ms, Average = 5ms

在CMD 2上:

telnet smtp.gmail.com 587
220 smtp.gmail.com ESMTP b11sm6804305edc.8 - gsmtp

这些结果并没有指出问题,所以我不知道如何解决这个问题,现在我已经陷入困境。

当我在生产中运行代码时,这就是我得到的:

2018-02-25 11:51:48 SMTP ERROR:无法连接服务器:(0)2018-02-25 11:51:48 SMTP connect()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

有人知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

经过大量工作并在网上搜索后,我建议那些使用Azure的人使用SendGrid等第三方服务发送电子邮件。

答案 1 :(得分:0)

为我工作。

如果安全域到SSL

添加

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

完整代码

$mail = new PHPMailer();
$mail->isSMTP();
$mail->CharSet = "utf-8";
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);
$mail->Username = "username@gmail.com";
$mail->Password = "password";
$mail->SetFrom('setFrom@gmail.com', 'xxx');
$mail->addReplyTo('replyTo@gmail.com', 'yyy');
$mail->addAddress('addAddress@gmail.com', 'addressName');
$mail->Subject = 'Subject Test';
$mail->msgHTML("Welcome to bamossza.");
$mail->AltBody = 'bamossza.com';
相关问题