jdz/mailer

JDZ mailer

Maintainers

Package info

github.com/joffreydemetz/mailer

Homepage

pkg:composer/jdz/mailer

Statistics

Installs: 37

Dependents: 1

Suggesters: 1

Stars: 0

Open Issues: 0

1.0.9 2026-04-09 15:20 UTC

This package is auto-updated.

Last update: 2026-04-09 15:21:50 UTC


README

Flexible PHP mailer with SMTP (via PHPMailer), Mailchimp Transactional, and native mail() support. Automatic fallback between senders, HTML/plain-text content handling, DKIM signing, and attachments.

Installation

composer require jdz/mailer

Optional senders (install as needed):

composer require phpmailer/phpmailer        # SMTP / DKIM / Sendmail
composer require mailchimp/transactional     # Mailchimp Transactional API

Optional HTML-to-text converters for the alternative body:

composer require ph-7/html-to-text           # PH7 converter
composer require soundasleep/html2text       # Soundasleep converter

Requirements

  • PHP >= 8.2

Configuration

Copy .env.dist to .env and fill in your credentials:

cp .env.dist .env

The .env file is gitignored and never committed.

Usage

SMTP Email

use Symfony\Component\Dotenv\Dotenv;
use JDZ\Mailer\Mailer;
use JDZ\Mailer\AltBody\BasicAltBody;

(new Dotenv())->loadEnv(__DIR__ . '/.env');

$mail = new Mailer();

$mail->setProperties([
    'domain' => 'example.com',
    'language' => 'fr',
    'charset' => 'utf-8',
]);

$mail->setContent([
    'isHtml' => true,
    'content' => '<h1>Hello</h1><p>This is a test email.</p>',
    'template' => '<html><body>{{BODY}}</body></html>',
    'altBodyFormatter' => new BasicAltBody(),
]);

$mail->setSMTP([
    'host' => $_ENV['SMTP_HOST'],
    'port' => (int)$_ENV['SMTP_PORT'],
    'secure' => $_ENV['SMTP_SECURE'],
    'auth' => true,
    'user' => $_ENV['SMTP_USER'],
    'pass' => $_ENV['SMTP_PASS'],
]);

$mail->setFrom($_ENV['MAIL_FROM_EMAIL'], $_ENV['MAIL_FROM_NAME']);
$mail->addRecipient('recipient@example.com', 'Recipient');
$mail->set('subject', 'Hello from JDZ Mailer');

$mail->send();

Mailchimp Transactional

$mail = new Mailer();

$mail->setContent([
    'isHtml' => true,
    'content' => '<p>Newsletter content</p>',
    'altBodyFormatter' => new BasicAltBody(),
]);

$mail->setMailchimp([
    'apiKey' => $_ENV['MAILCHIMP_API_KEY'],
    'track_opens' => true,
    'track_clicks' => true,
]);

$mail->addRecipient('subscriber@example.com', 'Subscriber');
$mail->set('subject', 'Monthly Newsletter');
$mail->send();

Native mail()

If no SMTP or Mailchimp is configured, the mailer falls back to PHP's native mail() function:

$mail = new Mailer();
$mail->domain = 'example.com';
$mail->setFrom('sender@example.com', 'Sender');
$mail->addRecipient('recipient@example.com');
$mail->set('subject', 'Simple email');
$mail->content->setProperties([
    'content' => 'Plain text message',
    'altBodyFormatter' => new BasicAltBody(),
]);
$mail->send();

Fallback

Enable useFallback to automatically fall back to a simpler sender if the preferred one is unavailable:

$mail->useFallback = true;  // mailchimp -> smtp -> mail()

Recipients

$mail->addRecipient('to@example.com', 'To');
$mail->addCc('cc@example.com', 'CC');
$mail->addBcc('bcc@example.com', 'BCC');
$mail->addReplyTo('reply@example.com', 'Reply To');

// Or bulk:
$mail->addRecipients([
    ['email' => 'alice@example.com', 'name' => 'Alice'],
    ['email' => 'bob@example.com', 'name' => 'Bob'],
]);

// No-reply (prevents further addReplyTo calls):
$mail->setNoReply('noreply@example.com');

Attachments

$mail->addAttachment('/path/to/file.pdf', 'report.pdf', 'base64', 'application/pdf');

Content Replacements

$mail->setContent([
    'isHtml' => true,
    'content' => '<p>Hello {{NAME}}, your order #{{ORDER}} is ready.</p>',
    'replacements' => [
        '{{NAME}}' => 'John',
        '{{ORDER}}' => '12345',
    ],
    'altBodyFormatter' => new BasicAltBody(),
]);

Template-Only Mode

Use a full template without separate content:

$mail->setContent([
    'templateOnly' => true,
    'template' => '<html><body><h1>Full HTML template</h1></body></html>',
    'altBodyFormatter' => new BasicAltBody(),
]);

Alt Body Formatters

Three HTML-to-plain-text converters are available:

  • BasicAltBody - Built-in, uses strip_tags() (no extra dependency)
  • Ph7AltBody - Uses ph-7/html-to-text
  • SoundasleepAltBody - Uses soundasleep/html2text

Exceptions

  • ConfigException - Invalid configuration (missing fields, bad values)
  • SmtpException - SMTP send failure
  • MailchimpException - Mailchimp API failure
  • Exception - General mailer errors
try {
    $mail->send();
} catch (\JDZ\Mailer\Exception\ConfigException $e) {
    // Bad configuration
} catch (\JDZ\Mailer\Exception\SmtpException $e) {
    // SMTP error
} catch (\JDZ\Mailer\Exception\MailchimpException $e) {
    // Mailchimp error
} catch (\JDZ\Mailer\Exception\Exception $e) {
    // General error
}

Testing

composer test
# or
vendor/bin/phpunit

License

This project is licensed under the MIT License - see the LICENSE file for details.