邮件不使用SMTP通过SSL发送PHPMailer

时间:2013-08-30 14:26:10

标签: php email ssl smtp phpmailer

我正在尝试使用PHPMailer通过SMTP发送电子邮件,但到目前为止还没有运气。我已经阅读了许多SO问题,PHPMailer教程和论坛帖子,但仍然无法使其工作。我将记录尽可能多的失败尝试,因为我记得节省时间,但首先是我正在使用的代码:

<?php
    session_start();
    error_reporting(E_ALL);
    ini_set('display_errors','On');

    require('includes/class.phpmailer.php');
    include('includes/class.smtp.php');
    $mail = new PHPMailer(); 

    $name = $_POST["name"];
    $guests = $_POST["guests"];
    $time = $_POST["time"];

    $message = "<h1>".$name." has booked a table for ".$guests." at ".$time."</h1>";

    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->Host       = "ssl://smtp.gmail.com"; // SMTP server
    $mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "myEmail@gmail.com"; // SMTP account username
    $mail->Password   = "myPassword";        // SMTP account password
    $mail->SetFrom('myEmail@gmail.com', 'James Cushing');
    $mail->AddReplyTo("myEmail@gmail.com","James Cushing");
    $mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";
    $mail->MsgHTML($message)
    $address = "myOtherEmail@me.com";
    $mail->AddAddress($address, "James Cushing");

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

首先,当我运行此代码时,我得到两个不同的错误。在我的本地服务器上,我收到错误:
SMTP - &gt;错误:无法连接到服务器:操作超时(60)
以下发件人地址失败:myEmail@gmail.com:调用Mail()而未连接
邮件程序错误:以下发件人地址失败:myEmail@gmail.com:未连接称为Mail()

我在网络服务器上运行相同代码时遇到同样的错误,但第一行是:
SMTP - &gt;错误:无法连接到服务器:网络无法访问(101)

显然值得指出的是,我没有使用文字“myEmail@gmail.com”,但我已经用自己的电子邮件替换了这篇文章。

我尝试过的事情
  - 使用iCloud SMTP服务器
  - 使用不同的端口
  - 在我的php.ini文件中启用OpenSSL扩展   - 从各种PHPMailer示例复制代码
  - 使用Google的“DisplayUnlockCaptcha”系统启用连接
  - 发送到不同地址和从不同地址发送   - 从Username属性中删除“@ gmail.com”   - 其他一些我不记得的事情

这已经让我疯狂了大约一天,所以如果有人能解决它,他们就会成为英雄。

由于

5 个答案:

答案 0 :(得分:30)

$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "myemail@gmail.com";
$mail->Password = "**********";
$mail->Port = "465";

这是一种有效的配置。

尝试替换你拥有的东西

答案 1 :(得分:3)

不要在端口465上使用SSL,它自1998年以来一直被弃用,仅供未获得备忘录的Microsoft产品使用;改为使用端口587上的TLS:所以,下面的代码应该对你有用。

mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "smtp.gmail.com"; // SMTP server

$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "tls";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 587;                   // set the SMTP port for the 

答案 2 :(得分:2)

首先,将这些设置用于Google:

$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls"; //edited from tsl
$mail->Username = "myEmail";
$mail->Password = "myPassword";
$mail->Port = "587";

但是,你设置了什么防火墙?

如果您要过滤掉TCP端口465/995,可能还有587,那么您需要配置一些例外或将它们从规则列表中删除。

https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

答案 3 :(得分:1)

每当我的客户端计算机更改网络连接(例如,家庭与办公室网络)时,我就会遇到类似的SMTP故障,并且以某种方式重新启动网络服务(或重新启动计算机)可以解决我的问题。不确定这是否适用于您的情况,但以防万一。

sudo /etc/init.d/networking restart   # for ubuntu

答案 4 :(得分:0)

首先,Google创建了“使用安全性较低的帐户方法”功能:

https://myaccount.google.com/security

然后创建了另一个权限:

https://accounts.google.com/b/0/DisplayUnlockCaptcha

希望它有所帮助。