Nette Documentation Preview

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

Installation:

composer require nette/mail

Creating Emails

Email is a Nette\Mail\Message object:

use Nette\Mail\Message;

$mail = new Message;
$mail->setFrom('John <john@example.com>')
	->addTo('peter@example.com')
	->addTo('jack@example.com')
	->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@example.com');
$mail->setFrom('john.doe@example.com', 'John Doe');
$mail->setFrom('John Doe <john.doe@example.com>');

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.

Attachments

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

// attaches example.zip to the e-mail
$mail->addAttachment('path/to/example.zip');

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

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

Can be sending e-mails even easier?

Templates

The real power comes in combination with Latte templating system:

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

$mail = new Message;
$mail->setFrom('John <john@example.com>')
	->addTo('jack@example.com')
	->setHtmlBody($template);

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 <john@example.com>" }
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>Order Confirmation</title>
	<style>
	body {
		background: url("background.png")
	}
	</style>
</head>
<body>
	<p>Hello,</p>

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

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;
$mailer->send($mail);

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

SendmailMailer

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 = '-fmy@email.com';

SmtpMailer

To send mail via the SMTP server, use SmtpMailer.

$mailer = new Nette\Mail\SmtpMailer(array(
	'host' => 'smtp.gmail.com', //  if not set, php.ini settings will be used
	'username' => 'franta@gmail.com',
	'password' => '*****',
	'secure' => 'ssl',
));
$mailer->send($mail);

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