沃梦达 / 编程问答 / php问题 / 正文

无法连接到 SMTP 服务器

Unable to connect to SMTP server(无法连接到 SMTP 服务器)

本文介绍了无法连接到 SMTP 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a server with mail support, say example.com. I configured the server and added MX records via cpanel, so that I can receive and send mails via outlook.com with address myaddr@example.com. The MX records are got from domains.live.com.

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

Now I need to send mail programmatically using PHP using SMTP. I tried PHPmailer using the following script. But it is showing the error

Mailer Error: SMTP Connect() failed. 

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

(But I can send and receive emails via outlook.com using myaddr@example.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;
}

我该如何解决这个问题.

How can I resolve the issue.

推荐答案

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

Finally I just solved the issue by replacing some of the settings as below and it worked :).

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

这篇关于无法连接到 SMTP 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:无法连接到 SMTP 服务器

基础教程推荐