Send Email Attachments with PHPMailer

This section provides a tutorial example on how to send email attachments with the PHPMailer package.

If you want to send attachments with your email message, you can use PHPMailer's AddAttachment() method to add a given file as an attachment. You can call this method multiple times to attachment multiple files.

Here is a PHP script example that sends 2 attachments with an email message.

#  PHPMailer-Send-Email-Attachment.php
#- Copyright (c) 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->SMTPAutoTLS = false; # default is on
$mail->Host = "";

try {
   $mail->setFrom('', 'Joe Doe');
   $mail->addAddress('', 'Herong Yang');
   $mail->Subject = 'PHPMailer Test - Send Attachments';
   $mail->Body = 'Herong, 2 files are attached --Joe';


} 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


 Full Version in PDF/EPUB