即使在连接到gmail SMTP

时间:2015-05-01 18:43:06

标签: php gmail phpmailer

我正在尝试使用从localhost运行的PHPMailer从我的“gmail”帐户发送邮件。我在浏览器中收到这样的错误:

Connection: opening to ssl://smtp.gmail.com:587, timeout=300, options=array ()
SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed.
Mailer Error: SMTP connect() failed

用于发送邮件的php代码如下:

<?php
function Send_Mail($to,$subject,$body)
{
    //require 'phpmailer/class.phpmailer.php';
    require 'phpmailer/PHPMailerAutoload.php';
    require 'phpmailer/class.phpmailer.php';
    $from = "mysite.com";
    $mail = new PHPMailer();
    $mail->isSMTP();// use SMTP

    $mail->SMTPDebug = 4;
    $mail->Debugoutput = 'html';
    //// enable SMTP authentication
    $mail->SMTPSecure = 'ssl';
    $mail->Host = "smtp.gmail.com";// SMTP host
    $mail->Port = 587;// set the SMTP port
    $mail->SMTPAuth = true;
    $mail->Username = "mymail@gmail.com";  // SMTP  username
    $mail->Password   = "mypass";  // SMTP password
    $mail->setFrom('from@example.com', 'First Last');
    $mail->addReplyTo('replyto@example.com', 'First Last');
    $mail->addAddress('whoto@example.com', 'John Doe');
    $mail->Subject = 'PHPMailer GMail SMTP test';
    $mail->msgHTML("the body of the mail");
    $mail->AltBody = 'This is a plain-text message body';

    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
    echo "Message sent!";}
    //$mail->Subject    = $subject;
    //$mail->MsgHTML($body);
    //$address = $to;
   // $mail->AddAddress($address, $to);
    //$mail->Send(); 
}
?>

我尝试了很多来自tsl / ssl和port的不同排列的东西,但都是徒劳的。我还telnet smtp.gmail.com 587检查与gmailSMTP的连接,结果如下所示:

Trying 74.125.130.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP ml6sm5492964pdb.69 - gsmtp

我对PHP不熟悉,并且一直试图从过去6小时开始解决问题。有什么想法吗? 如果问题很愚蠢,请提前抱歉。

1 个答案:

答案 0 :(得分:0)

您应该将代码基于the provided gmail example。您正在尝试将SMTPSecure = 'ssl'Port = 587一起使用。这不会起作用:将tls与端口587一起使用。您还会混淆包含文件。也许你应该尝试阅读文档?即使是自述文件中的示例也能正确执行此操作。