Email Tutorials - Herong's Tutorial Examples - v1.04, by Herong Yang
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.
<?php
# PHPMailer-Send-Email-Attachment.php
#- Copyright (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 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 - Send Attachments';
$mail->Body = 'Herong, 2 files are attached --Joe';
$mail->AddAttachment('/home/herong/tmp/invoice.pdf');
$mail->AddAttachment('/home/herong/tmp/tickets.pdf');
$mail->send();
} catch (Exception $e) {
# PHPMailer exceptions
echo $e->errorMessage();
} catch (\Exception $e) {
# PHP exceptions
echo $e->getMessage();
}
?>
Table of Contents
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
Install PHPMailer from Source Code
►Send Email Attachments with PHPMailer
Send Email in HTML with PHPMailer