Extra contribution to nette/mail (opens new window).

# Content

# Setup

composer require contributte/mail
1

# MailExtension

You have to manually register this extension in the first place.

Be careful, nette/mail is registered by default under the mail key, that's why we have picked the post key.

Simple example:

extensions:
    mail: Nette\Bridges\MailDI\MailExtension
    post: Contributte\Mail\DI\MailExtension

post:
    # Trace emails in Tracy
    trace: %debugMode%
1
2
3
4
5
6
7

There are several mailer implementations:

services:
    # Dump mails in folder
    mail.mailer: Contributte\Mail\Mailer\FileMailer(%tempDir%/mails)

    # Polished sendmail
    mail.mailer:
      class: Contributte\Mail\Mailer\SendmailMailer
      setup:
        - setBounceMail(mail@contributte.org)

    # Redirect all mails to one address
    mail.mailer: Contributte\Mail\Mailer\DevOpsMailer(dev@contributte.org)

    # Send mails to multiple mailers
    mail.mailer:
      class: Contributte\Mail\Mailer\CompositeMailer
      arguments: [silent: false] # If silent is enabled then exceptions from mailers are catched
      setup:
        - add(@mailer1)
        - add(@mailer2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# Mailers

# FileMailer

Stores emails on your file system.

$mailer = new FileMailer(__DIR__ . '/temp/mails');
1

# SendmailMailer

This is the default Nette\Mail\SendmailMailer with some extra methods and fields.

Bounce mail

$mailer->setBounceMail('mail@contributte.org');
1

Events

$mailer->onSend[] = function($mailer, $message) {};
1

# DevOpsMailer

Sends all emails to one address with preserved original attributes.

$mailer = new DevOpsMailer('dev@contributte.org');
1

# DevNullMailer

Does literally nothing.

$mailer = new DevNullMailer();
1

# CompositeMailer

Combines more mailers together.

$mailer = new CompositeMailer($silent = false); // If silent is enabled then exceptions from mailers are caught
$mailer->add(new FileMailer(__DIR__ . '/temp/mails'));
$mailer->add(new DevOpsMailer('dev@contributte.org'));
1
2
3

# TraceableMailer

Internally wraps your mailer and displays sent mails when the trace option is set to true in a Tracy panel.

# Message

# MessageFactory

DIC-generated Message factory

use Contributte\Mail\Message\IMessageFactory;

class Foo
{

    /** @var IMessageFactory @inject */
    public $messageFactory;

    public function sendMail(): void
    {
        $message = $this->messageFactory->create();
        // ...
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# Message::addTos(array $tos)

This wrapper accepts an array of recipients and calls addTo on each one of them.