Sending Emails
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
Download and install the package using Composer:
Creating Emails
Email is a Nette\Mail\Message object:
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:
The body of an email written in HTML is passed using the setHtmlBody()
method:
You don't have to create a text alternative, Nette will generate it automatically for you. And if the email does not have a
subject set, it will be taken from the <title>
element.
Images can also be extremely easily inserted into the HTML body of an email. Just pass the path where the images are physically located as the second parameter, and Nette will automatically include them in the email:
The image embedding algorithm supports the following patterns: <img src=...>
,
<body background=...>
, url(...)
inside the HTML attribute style
and special syntax
[[...]]
.
Can sending emails be even easier?
Emails are like postcards. Never send passwords or other credentials via email.
Attachments
You can, of course, attach attachments to email. Use the
addAttachment(string $file, ?string $content = null, ?string $contentType = null)
.
Templates
If you send HTML emails, it's a great idea to write them in the Latte template system. How to do it?
File email.latte
:
Nette automatically inserts all images, sets the subject according to the <title>
element, and generates
text alternative for HTML body.
Using in Nette Application
If you use e-mails together with Nette Application, ie presenters, you may want to create links in templates using the
n:href
attribute or the {link}
tag. Latte basically does not know them, but it's very easy to add them.
Creating links can do object Nette\Application\LinkGenerator
, which you get by passing it using dependency injection.
In the template, link is created like in a normal template. All links create over LinkGenerator are absolute:
Sending Emails
Mailer is class responsible for sending emails. It implements the Nette\Mail\Mailer interface and several ready-made mailers are available which we will introduce.
The framework automatically adds a Nette\Mail\Mailer
service based on configuration
to the DI container, which you get by passing it using dependency
injection.
SendmailMailer
The default mailer is SendmailMailer which uses PHP function mail. Example of use:
If you want to set returnPath
and the server still overwrites it, use
$mailer->commandArgs = '-fmy@email.com'
.
SmtpMailer
To send mail via the SMTP server, use SmtpMailer
.
The following additional parameters can be passed to the constructor:
port
– if not set, the default 25 or 465 forssl
will be usedtimeout
– timeout for SMTP connectionpersistent
– use persistent connectionclientHost
– client designationstreamOptions
– allows you to set SSL context options for connection
FallbackMailer
It does not send email but sends them through a set of mailers. If one mailer fails, it repeats the attempt at the next one. If the last one fails, it starts again from the first one.
Other parameters in the constructor include the number of repeat and waiting time in milliseconds.
DKIM
DKIM (DomainKeys Identified Mail) is a trustworthy email technology that also helps detect spoofed messages. The sent message is signed with the private key of the sender's domain and this signature is stored in the email header. The recipient's server compares this signature with the public key stored in the domain's DNS records. By matching the signature, it is shown that the email actually originated from the sender's domain and that the message was not modified during the transmission of the message.
You can set up mailer to sign email in configuration. If you do not use dependency injection, it is used as follows:
Configuring
Overview of configuration options for the Nette Mail. If you are not using the whole framework, but only this library, read how to load the configuration.
By default, the mailer Nette\Mail\SendmailMailer
is used to send emails, which is not further configured. However,
we can switch it to Nette\Mail\SmtpMailer
:
You can disable SSL certificate authentication using the context › ssl › verify_peer: false
option. It is
strongly recommended not to do this as it will make the application vulnerable. Instead, add certificates to trust store.
To increase trustfulness, we can sign emails using DKIM technology:
DI Services
These services are added to the DI container:
Name | Type | Description |
---|---|---|
mail.mailer |
Nette\Mail\Mailer | email sending class |
mail.signer |
Nette\Mail\Signer | DKIM signing |