当我尝试使用 phpMailer 从 php 发送电子邮件时,如何解决 SMTP 连接错误? POP/IMAP

How to solve error on SMTP connect when I try send email from php using phpMailer? POP/IMAP

我正在尝试使用 php 从 php 文件向 outlook 交换帐户发送电子邮件。该帐户是 imap 或 pop 而不是 SMTP。

我一直收到错误 SMTP connect() failed

<?php
// Start with PHPMailer class
use PHPMailer\PHPMailer\PHPMailer; 


// Base files 
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require 'PHPMailer/src/POP3.php';
$mail = new PHPMailer();
// configure an SMTP
$mail->isSMTP();
$mail->Host = 'exchange2019.livemail.co.uk';
$mail->SMTPAuth = true;
$mail->Username = 'email';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 995;

$mail->setFrom('email', 'Your Hotel');
$mail->addAddress('email', 'Me');
$mail->Subject = 'Thanks for choosing Our Hotel!';
// Set HTML 
$mail->isHTML(TRUE);
$mail->Body = '<html>Hi there, we are happy to <br>confirm your booking.</br> Please check the document in the attachment.</html>';
$mail->AltBody = 'Hi there, we are happy to confirm your booking. Please check the document in the attachment.';
// add attachment
$mail->addAttachment('//confirmations/yourbooking.pdf', 'yourbooking.pdf');
// send the message
if(!$mail->send()){
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

您可以尝试使用:

smtp.livemail.co.uk

而不是那样。

端口 995 用于 POP3。尝试使用端口 993.

但是,由于您正在尝试发送邮件,因此您将不得不使用端口 465

或者你可以使用587端口,把SMTPSecure改成tls

require_once("phpMailer/class.phpmailer.php");
require_once("phpMailer/PHPMailerAutoload.php");

$mail = new PHPMailer(true); 
        $mail->SMTPDebug = 2;

        $mail->SMTPSecure = "tsl";
        $mail->SMTPAuth   = true;
        $mail->Username   = '000@000';
        $mail->Password   = 'xxxxx';
        $mail_from        = "000@000";
        $subject          = "Hola";
        $body             = "email body";
        $mail_to          = "000@000";
        $mail->IsSMTP(); 
        try {
              $mail->Host= "smtp.office365.com";
              $mail->Port = "587";// ssl port :465, 
              $mail->Debugoutput = 'html';
              $mail->AddAddress($mail_to, "000@000");
              $mail->SetFrom($mail_from,'000@000'); 
              $mail->Subject = $subject;
              $mail->MsgHTML($body);
              $mail->Send();
             $emailreturn = 200;
            } catch (phpmailerException $e) {
              $emailreturn = $e->errorMessage();             
            } catch (Exception $e) {
             $emailreturn = $e->getMessage();
            }
echo $emailreturn;
2021-02-10 18:08:02 SERVER -> CLIENT: 220 MN2PR01CA0060.outlook.office365.com Microsoft ESMTP MAIL Service ready at Wed, 10 Feb 2021 18:08:01 +0000
2021-02-10 18:08:02 CLIENT -> SERVER: EHLO localhost
2021-02-10 18:08:21 SERVER -> CLIENT:
2021-02-10 18:08:21 SMTP ERROR: EHLO command failed:
2021-02-10 18:08:21 SMTP NOTICE: EOF caught while checking if connected
SMTP Error: Could not authenticate.
SMTP Error: Could not authenticate.
SMTP Error: Could not authenticate.