Use SMTPS Protocol with PHPMailer

This section provides a tutorial example on how to send remote emails with PHPMailer using STMPS (SMTP Secure) protocol.

If your remote email server uses STMPS protocol, you need to add more settings on the PHPMailer instance.

For example, your remote email service provider gives you the following access information:

Host Name: smtp.example.com
Port: 587
Protocol: SMTPS (Use TLS/SSL)
Authentication: Password
User Name: herong@herongyang.com
Password: TopScret

Then you can use the following PHP script to send out emails.

<?php
#  PHPMailer-Send-Email-SMTPS.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->SMTPAuth = true;
$mail->SMTPSecure = 'TLS';
$mail->Host = "smtp.example.com";
$mail->Port = 587;
$mail->Username = 'herong@herongyang.com';
$mail->Password = 'TopScret';

try {
   $mail->setFrom('herong@herongyang.com', 'Herong Yang');
   $mail->addAddress('joe@herongyang.com', 'Joe Doe');
   $mail->Subject = 'PHPMailer Test - SMTPS Protocol';
   $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();
}
?>

If you run the above PHP script, you should these debugging messages:

heronng$ php PHPMailer-Send-Email-SMTPS.php
...	Connection: opening to smtp.herongyang.com:587, timeout=300
...	Connection: opened
...	SMTP INBOUND: "220 smtp.herongyang.com"
...	SERVER -> CLIENT: 220 smtp.herongyang.com
...	CLIENT -> SERVER: EHLO localhost
...	SMTP INBOUND: "250-smtp.herongyang.com"
...	SMTP INBOUND: "250-PIPELINING"
...	SMTP INBOUND: "250-SIZE 73400320"
...	SMTP INBOUND: "250-STARTTLS"
...	SMTP INBOUND: "250-AUTH LOGIN PLAIN"
...	SMTP INBOUND: "250-AUTH=LOGIN"
...	SMTP INBOUND: "250-MAILCOMPRESS"
...	SMTP INBOUND: "250 8BITMIME"
...	CLIENT -> SERVER: STARTTLS
...	SMTP INBOUND: "220 Ready to start TLS"
...	SERVER -> CLIENT: 220 Ready to start TLS
...	Auth method requested: UNSPECIFIED
...	Auth methods available on the server: LOGIN,PLAIN
...	Requested auth method not available:
...	Auth method selected: LOGIN
...	CLIENT -> SERVER: AUTH LOGIN
...	SMTP INBOUND: "334 ..."
...	SMTP INBOUND: "235 Authentication successful"
...	SERVER -> CLIENT: 235 Authentication successful
...	CLIENT -> SERVER: MAIL FROM:<herong.yang@herongyang.com>
...	SMTP INBOUND: "250 Ok"
...	SERVER -> CLIENT: 250 Ok
...	CLIENT -> SERVER: RCPT TO:<herong.yang@herongyang.com>
...	SMTP INBOUND: "250 Ok"
...	SERVER -> CLIENT: 250 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: .
...	SMTP INBOUND: "250 Ok: queued as"
...	SERVER -> CLIENT: 250 Ok: queued as
...	CLIENT -> SERVER: QUIT
...	SMTP INBOUND: "221 Bye"
...	SERVER -> CLIENT: 221 Bye
...	Connection: closed

Sometimes, you may get the following error message. The error message suggests that the certificate from the remote host failed to pass the validation. It might be using a self-signed certificate, which should be imported manually imported to your local computer.

...	Connection: opening to somehost.com:587, timeout=300, options=array()
...	Connection: opened
...	SMTP INBOUND: "220 somehost.com ESMTP Postfix"
...	SERVER -> CLIENT: 220 somehost.com ESMTP Postfix
...	CLIENT -> SERVER: EHLO local.herongyang.com
...	SMTP INBOUND: "250-somehost.com"
...
... CLIENT -> SERVER: STARTTLS
... SMTP INBOUND: "220 2.0.0 Ready to start TLS"
... SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
... Connection failed. Error #2: stream_socket_enable_crypto(): SSL
      operation failed with code 1. OpenSSL Error messages:
      rror:14090086:SSL routines: SSL3_GET_SERVER_CERTIFICATE:
      certificate verify failed...
... SMTP Error: Could not connect to SMTP host.
... CLIENT -> SERVER: QUIT
... SMTP INBOUND: "...??????\4O...
... SERVER -> CLIENT: ????B??dU??S9Ly...
... SMTP ERROR: QUIT command failed: ????B??dU??S9Ly...
... Connection: closed
... SMTP Error: Could not connect to SMTP host.

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.3

 PHP Script File Syntax

 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

 Arrays - Ordered Maps

 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

 Managing File Upload

 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

 PHP 5.6 and PHPMailer 5.2

 Install PHPMailer from Source Code

 Image and Picture Processing

 Managing ZIP Archive Files

 Managing PHP Engine and Modules on macOS

 Managing PHP Engine and Modules on CentOS

 Archived Tutorials

 References

 Full Version in PDF/EPUB