邮件未通过SMTP使用SSL和PHPMailer发送 [英] Mail not sending with PHPMailer over SSL using SMTP

查看:240
本文介绍了邮件未通过SMTP使用SSL和PHPMailer发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use PHPMailer to send e-mails over SMTP but so far have had no luck. I've gone through a number of SO questions, PHPMailer tutorials and forum posts but still cannot get it to work. I'll document as many of my failed attempts as I can remember to save time, but firstly here is the code I am using:

<?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->错误:无法连接到服务器:操作超时(60)
以下发件人地址失败:myEmail@gmail.com:未连接就调用Mail()
Mailer错误:以下发件人"地址失败:myEmail@gmail.com:未连接就调用了Mail()

Firstly, when I run this code now I get two different errors. On my local server I get the error:
SMTP -> ERROR: Failed to connect to server: Operation timed out (60)
The following From address failed: myEmail@gmail.com : Called Mail() without being connected
Mailer Error: The following From address failed: myEmail@gmail.com : Called Mail() without being connected

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

I get moreorless the same error running the same code on my web server, but the first line is:
SMTP -> ERROR: Failed to connect to server: Network is unreachable (101)

很显然,值得指出的是,我并没有使用文字"myEmail@gmail.com",而是用我自己的电子邮件代替了这篇文章.

Obviously it's worth pointing out that I'm not using the literal "myEmail@gmail.com" but I've substituted my own email out for this post.

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

Things I've tried
- Using the iCloud SMTP server
- Using a different port
- Enabling the OpenSSL extension in my php.ini file
- Copying code from various PHPMailer examples
- Using Google's "DisplayUnlockCaptcha" system to enable connections
- Sending to and from different addresses - Removing the "@gmail.com" from the Username property - A number of other things I can't remember

这已经使我发疯了大约一天,所以如果有人能够解决,他们将成为英雄.

This has now been driving me mad for about a day, so if anyone can solve it they will be a hero.

谢谢

推荐答案

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

这是有效的配置.

尝试替换您拥有的物品

这篇关于邮件未通过SMTP使用SSL和PHPMailer发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆