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

Sending E-mails

Almost every web application needs to send e-mails, whether it's newsletter or order confirmation. That's why Nette Framework provides necessary tools. This tutorial will show you how to:

  • create e-mail
  • send it
  • add attachments
  • use templates in e-mails
  • create links

Never send passwords or any other login credentials by e-mail.

Example of creating an e-mail using Nette\Mail\Message class:

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.

And sending:

use Nette\Mail\SendmailMailer;

$mailer = new SendmailMailer;

In addition to specifying recipient with addTo(), it's possible to specify recipient of copy with addCc() and recipient of blind copy: addBcc(). In all these methods, including setFrom(), we can specify addressee in three ways:

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

HTML content can be defined using setHtmlBody() method:

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

Embedded images can be inserted using $mail->addEmbeddedFile('background.gif'), but it is not necessary. Why? Because Nette Framework finds and inserts all files referenced in the HTML code automatically. This behavior can be supressed by adding FALSE as a second parameter of the setHtmlBody() method.

If a 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.


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/'));


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}

Custom mailer

Default mailer uses PHP function mail. If you need to send mail through a SMTP server, you can use SmtpMailer.

$mailer = new Nette\Mail\SmtpMailer(array(
	'host' => '',
	'username' => '',
	'password' => '*****',
	'secure' => 'ssl',

You can also create your own mailer – it's a class implementing Nette\Mail\IMailer interface.