PHPMailer 不工作:有没有新的方法可以做到这一点?

PHPMailer not working: Is there a new way of doing this?

我遵循了这个问题中的所有说明:

SMTP connect() failed PHPmailer - PHP

但我仍然没有成功地让 PHPMailer 工作。我在别处搜索 - 没有解决方案。

您可以在此处查看结果:https://unidrones.co.za/JuneSecond

当我尝试使用我的 gmail 帐户凭据发送测试电子邮件时,returns 出现 "SMTP connect() failed" 错误。

我正在使用这个模板代码:

<?php
require 'PHPMailerAutoload.php';
if(isset($_POST['send']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;
$mail->addAddress($to_id);
$mail->Subject = $subject;
$mail->msgHTML($message);
if (!$mail->send()) {
$error = "Mailer Error: " . $mail->ErrorInfo;
echo '<p id="para">'.$error.'</p>';
}
else {
echo '<p id="para">Message sent!</p>';
}
}
else{
echo '<p id="para">Please enter valid data</p>';
}
?>

编辑:我不知道现在是否有通过 PHP 发送电子邮件的新方法。我使用的所有教程和课程都是以这种方式教授的(即使用 PHPMailer 库)。

我很难找到包含 PHPMailerAutoload.php 文件的 PHPMailer 库,这让我觉得它有点过时或已弃用,但我还能如何发送电子邮件?我不知道。

您是否尝试使用以下配置?

$mail->Host       = 'smtp.gmail.com';
$mail->SMTPSecure = 'ssl';
$mail->Port       = 465;
$mail->CharSet    = 'UTF-8';
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;

//$mail->SMTPDebug = 4;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'your gamil id';                 // SMTP username
$mail->Password = 'gmail password';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('set from address', 'name');
$mail->addAddress('recipient address', 'Joe User');     // Add a recipient

$mail->addReplyTo('reply address', 'Information');


// $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

您很难找到 PHPMailerAutoload.php 的原因是它太旧了,不再受支持。 Get the latest and base your code on the gmail example provided. If you're new to PHP, learn to use composer.

这三行是冲突的:

$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';

Host 中的 ssl:// 覆盖了 SMTPSecure 中的 tls,导致它尝试对需要显式 TLS 的端口使用隐式 TLS。使用端口 465 的 ssl 或端口 587 的 tls,而不是其他组合。不管怎样,看来这不是你的问题。

正如 the troubleshooting guide 所说的那样 "SMTP connect() failed" 错误:

This is often reported as a PHPMailer problem, but it's almost always down to local DNS failure, firewall blocking (for example as GoDaddy does) or another issue on your local network. It means that PHPMailer is unable to contact the SMTP server you have specified in the Host property, but doesn't say exactly why.

然后继续介绍几种技术,您可以使用这些技术来尝试准确诊断无法连接的原因。很棒的东西,文档。

我用一些随机数据尝试了您的表单,发现您未能在问题中包含最重要的错误消息:

2018-06-02 14:47:25 SMTP ERROR: Failed to connect to server: Network is unreachable (101)
2018-06-02 14:47:25 SMTP connect() failed

这表明您的 ISP 可能阻止了出站 SMTP,因此您得到了答案 - 也许确认使用指南建议的步骤(telnet 等),并参考您的 ISP 的文档或支持。

您还有一个重大遗漏 - 您没有设置 "from" 地址:

$mail->setFrom('myname@gmail.com', 'My Name');

请注意,如果您通过 gmail 发送,则只能使用您帐户的地址或预设别名(在 gmail 首选项中设置),而不是任意地址。

与此同时,无论如何,这都是一件有点疯狂的事情 - 为什么有人会在这样的表格上输入他们的 gmail 凭据?