Current Location: Home> Latest Articles> How to Send HTML Emails with Attachments Using PHP and PHPMailer

How to Send HTML Emails with Attachments Using PHP and PHPMailer

M66 2025-06-12

How to Send HTML Emails with Attachments Using PHP and PHPMailer

In today’s work and personal life, email has become an indispensable communication tool. Sending HTML emails not only makes the content richer and more attractive but also allows for a variety of layouts and styles. PHPMailer is a popular PHP library that provides email sending functionality, with the ability to easily add attachments.

This article will show you how to use PHP and the PHPMailer library to send HTML emails with attachments. We will guide you through the steps of installing PHPMailer, configuring SMTP settings, writing email content, and sending the email with attachments.

Step 1: Install PHPMailer

First, you need to download the latest version of PHPMailer from the GitHub repository (https://github.com/PHPMailer/PHPMailer) and extract it into your project directory.

Step 2: Include PHPMailer Class File and Create Email Instance

At the beginning of your PHP file, use the `require_once` statement to include the PHPMailer class file, like this:

require_once 'PHPMailer/PHPMailerAutoload.php';

Next, create a PHPMailer instance and configure the SMTP parameters:

$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_email@example.com';
$mail->Password = 'your_email_password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;

In this code, we configure the SMTP server address, SMTP authentication username and password, encryption method, and port number. Be sure to modify these values according to your actual setup.

Step 3: Set Email Content

Next, we will set the sender, recipient, subject, and content of the email, like this:

$mail->setFrom('your_email@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Email Subject';
$mail->isHTML(true);
$mail->Body = '<h1>This is the HTML body of the email</h1>';

In this code, the `setFrom()` method sets the sender's email address and name, the `addAddress()` method sets the recipient's email address and name, `Subject` is the email subject, `isHTML(true)` specifies that the email content is in HTML format, and `Body` contains the HTML body of the email.

Step 4: Add Attachments

To add an attachment, use the `addAttachment()` method, like this:

$mail->addAttachment('path_to_attachment_file.pdf', 'Attachment Name');

The first parameter is the path to the attachment file, and the second parameter is the name of the attachment.

Step 5: Send the Email

Finally, use the `send()` method to send the email, like this:

if ($mail->send()) {
    echo 'Email sent successfully';
} else {
    echo 'Email sending failed';
}

Complete Example Code

Here’s the complete PHP code example:

require_once 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_email@example.com';
$mail->Password = 'your_email_password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;

$mail->setFrom('your_email@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Email Subject';
$mail->isHTML(true);
$mail->Body = '<h1>This is the HTML body of the email</h1>';
$mail->addAttachment('path_to_attachment_file.pdf', 'Attachment Name');

if ($mail->send()) {
    echo 'Email sent successfully';
} else {
    echo 'Email sending failed';
}

Conclusion

Through the steps above, you now know how to send HTML emails with attachments using PHP and PHPMailer. This method allows you to send rich and well-formatted emails with various attachments. We hope this guide helps you in sending more effective and professional emails!