How to Build a Browser OCR Tool with Tesseract.js
Learn how to build a browser-based OCR app using Tesseract.js. Extract text from images directly in the browser with a simple client-side setup.
Suggested:
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.
Table of contents [Show]
php -v)crontab -e)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/phpmailerNow that your PHP script is ready, schedule it using cron.
Open the crontab editor:
crontab -e0 8 * * * /usr/bin/php /path/to/send_email.php >> /var/log/email_cron.log 2>&1Breakdown:
0 8 * * *: Runs at 8:00 AM every day/usr/bin/php: Path to PHP CLI/path/to/send_email.php: Absolute path to your PHP script>> /var/log/email_cron.log: Log output2>&1: Redirect stderr to stdoutphp /path/to/send_email.phptail -f /var/log/email_cron.log$body = file_get_contents("https://example.com/daily-summary");
$mail->Body = $body;
$recipients = ['user1@example.com', 'user2@example.com'];
foreach ($recipients as $email) {
$mail->clearAddresses();
$mail->addAddress($email);
$mail->send();
}
Store scheduling data in a DB and use one cron job to process all due emails.
| Problem | Solution |
|---|---|
| Script works manually but not via cron | Check PHP path, file permissions, and environment variables |
| Emails not sending | Enable PHPMailer debugging: $mail->SMTPDebug = 2; |
| No logs generated | Verify correct redirection in cron command |
Use env inside your cron job to replicate environment:
* * * * * env > /tmp/env.output$schedule->command('emails:send')->dailyAt('08:00');Then run the scheduler via cron:
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1Automating 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.
Happy coding! 🚀
Learn how to build a browser-based OCR app using Tesseract.js. Extract text from images directly in the browser with a simple client-side setup.
Step-by-step guide to creating a PHP backend that removes image backgrounds using Remove.bg API. Ideal for developers building automated image tools
Learn how to create a real-time PHP chat app using MySQL and JavaScript with user login, typing status, chat history deletion, and a modern, responsive UI.