当联系表格提交到我的网站时,我正在尝试从我的辅助电子邮件地址向我的主电子邮件地址发送一封电子邮件。我正在使用Swiftmailer来实现这一点。当我在计算机上的 XAMPP 服务器上脱机使用该代码时,该代码运行良好,但是当我尝试在运行 LAMP 堆栈的 Digital Ocean Droplet 上运行它时,这与我的脱机设置几乎相同,它不起作用并返回错误 500。
我调查了一下,结果发现,SMTP 被禁用以防止垃圾邮件,如果我联系支持,它可以被解除。在我联系他们之后,他们告诉我他们无法解除对端口 25 的限制,但告诉我尝试其他端口,并且 SMTP 不适用于浮动 IP。我删除了我的浮动 IP 来帮助解决这个问题,我目前正在尝试将端口 578 与 Hotmail 电子邮件地址一起使用,但它仍然无法正常工作。
到目前为止我已经尝试过:
我使用此命令打开了 Digital Ocean Support 建议的 587端口。
sudo iptables -A INPUT -p tcp --dport 587 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
sudo iptables -A OUTPUT -p tcp --sport 587 -m conntrack --ctstate ESTABLISHED -j ACCEPT
之后,我的服务器就可以在 587 端口连接到 smtp.office365.com,如下面的命令所示:
nc -vz smtp.office365.com 587
Connection to smtp.office365.com 587 port [tcp/submission] succeeded!
命令“ufw status”显示端口 587 在 ipv4 和 ipv6 上都是打开的:
To Action From
-- ------ ----
22/tcp LIMIT Anywhere
80/tcp ALLOW Anywhere
443/tcp ALLOW Anywhere
3306/tcp ALLOW Anywhere
587/tcp ALLOW Anywhere
22/tcp (v6) LIMIT Anywhere (v6)
80/tcp (v6) ALLOW Anywhere (v6)
443/tcp (v6) ALLOW Anywhere (v6)
3306/tcp (v6) ALLOW Anywhere (v6)
587/tcp (v6) ALLOW Anywhere (v6)
但是,当我尝试运行联系表单时,它最终无法在 droplet 上运行,而它确实可以离线运行,并且我按预期收到了电子邮件。
有什么我想念的吗?
我将在下面附上我的 swiftmailer 部分的代码,以防它有助于识别我似乎遗漏的代码中的任何潜在错误。
include_once 'private/pass.inc.php';
//These fields usually get the data using a a form using POST method and PHP but left with placeholders here for the sake of simplicity.
$name = 'name';
$email = 'email';
$phone = 'phone';
$subject = 'subject';
$message = 'message';
// Send Email
// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.office365.com', 587, 'tls'))
->setUsername($emailusername)
->setPassword($emailpassword);
// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);
function sendVerificationEmail($name, $email, $phone, $subject, $message)
{
global $mailer;
$sentfrom = "[email protected]";
$sentto = "[email protected]";
$body = 'Message From:' . $name . '<br>' . $email . '<br>' . $phone . '<br>' . $subject . '<br>' . $message;
// Create a message
$message = (new Swift_Message("A New Message From " . $name))
->setFrom([$sentfrom])
->setTo([$sentto])
->setBody($body, 'text/html');
// Send the message
$result = $mailer->send($message);
}
sendVerificationEmail($name, $email, $phone, $subject, $message);
echo "<script> window.location.assign('./contactformsuccess'); </script>";
编辑:添加日志
[Sun Aug 15 07:34:58.851255 2021] [php:warn] [pid 182629] [client 162.158.167.91:37848] PHP Warning: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:\nerror:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in /var/www/site.com/html/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php on line 94 [Sun Aug 15 07:34:58.851487 2021] [php:error] [pid 182629] [client 162.158.167.91:37848] PHP Fatal error: Uncaught Swift_TransportException: Unable to connect with TLS encryption in /var/www/site.com/html/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php:349\nStack trace:\n#0 /var/www/site.com/html/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(148): Swift_Transport_EsmtpTransport->doHeloCommand()\n#1 /var/www/site.com/html/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mailer.php(65): Swift_Transport_AbstractSmtpTransport->start()\n#2 /var/www/site.com/html/includes/cf2.inc.php(39): Swift_Mailer->send()\n#3 /var/www/site.com/html/includes/cf2.inc.php(42): sendVerificationEmail()\n#4 {main}\n thrown in /var/www/site.com/html/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php on line 349