Sending emails is a core feature in PHP applications, used for user registration, password resets, notifications, and marketing campaigns. While PHPβs built-in mail()
function exists, it lacks reliability and security. Thatβs why PHPMailer is the go-to library for sending emails in PHP.
π― In this guide, youβll learn:
β
Why PHPMailer is better than PHPβs mail()
β
How to install and configure PHPMailer
β
How to send emails using SMTP (Gmail, Outlook, or custom servers)
β
How to send HTML emails with attachments
β
Best security practices to prevent email abuse
By the end, youβll have a fully functional email-sending system ready for real-world use! π
1οΈβ£ Why Use PHPMailer Instead of PHPβs mail()
?
PHP provides a built-in function to send emails:
mail("recipient@example.com", "Subject", "Hello, world!");
π₯ But hereβs why mail()
is a bad idea:
β No authentication β Most mail servers block unauthenticated emails.
β Easily flagged as spam β Emails often end up in the spam folder.
β No HTML support β Limited to plain text emails.
β No debugging β Errors are hard to track.
β Why PHPMailer?
PHPMailer is a fully-featured email library that:
β
Supports SMTP authentication (Gmail, Outlook, etc.)
β
Sends HTML emails with rich formatting
β
Attaches files and images easily
β
Handles error debugging
2οΈβ£ Installing PHPMailer (Easy Setup)
1οΈβ£ Install via Composer (Recommended)
composer require phpmailer/phpmailer
π₯ Why Composer?
β
Automatically manages dependencies.
β
Keeps your project organized and up-to-date.
2οΈβ£ Manual Installation
1οΈβ£ Download PHPMailer from GitHub.
2οΈβ£ Extract and place the PHPMailer
folder in your project.
3οΈβ£ Include it in your PHP script:
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require 'PHPMailer/src/Exception.php';
3οΈβ£ Sending Emails with PHPMailer
Now, letβs send an email using Gmail SMTP.
1οΈβ£ Configure PHPMailer in send.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
// SMTP settings
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'your-email@gmail.com'; // Replace with your email
$mail->Password = 'your-app-password'; // Use an app password, NOT your Gmail password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Email headers
$mail->setFrom('your-email@gmail.com', 'Zero Dev');
$mail->addAddress('recipient@example.com', 'Recipient Name');
// Email content
$mail->isHTML(true);
$mail->Subject = 'Test Email from PHPMailer';
$mail->Body = '<h1>Hello, Zero Dev!</h1><p>This is a test email sent via PHPMailer.</p>';
// Send email
$mail->send();
echo 'Email sent successfully!';
} catch (Exception $e) {
echo "Error: {$mail->ErrorInfo}";
}
?>
π₯ Whatβs happening?
β
Configures SMTP with Gmail (smtp.gmail.com
)
β
Sends an HTML email
β
Handles errors with try...catch
4οΈβ£ Sending an Email with Attachments
Letβs send an email with a file attachment (e.g., a PDF).
$mail->addAttachment('files/report.pdf', 'MyReport.pdf'); // Attach a file
$mail->send();
π₯ Now you can send invoices, reports, and images!
5οΈβ£ Sending Bulk Emails (Multiple Recipients)
To send mass emails, add multiple recipients:
$mail->addAddress('user1@example.com');
$mail->addAddress('user2@example.com');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
$mail->send();
π₯ Best for:
β
Newsletters
β
Marketing campaigns
6οΈβ£ Using a Custom SMTP Server
Instead of Gmail, use your own mail server (e.g., SendGrid, Mailgun, Outlook).
$mail->Host = 'smtp.yourdomain.com';
$mail->Username = 'your@yourdomain.com';
$mail->Password = 'your-email-password';
$mail->Port = 465;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
π₯ Why use a custom SMTP?
β
Higher sending limits
β
Better email branding
7οΈβ£ Best Security Practices for Sending Emails
π¨ To prevent email abuse, follow these security tips:
β
Use App Passwords Instead of Real Passwords
For Gmail, enable 2FA and generate an App Password instead of using your real password.
β
Limit Email Sending Frequency
Prevent spam by restricting email sending per user per hour.
β
Enable DKIM & SPF Authentication
Use DKIM & SPF to verify emails and reduce spam flagging.
β
Use reply-to
for Better Email Communication
$mail->addReplyTo('support@yourdomain.com', 'Support Team');
8οΈβ£ Complete Email Sending Script
Hereβs the full working script that:
β
Sends an email with SMTP authentication
β
Supports attachments
β
Handles errors properly
π send_email.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
// SMTP Settings
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'your-email@gmail.com';
$mail->Password = 'your-app-password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Email Headers
$mail->setFrom('your-email@gmail.com', 'Zero Dev');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->addReplyTo('support@yourdomain.com', 'Support Team');
// Email Content
$mail->isHTML(true);
$mail->Subject = 'Secure Email with PHPMailer';
$mail->Body = '<h1>Hello, Zero Dev!</h1><p>This is a test email with PHPMailer.</p>';
// Attach a file
$mail->addAttachment('files/report.pdf', 'Report.pdf');
// Send email
$mail->send();
echo 'Email sent successfully!';
} catch (Exception $e) {
echo "Error: {$mail->ErrorInfo}";
}
?>
π₯ Now you can send secure and professional emails! π
π Final Thoughts
Now you can send emails like a pro using PHPMailer!
β
Use SMTP for authentication
β
Send HTML emails & attachments
β
Implement security best practices
π Next: Handle Background Jobs and Queues in PHP
Happy coding! ππ