首页 > 解决方案 > 无法从php发送邮件

问题描述

我们正在尝试从 bigrock 上的 PHP 程序发送邮件,邮件服务器在 Digital Ocean 上 但是邮件没有被发送 指定邮件服务器的方式有什么问题吗?

<?php
require 'Exception.php';
require 'PHPMailer.php';
//Load Composer's autoloader
require 'SMTP.php';
$mail = new PHPMailer\PHPMailer\PHPMailer(true); // Passing true enables     
exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.mandify.in'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'ABC@mandify.in'; // SMTP username
$mail->Password = 'XYZ'; // SMTP password
$mail->SMTPSecure = 'STARTTLS'; // Enable TLS encryption, ssl also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('ABC@mandify.in', 'Mailer');
$mail->addAddress('QWERTY@gmail.com', 'Joe User'); // Add a recipient
//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;
}
?>

同样的代码也为 bigrock 上的邮件服务器运行(与执行相同的服务器),但这也不起作用

这是错误消息:

连接失败。错误 #2:stream_socket_enable_crypto():SSL 操作失败,代码为 1。OpenSSL 错误消息:错误:14090086:SSL 例程:ssl3_get_server_certificate:证书验证失败

根据服务器管理团队的反馈,代码似乎有问题。

标签: php

解决方案


这可能是配置问题或缺少依赖项的指示。尝试避免证书验证失败的一件事是配置以下内容:

'ssl' => [
  'allow_self_signed' => true,
  'verify_peer' => false,
  'verify_peer_name' => false,
]

推荐阅读