You are browsing the unmaintained documentation for old Nette 2.2. See documentation for current Nette.

Sending E-mails

Are you going to send emails such as newsletters or order confirmations? Nette Framework provides the necessary tools with a very nice API. We will show:

  • how to create an email, including attachments
  • how to send it
  • how to combine emails and templates


composer require nette/mail

Creating Emails

Email is a Nette\Mail\Message object:

use Nette\Mail\Message;

$mail = new Message;
$mail->setFrom('John <>')
	->setSubject('Order Confirmation')
	->setBody("Hello, Your order has been accepted.");

All parameters must be encoded in UTF-8.

In addition to specifying recipients with the addTo() method, you can also specify the recipient of copy with addCc(), or the recipient of blind copy with addBcc(). All these methods, including setFrom(), accepts addressee in three ways:

$mail->setFrom('', 'John Doe');
$mail->setFrom('John Doe <>');

The body of the email in HTML is set by the setHtmlBody() method:

$mail->setHtmlBody('<b>Sample HTML</b> <img src="background.gif">');

If an HTML e-mail has no plain-text alternative, it will be automatically generated. And if it has no subject set, it will be taken from the <title> element.

Embedded Images

Embedded images can be inserted into an email using the $mail->addEmbeddedFile('background.gif') method, but this is not necessary. Why? Because the Nette Framework finds and inserts all files referenced in the HTML code automatically. This behavior can be turned off by specifying false as the second parameter of the setHtmlBody() method, in this parameter it is also possible to pass the absolute path to the image folder.


Adding attachments to the e-mail is simple. In order to attach a file, we use addAttachment() method:

// attaches to the e-mail

// attaches new example.txt file with "Hello John!" in it
$mail->addAttachment('example.txt', 'Hello John!');

// attaches renamed to
$mail->addAttachment('', file_get_contents('path/to/'));

Can be sending e-mails even easier?


The real power comes in combination with Latte templating system:

$latte = new Latte\Engine;
$params = array(
	'orderId' => 123,

$mail = new Message;
$mail->setFrom('John <>')
	->setHtmlBody($latte->renderToString('email.latte', $params));

File email.latte:

	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>Order Confirmation</title>
	body {
		background: url("background.png")

	<p>Your order number {$orderId} has been accepted.</p>

The Latte Engine itself doesn't provide {link} macro. If we want to use it though, we can use a presenter or a component to create the template for us by calling $template = $this->createTemplate()->setFile('email.latte') and adding it in setHtmlBody(). This way, we are able to create links in the template.

use Nette;

class MyPresenter
	public function actionFoo()
		$template = $this->createTemplate()->setFile('email.latte');
		$mail = new Message();

Another option is to register the macro ourselves. Because a presenter is needed for generating links, we pass it as a parameter to the template:

use Nette;
use Nette\Bridges\ApplicationLatte\UIMacros;

function createMessage(Nette\Application\Application $application)

	/** @var Nette\Application\IPresenter */
	$presenter = $application->getPresenter();

	$latte = new Latte\Engine;
	$params = array(
		'orderId' => 123,
		'_presenter' => $presenter, // because of {plink}
		'_control' => $presenter    // because of {link}

	UIMacros::install($latte->getCompiler()); // Kromě jiných zaregistruje makro link a plink

	// ...

In the template, link is created like in a normal template. We use absolute address, so the link always points to the correct domain:

<a href="{link //Presenter:action}">Odkaz</a>

Sending Emails

Mailer is class responsible for sending e-mails. It implements the Nette\Mail\IMailer interface and several ready-made mailers are available.

Example of use:

$mailer = new Nette\Mail\SendmailMailer;

The framework automatically adds a Nette\Mail\IMailer service based on configuration to the DI container, which you get by passing it using dependency injection.


The default mailer is SendmailMailer which uses PHP function mail.

If you want to set returnPath and the server still overwrites it, use $commandArgs:

$mailer->commandArgs = '';


To send mail via the SMTP server, use SmtpMailer.

$mailer = new Nette\Mail\SmtpMailer(array(
	'host' => '', //  if not set, php.ini settings will be used
	'username' => '',
	'password' => '*****',
	'secure' => 'ssl',

Apart from options above SmtpMailer also offers more customisation options:

  • port – If not set, will be guessed based on secure option: 465 for value ssl, 25 otherwise.
  • timeout – Timeout for SMTP connection
  • persistent – Use persistent connection

Related blog posts