You are browsing the unmaintained documentation for old Nette 2.1. 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:

$template = new Nette\Templating\FileTemplate('email.latte');
$template->registerFilter(new Nette\Latte\Engine);
$template->orderId = 123;

$mail = new Message;
$mail->setFrom('John <>')

A local variable $mail containing the Message object will be available in the template, so it's possible to set message parameters directly in the template.

File email.latte:

{var $mail->from = "John <>" }
	<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>

If you are sending e-mail in a Component or Presenter, you can create the template with $template = $this->createTemplate(); and you will be able to also generate links in the template. Always make them absolute, via {link //Presenter:action}

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;

We do not create a mailer manually in application, but we configure it and let DI container to pass object Nette\Mail\IMailer.


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