Learn how to automate email sending in PHP using cron jobs with this advanced guide. Includes PHPMailer setup, scheduling scripts, logging, security tips, and real-world automation examples.
Automation is a cornerstone of modern web development, and one common task developers often automate is email sending — for reports, notifications, or marketing campaigns. In this guide, we’ll walk you through how to automate email sending using PHP and cron jobs on a UNIX-based system.
By the end of this article, you'll have a reliable system in place that sends scheduled emails using PHP scripts triggered by cron jobs.
Mail server configured (Sendmail, Postfix, or SMTP via PHPMailer)
Access to crontab (crontab -e)
Step 1: Create a PHP Script to Send Emails
Let’s begin by creating a PHP script that sends an email. We’ll use PHPMailer, a robust library that supports SMTP, HTML content, attachments, and more.
send_email.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php'; // Composer autoload
$mail = new PHPMailer(true);
try {
// SMTP server settings
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_email@example.com';
$mail->Password = 'your_password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Recipients
$mail->setFrom('your_email@example.com', 'Mailer');
$mail->addAddress('recipient@example.com', 'Recipient Name');
// Content
$mail->isHTML(true);
$mail->Subject = 'Automated Email';
$mail->Body = 'This is an automated email sent via a cron job.';
$mail->send();
echo "Email has been sent successfully\n";
} catch (Exception $e) {
echo "Email could not be sent. Mailer Error: {$mail->ErrorInfo}\n";
}
Install PHPMailer using Composer:
composer require phpmailer/phpmailer
Step 2: Schedule the PHP Script Using Cron
Now that your PHP script is ready, schedule it using cron.
Step-by-step:
Open the crontab editor:
crontab -e
Add a cron expression to execute the PHP script periodically.
Automating email sending in PHP using cron jobs is powerful and scalable — ideal for periodic notifications, reports, and campaigns. With robust libraries like PHPMailer and the precision of cron jobs, you can automate almost any email workflow.
Whether you're building a startup MVP or running backend services, adding this kind of automation will save you time and reduce human error.
Have questions or need help? Drop them in the comments.
Discover the innovative work in AI-generated blogs, seamlessly blending technology with creativity. This unique approach not only offers fresh perspectives on various topics but also ensures that content is engaging and relevant.
Learn how to build an advanced AJAX contact form with PHP that sends email without page reload. Includes modern UI design, form validation, and secure email handling using PHP's mail() function.
Learn how to remove .php, .html, and .htm extensions from URLs using .htaccess. Create clean, SEO-friendly URLs on Apache servers with our step-by-step guide.