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

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 presenters and services we don't create mailer manually, but we will require the service of type Nette\Mail\IMailer using DI.

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.

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

$mailer->commandArgs = '';


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 = [
    '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. LinkGenerator provides {link} and n:href macros in templates.

class MailSender
    /** @var Nette\Application\LinkGenerator */
    private $linkGenerator;

    /** @var Nette\Application\UI\ITemplateFactory */
    private $templateFactory;

    public function sendEmail()
        $template = $this->createTemplate();

        $template->name = $order->getName();
        $template->setFile(__DIR__ . '/email.latte');

        $mail = new Message;

    protected function createTemplate()
        $template = $this->templateFactory->createTemplate();
        $template->getLatte()->addProvider('uiControl', $this->linkGenerator);

        return $template;

In the template, link is created like in a normal template. All links create over LinkGenerator are absolute:

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


Mailer is class responsible for sending e-mails. You can also create your own mailer – it's a class implementing Nette\Mail\IMailer interface.


Default mailer is SendmailMailer which uses PHP function mail.


To send mail via the SMTP server, use SmtpMailer.

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


It does not send email, but sends them through a set of mailers. In the event that one mailer fails, it repeats the attempt at the next one. If the last one fails, it starts again from the first one.

$mailer = new Nette\Mail\FallbackMailer([

Other parameters in the constructor include the number of repeat and waiting time (in miliseconds).