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

# Content

# Setup

Install package

composer require contributte/mail
1

Register extension

extensions:
	# Native nette/mail
	mail: Nette\Bridges\MailDI\MailExtension

	# Our contributte/mail
	post: Contributte\Mail\DI\MailExtension
1
2
3
4
5
6

# Configuration

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
1
2
3

# Mailers

# FileMailer

Stores emails on your file system.

Configuration

services:
	# Dump mails in folder
	mail.mailer: Contributte\Mail\Mailer\FileMailer(%tempDir%/mails)
1
2
3
$mailer = new FileMailer(__DIR__ . '/temp/mails');
1

# SendmailMailer

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

Configuration

services:
	# Polished sendmail
	mail.mailer:
		class: Contributte\Mail\Mailer\SendmailMailer
		setup:
			- setBounceMail(mail@contributte.org)
1
2
3
4
5
6

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.

Configuration

services:
	# Redirect all mails to one address
	mail.mailer: Contributte\Mail\Mailer\DevOpsMailer(@originalMailer, dev@contributte.org)
1
2
3
$mailer = new DevOpsMailer($originalMailer, 'dev@contributte.org');
1

# DevNullMailer

Does literally nothing.

$mailer = new DevNullMailer();
1

# CompositeMailer

Combines more mailers together.

Configuration

services:
	# 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
$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.

post:
	# Trace emails in Tracy
	trace: %debugMode%
1
2
3

# Message

# MessageFactory

You can rely on IMessageFactory message factory for creating mail messages.

use Contributte\Mail\Message\IMessageFactory;

class Foo
{

	/** @inject */
	public IMessageFactory $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

Message extends Nette\Mail\Message and add more functions.

# $message->addTos(array $tos)

Accepts an array of recipients and calls addTo on each one of them.