我正试图在我的PHP网站中实现一个mailer。
使用Composer安装的PHPMailer。我使用的是Linux操作系统上的Ubuntu 18.04 LTS。
页面加载需要很长时间,然后显示异常。
test.php
<?
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;
require 'vendor/phpmailer/phpmailer/src/Exception.php';
require 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
require 'vendor/phpmailer/phpmailer/src/SMTP.php';
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'mail.thenetwork.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'service@thenetwork.com'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTI$
//Recipients
$mail->setFrom('service@thenetwork.com', 'The PC Repair Network');
$mail->addAddress('myemail@gmail.com', ''); // Add a recipient
$mail->addReplyTo('biz@thenetwork.com', '');
// Attachments
// Content
$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';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
异常
2020-03-16 05:13:59 SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
SMTP服务器
mail.thenetwork.com forwards successfully to
arrow.mxrouting.net
SMTP Ports: 465 (SSL), 587 (STARTTLS), 25 (Non Encrypted)
Force SSL: True
我尝试过的:
允许在端口587上使用
sudo ufw allow out 587
进行出站连接使用测试SMTP服务器https://mxtoolbox.com/成功返回
使用基于Windows的"SMTP诊断工具"测试SMTP服务器,该工具成功发送了一封电子邮件
从挂在
Trying **ipaddress**...
上的服务器进行telnet测试
我有一种感觉,无论是什么原因导致它无法工作,也导致我的telnet命令无法解决。
编辑
在了解到Linode有一项政策,即在确保符合CAN-SPAM之前,关闭新客户的端口25、465和587后,我向他们开出了一张支持票。
根据Linode的用户协议,出站邮件端口默认被阻止。稍后会有一张支持票,我就可以开始跑步了。如果您遇到类似上述PHPMailer或SwiftMailer的问题(尤其是极端的页面加载时间(,请确认您的托管提供商没有阻止端口465、25和587。