Unable to connect to SMTP server

2019-03-03 19:36发布

问题:

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.

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. 

(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.

回答1:

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';