Send Remote Emails with PHPMailer

This section provides a tutorial example on how to send remote emails with PHPMailer using STMP protocol.

If you don't have an email server running on your local host, you can use PHPMailer's isSMTP() option to use the SMTP protocol to deliver emails to a remote email server.

Here is a PHP script that delivers emails to a remote email server using the SMTP protocol without the Opportunistic TLS option.

<?php
#  PHPMailer-Send-Email-Remotely.php
#- Copyright (c) 2019-2023 HerongYang.com. All Rights Reserved.

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

$srcDir = "/home/herong/local/php/vendor/phpmailer/phpmailer/src/";
require "$srcDir/PHPMailer.php";
require "$srcDir/SMTP.php";
require "$srcDir/Exception.php";

$mail = new PHPMailer(true);
$mail->SMTPDebug = 4;
$mail->isSMTP();
$mail->SMTPAutoTLS = false; # default is "true"
$mail->Host = "192.168.1.25";

try {
   $mail->setFrom('joe@herongyang.com', 'Joe Doe');
   $mail->addAddress('herong@herongyang.com', 'Herong Yang');
   $mail->Subject = 'PHPMailer Test - Remote Delivery';
   $mail->Body = 'There is a test message from PHPMailer.';
   $mail->send();
} catch (Exception $e) {
   # PHPMailer exceptions
   echo $e->errorMessage();
} catch (\Exception $e) {
   # PHP exceptions
   echo $e->getMessage();
}
?>

Run the above PHP script, you will get the follow debugging messages.

herong$ php PHPMailer-Send-Email-Remotely.php
...	Connection: opening to 192.168.1.25:25, timeout=300, options=array()
...	Connection: opened
...	SMTP INBOUND: "220 mail.herongyang.com ESMTP Postfix"
...	SERVER -> CLIENT: 220 mail.herongyang.com ESMTP Postfix
...	CLIENT -> SERVER: EHLO mail.herongyang.com
...	SMTP INBOUND: "250-mail.herongyang.com"
...	SMTP INBOUND: "250-PIPELINING"
...	SMTP INBOUND: "250-SIZE 5111222333"
...	SMTP INBOUND: "250-VRFY"
...	SMTP INBOUND: "250-ETRN"
...	SMTP INBOUND: "250-STARTTLS"
...	SMTP INBOUND: "250-ENHANCEDSTATUSCODES"
...	SMTP INBOUND: "250-8BITMIME"
...	SMTP INBOUND: "250-DSN"
...	SMTP INBOUND: "250 SMTPUTF8"

...	CLIENT -> SERVER: MAIL FROM:<joe@herongyang.com>
...	SMTP INBOUND: "250 2.1.0 Ok"
...	SERVER -> CLIENT: 250 2.1.0 Ok
...	CLIENT -> SERVER: RCPT TO:<herong@herongyang.com>
...	SMTP INBOUND: "250 2.1.5 Ok"
...	SERVER -> CLIENT: 250 2.1.5 Ok
...	CLIENT -> SERVER: DATA
...	SMTP INBOUND: "354 End data with <CR><LF>.<CR><LF>"
...	SERVER -> CLIENT: 354 End data with <CR><LF>.<CR><LF>
...	CLIENT -> SERVER: Date: Sun, 13 Jun 2021 08:18:02 +0000
...	CLIENT -> SERVER: To: Herong Yang <herong@herongyang.com>
...	CLIENT -> SERVER: From: Joe Doe <joe@herongyang.com>
...	CLIENT -> SERVER: Subject: PHPMailer Test - Remote Delivery
...	CLIENT -> SERVER: Message-ID: <...@mail.herongyang.com>
...	CLIENT -> SERVER: X-Mailer: PHPMailer 6.4.1
...	CLIENT -> SERVER: MIME-Version: 1.0
...	CLIENT -> SERVER: Content-Type: text/plain; charset=iso-8859-1
...	CLIENT -> SERVER:
...	CLIENT -> SERVER: There is a test message from PHPMailer.
...	CLIENT -> SERVER:
...	CLIENT -> SERVER: .
...	SMTP INBOUND: "250 2.0.0 Ok: queued as ..."
...	SERVER -> CLIENT: 250 2.0.0 Ok: queued as ...
...	CLIENT -> SERVER: QUIT
...	SMTP INBOUND: "221 2.0.0 Bye"
...	SERVER -> CLIENT: 221 2.0.0 Bye
...	Connection: closed

Table of Contents

 About This Book

 Introduction to Email

 Postfix - Mail Transport Agent (MTA)

 SSL/TLS Secure Connections with Postfix Server

 Dovecot - IMAP and POP3 Server

 SSL/TLS Secure Connections with Dovecot Server

 Email Client Tools - Mail User Agents (MUA)

 Mozilla Thunderbird - Mail User Agents (MUA)

PHPMailer - PHP Package for Sending Emails

 Install PHPMailer on CentOS Systems

 Send Local Emails with PHPMailer

Send Remote Emails with PHPMailer

 Use SMTPS Protocol with PHPMailer

 PHP 5.6 and PHPMailer 5.2

 Install PHPMailer from Source Code

 Send Email Attachments with PHPMailer

 Send Email in HTML with PHPMailer

 Email Message Format and Headers

 References

 Full Version in PDF/EPUB