PHPackages                             jdz/mailer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. jdz/mailer

ActiveLibrary[Mail &amp; Notifications](/categories/mail)

jdz/mailer
==========

JDZ mailer

1.0.9(2mo ago)0491MITPHPPHP &gt;=8.2

Since Dec 13Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/joffreydemetz/mailer)[ Packagist](https://packagist.org/packages/jdz/mailer)[ Docs](https://jdz.joffreydemetz.com/mailer)[ RSS](/packages/jdz-mailer/feed)WikiDiscussions main Synced today

READMEChangelog (9)Dependencies (10)Versions (10)Used By (1)

Mailer
======

[](#mailer)

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
------------

[](#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
------------

[](#requirements)

- PHP &gt;= 8.2

Configuration
-------------

[](#configuration)

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

```
cp .env.dist .env
```

The `.env` file is gitignored and never committed.

Usage
-----

[](#usage)

### SMTP Email

[](#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' => 'HelloThis is a test email.',
    'template' => '{{BODY}}',
    '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

[](#mailchimp-transactional)

```
$mail = new Mailer();

$mail->setContent([
    'isHtml' => true,
    'content' => 'Newsletter content',
    '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()

[](#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

[](#fallback)

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

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

### Recipients

[](#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

[](#attachments)

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

### Content Replacements

[](#content-replacements)

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

### Template-Only Mode

[](#template-only-mode)

Use a full template without separate content:

```
$mail->setContent([
    'templateOnly' => true,
    'template' => 'Full HTML template',
    'altBodyFormatter' => new BasicAltBody(),
]);
```

Alt Body Formatters
-------------------

[](#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
----------

[](#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
-------

[](#testing)

```
composer test
# or
vendor/bin/phpunit
```

License
-------

[](#license)

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

###  Health Score

42

—

FairBetter than 88% of packages

Maintenance83

Actively maintained with recent releases

Popularity8

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~60 days

Recently: every ~82 days

Total

9

Last Release

85d ago

PHP version history (2 changes)1.0.1PHP &gt;=8.1

1.0.5PHP &gt;=8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/5e83e3701566e43438525ed14578487e732b849d152b5071aa1613a0dad96913?d=identicon)[jdz](/maintainers/jdz)

---

Top Contributors

[![joffreydemetz](https://avatars.githubusercontent.com/u/15113527?v=4)](https://github.com/joffreydemetz "joffreydemetz (15 commits)")

---

Tags

mailerservicesutilitiesJDZ

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jdz-mailer/health.svg)

```
[![Health](https://phpackages.com/badges/jdz-mailer/health.svg)](https://phpackages.com/packages/jdz-mailer)
```

###  Alternatives

[nette/mail

📧 Nette Mail: A handy library for creating and sending emails in PHP.

54010.2M282](/packages/nette-mail)[fedeisas/laravel-mail-css-inliner

Inline the CSS of your HTML emails using Laravel

6084.8M3](/packages/fedeisas-laravel-mail-css-inliner)[aplus/email

Aplus Framework Email Library

2561.6M3](/packages/aplus-email)[sylius/mailer-bundle

Mailers and e-mail template management for Symfony projects.

728.7M90](/packages/sylius-mailer-bundle)[voku/bounce-mail-handler

Bounce Mail Handler

50246.0k2](/packages/voku-bounce-mail-handler)[nickcv/yii2-mandrill

Mandrill Api Integration for Yii2

29576.8k2](/packages/nickcv-yii2-mandrill)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
