邮件未使用 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()
邮件程序错误:以下发件人地址失败: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
在我的网络服务器上运行相同的代码时,我会遇到同样的错误,但第一行是:
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";
这是一个有效的配置.
尝试替换你所拥有的
相关文章