使用SMTP不发送带有PHPMailer的邮件。

时间:2022-03-18 18:13:03

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:

我正在尝试用PHPMailer发送电子邮件到SMTP,但到目前为止没有运气。我已经经历了许多这样的问题,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!";
    }
?>

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

首先,当我运行这个代码时,我得到两个不同的错误。在我的本地服务器上,我得到了错误:SMTP ->错误:未能连接到服务器:运行超时(60)以下地址失败:myEmail@gmail.com:没有连接Mailer错误的邮件():以下地址失败:myEmail@gmail.com:没有连接的邮件()。

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)

在我的web服务器上运行相同的代码时,我得到了更多或更少的错误,但是第一行是:SMTP ->错误:连接到服务器失败:网络是不可访问的(101)

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.

很明显,我并不是在使用“myEmail@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

我尝试过的事情——使用iCloud SMTP服务器——使用一个不同的端口——在我的php中启用了OpenSSL扩展。使用谷歌的“DisplayUnlockCaptcha”系统,可以从不同的地址发送到不同的地址——从用户名属性中删除“@gmail.com”——还有一些我不记得的东西。

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

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

Thanks

谢谢

5 个解决方案

#1


27  

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

That is a working configuration.

这是一个工作配置。

try to replace what you have

试着替换你所拥有的。

#2


2  

Don't use SSL on port 465, it's been deprecated since 1998 and is only used by Microsoft products that didn't get the memo; use TLS on port 587 instead: So, the code below should work very well for you.

不要在端口465上使用SSL,它从1998年就被弃用了,只被微软的产品使用,但没有得到备忘录;在端口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 

#3


2  

Firstly, use these settings for 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";

But also, what firewall have you got set up?

还有,你设置了什么防火墙?

If you're filtering out TCP ports 465/995, and maybe 587, you'll need to configure some exceptions or take them off your rules list.

如果您正在过滤TCP端口465/995,或者587,您将需要配置一些异常或将它们从规则列表中删除。

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

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

#4


1  

I got a similar failure with SMTP whenever my client machine changes network connection (e.g., home vs. office network) and somehow restarting network service (or rebooting the machine) resolves the issue for me. Not sure if this would apply to your case, but just in case.

当我的客户端机器改变网络连接(例如,home vs. office网络)和重新启动网络服务(或重启机器)时,SMTP也会出现类似的故障。不确定这是否适用于你的情况,但以防万一。

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

#5


0  

First, Google created the "use less secure accounts method" function:

首先,谷歌创建了“use less secure accounts method”功能:

https://myaccount.google.com/security

https://myaccount.google.com/security

Then created the another permission:

然后创建了另一个权限:

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

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

Hope it helps.

希望它可以帮助。

#1


27  

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

That is a working configuration.

这是一个工作配置。

try to replace what you have

试着替换你所拥有的。

#2


2  

Don't use SSL on port 465, it's been deprecated since 1998 and is only used by Microsoft products that didn't get the memo; use TLS on port 587 instead: So, the code below should work very well for you.

不要在端口465上使用SSL,它从1998年就被弃用了,只被微软的产品使用,但没有得到备忘录;在端口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 

#3


2  

Firstly, use these settings for 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";

But also, what firewall have you got set up?

还有,你设置了什么防火墙?

If you're filtering out TCP ports 465/995, and maybe 587, you'll need to configure some exceptions or take them off your rules list.

如果您正在过滤TCP端口465/995,或者587,您将需要配置一些异常或将它们从规则列表中删除。

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

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

#4


1  

I got a similar failure with SMTP whenever my client machine changes network connection (e.g., home vs. office network) and somehow restarting network service (or rebooting the machine) resolves the issue for me. Not sure if this would apply to your case, but just in case.

当我的客户端机器改变网络连接(例如,home vs. office网络)和重新启动网络服务(或重启机器)时,SMTP也会出现类似的故障。不确定这是否适用于你的情况,但以防万一。

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

#5


0  

First, Google created the "use less secure accounts method" function:

首先,谷歌创建了“use less secure accounts method”功能:

https://myaccount.google.com/security

https://myaccount.google.com/security

Then created the another permission:

然后创建了另一个权限:

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

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

Hope it helps.

希望它可以帮助。