FreeIntermediatePHP 8.0+v2.2

PHP Email Automation Script

A powerful email automation system with customizable HTML templates, queue-based sending, bounce and complaint handling, open and click tracking, subscriber management, and full SMTP integration.

Overview

Sending emails reliably at scale requires more than just calling the mail() function. This script provides a complete email infrastructure layer for your PHP application. Emails are composed from reusable HTML templates, placed into a database queue, and processed by a background worker that handles delivery, retries, and error logging.

The system integrates with any SMTP provider including Amazon SES, SendGrid, Mailgun, and standard mail servers. It tracks delivery status, open rates via tracking pixels, and click-through rates via link wrapping. A built-in subscriber management module handles opt-in, opt-out, and list segmentation for newsletter use cases.

Key Features

  • Customizable HTML email templates
  • Template variables with merge tags
  • Database-backed email queue
  • Background worker for batch processing
  • Automatic retry on temporary failures
  • Bounce and complaint webhook handlers
  • Open tracking with invisible pixel
  • Click tracking via link wrapping
  • Subscriber list management
  • One-click unsubscribe headers
  • DKIM signing support
  • Delivery reports and analytics dashboard

Code Example

Sending a templated email through the queue is simple and clean:

// Create an email from a template $mailer = new EmailService($config); $mailer->send( to: '[email protected]', template: 'welcome', data: [ 'name' => 'John Doe', 'login_url' => 'https://app.example.com/login', 'plan' => 'Professional', ] ); // Send a newsletter to a subscriber list $mailer->sendToList( listId: 'weekly-digest', template: 'newsletter', data: ['issue' => 42, 'date' => '2026-03-15'], ); // Process the queue (run via cron) // php cli/process-queue.php --batch=50

Template System

Email templates are stored as HTML files with merge tag placeholders like {{name}} and {{login_url}}. The template engine replaces these tags with actual data at send time. Templates support conditional blocks for showing or hiding sections based on data values, and loop blocks for rendering lists of items like order line items or activity summaries. A base layout template provides consistent header, footer, and branding across all emails.

Queue Processing

Instead of sending emails synchronously during web requests, this script queues them in a database table. A CLI worker script processes the queue in configurable batch sizes, typically triggered by a cron job every minute. This approach prevents slow page loads, handles temporary SMTP failures with automatic retries, and lets you control sending rate to stay within provider limits. Failed emails are retried up to three times with exponential backoff before being marked as permanently failed.

Tracking and Analytics

Every sent email can optionally include an invisible tracking pixel that records when the email is opened. Links within the email are wrapped through a redirect endpoint that logs clicks before forwarding to the destination URL. The analytics dashboard shows delivery rates, open rates, click-through rates, and bounce rates per template and per campaign. All tracking respects subscriber privacy preferences and can be disabled globally or per list.

Installation Steps

  1. Download and extract the script files to your web server
  2. Import database.sql to create the queue, subscribers, and tracking tables
  3. Edit config.php with your SMTP credentials and application URL
  4. Customize the email templates in the templates/ directory
  5. Set up a cron job to run php cli/process-queue.php every minute
  6. Configure bounce webhooks if your SMTP provider supports them

Server Requirements

RequirementMinimumRecommended
PHP Version8.08.2+
DatabaseMySQL 5.7MySQL 8.0+
PHP ExtensionsPDO, OpenSSL, mbstring+ curl for webhooks
Cron AccessRequiredEvery 1 minute
SMTP ProviderAny SMTP serverSES, SendGrid, Mailgun