0

I am trying to use php to send an email from a php file to an outlook exchange account. The account is imap or pop not SMTP.

I keep getting an error 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';
}
7
  • change host to $mail->Host = 'ssl://smtp.livemail.co.uk'; Commented Feb 1, 2021 at 16:05
  • Add $mail->SMTPDebug(); to show the actual error. Commented Feb 1, 2021 at 16:08
  • Does my answer work Commented Feb 1, 2021 at 16:10
  • @mikel Also double check your port as written mail setting port should be 587. Commented Feb 1, 2021 at 16:11
  • @MohdAlomar, the port should be 465 when using SSL. And 587 when using tls Commented Feb 1, 2021 at 16:12

2 Answers 2

1

You can try to use:

smtp.livemail.co.uk

instead of that.

And port 995 is for POP3. Try to use port 993.

But, since you are trying to send mail, you will have to use port 465

Or you can use port 587, and change the SMTPSecure to tls.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.