PHP Tutorials - Herong's Tutorial Examples - v5.17, by Herong Yang
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 2019 (c) 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@mail php]$ 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
Introduction and Installation of PHP
PHP Data Types and Data Literals
Variables, References, and Constants
Expressions, Operations and Type Conversions
Conditional Statements - "if" and "switch"
Loop Statements - "while", "for", and "do ... while"
Function Declaration, Arguments, and Return Values
Interface with Operating System
Introduction of Class and Object
Integrating PHP with Apache Web Server
Retrieving Information from HTTP Requests
Creating and Managing Sessions in PHP Scripts
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
MySQL Server Connection and Access Functions
Functions to Manage Directories, Files and Images
SOAP Extension Function and Calling Web Services
SOAP Server Functions and Examples
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
"mbstring" Extension and Non-ASCII Encoding Management
Managing Non-ASCII Character Strings with MySQL Servers
Parsing and Managing HTML Documents
►Configuring and Sending Out Emails
Using Local Windows System as a Mail Server
Sending Out Emails from PHP Scripts
Sending Out Emails on Linux Systems
Install PHPMailer on CentOS Systems
Send Local Emails with PHPMailer
►Send Remote Emails with PHPMailer
Use SMTPS Protocol with PHPMailer
Install PHPMailer from Source Code
Managing PHP Engine and Modules on macOS