【问题标题】:Unable to connect to SMTP server无法连接到 SMTP 服务器
【发布时间】:2024-01-09 19:10:02
【问题描述】:

我有一个支持邮件的服务器,比如example.com。我配置了服务器并通过cpanel添加了MX记录,这样我就可以通过outlook.com接收和发送邮件,地址为myaddr@example.com。 MX 记录来自domains.live.com

现在我需要使用 PHP 使用 SMTP 以编程方式发送邮件。我使用以下脚本尝试了 PHPmailer。但它显示错误

Mailer Error: SMTP Connect() failed. 

(但我可以使用 myaddr@example.com 通过 outlook.com 发送和接收电子邮件)

$body             = $_POST['message'];

$to = "support@example.org";
$from = 'fromAddress@gmail.com';
$fName = 'first name';
$lName = 'last name';
$subject =  'my subject';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
  //  $body             = eregi_replace("[\]",'',$body);
$mail->Host       = "mail.example.org"; // SMTP server example
$mail->SMTPDebug  = 0;           // enables SMTP debug information (for testing)
$mail->SMTPAuth   = true;        // enable SMTP authentication
$mail->Port       = 25;          // set the SMTP port for the GMAIL server
$mail->Username   = "myaddr@example.org"; // SMTP account username example
$mail->Password   = "password";
$mail->SetFrom($from, $fName.' '.$lName);
$mail->Subject = $subject;
$mail->AddAddress($to, "Support Team");
$mail->MsgHTML($body);

if(!$mail->Send()) {
     echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

我该如何解决这个问题。

【问题讨论】:

    标签: php smtp phpmailer


    【解决方案1】:

    最后我只是通过替换下面的一些设置解决了这个问题,它起作用了:)。

        $mail->Host       = "smtp-mail.outlook.com"; // SMTP server example
        $mail->Port       = 587;  
        $mail->SMTPSecure = 'tls';
    

    【讨论】: