Search code examples
phpemailphpmailerimappop3

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


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

Solution

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