Send Email in HTML with PHPMailer

This section provides a tutorial example on how to send email messages in HTML format with the PHPMailer package.

By default, PHPMailer will send email messages in plain text. If you want to send email messages in HTML format, you need to call isHTML(true) method to set the message content type to be "text/html".

Here is a PHP script example that sends an email message in HTML format.

<?php
#  PHPMailer-Send-Email-in-HTML.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 on
$mail->Host = "127.0.0.1";

try {
   $mail->setFrom('joe@herongyang.com', 'Joe Doe');
   $mail->addAddress('herong@herongyang.com', 'Herong Yang');
   $mail->Subject = 'PHPMailer Test - HTML Content';

   $mail->isHTML(true);
   $mail->Body = '<p>Herong, Happy New Year! --<i>Joe</i></p>';

   $mail->send();
} catch (Exception $e) {
   # PHPMailer exceptions
   echo $e->errorMessage();
} catch (\Exception $e) {
   # PHP exceptions
   echo $e->getMessage();
}
?>

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