Current Location: Home> Latest Articles> PHP and PHPMailer: A Complete Guide to Automatically Sending Emails via Code

PHP and PHPMailer: A Complete Guide to Automatically Sending Emails via Code

M66 2025-06-16

PHP and PHPMailer: A Complete Guide to Automatically Sending Emails via Code

Email has become an essential part of daily communication, and many applications require the ability to send emails automatically. PHP, a widely used programming language, combined with the PHPMailer library, greatly simplifies the process of sending emails. In this article, we'll walk you through how to use PHP and PHPMailer to send emails automatically, and provide clear code examples.

1. Installing PHPMailer

Before we begin, you'll first need to install PHPMailer. You can download the latest version from PHPMailer's GitHub page and integrate it into your project. After downloading, unzip the file and place the PHPMailer folder in the root directory of your project.

2. Sending Basic Emails with PHPMailer

Next, we'll show how to send a simple email using PHPMailer. Here's an example of sending a basic email:

<?php
require 'path/to/PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.example.com';  // Set SMTP server address
$mail->SMTPAuth = true;           // Enable SMTP authentication
$mail->Username = 'your-email@example.com'; // SMTP username
$mail->Password = 'your-password';         // SMTP password
$mail->SMTPSecure = 'ssl';        // Use SSL encryption
$mail->Port = 465;                // SMTP port number

$mail->setFrom('from@example.com', 'Sender Name');  // Sender's email address and name
$mail->addAddress('to@example.com', 'Recipient Name');  // Recipient's email address and name
$mail->addReplyTo('reply-to@example.com', 'Reply-to Name'); // Reply-to email address and name
$mail->Subject = 'Subject Line';  // Email subject
$mail->Body    = 'Email Body';    // Email body

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
?>

In the above code, we first load the PHPMailer class and create a PHPMailer instance. Then, we configure the necessary SMTP information, including server address, username, password, and encryption method. Next, we set the sender, recipient, subject, and body content, and finally send the email using the $mail->send() method.

3. Sending Emails with Attachments

In addition to sending basic emails, PHPMailer also supports sending emails with attachments. Here's an example of how to send an email with an attachment:

<?php
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your-email@example.com';
$mail->Password = 'your-password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;

$mail->setFrom('from@example.com', 'Sender Name');
$mail->addAddress('to@example.com', 'Recipient Name');
$mail->addReplyTo('reply-to@example.com', 'Reply-to Name');
$mail->Subject = 'Subject Line';
$mail->Body    = 'Email Body';

$mail->addAttachment('path/to/file');  // Add an attachment

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
?>

In this code, we use the $mail->addAttachment() method to attach a file. You can pass the file path to this method as the parameter.

Conclusion

With PHP and PHPMailer, it's easy to automatically send emails via code. This article covered how to use PHPMailer to send both basic and email attachments, with complete code examples. We hope these examples help you implement email sending functionality in your projects.

When using this functionality in practice, make sure to use valid SMTP server addresses, usernames, and passwords. Additionally, it's important to be aware of potential risks like malicious scripts that could misuse this feature to send spam emails.