You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Almost every web application needs to send e-mails, whether newsletters or order confirmations. This library provides necessary tools.
14
+
Are you going to send emails such as newsletters or order confirmations? Nette Framework provides the necessary tools with a very nice API.
15
15
16
-
Documentation can be found on the [website](https://doc.nette.org/mailing).
16
+
Documentation can be found on the [website](https://doc.nette.org/mailing). If you like it, **[please make a donation now](https://github.com/sponsors/dg)**. Thank you!
17
17
18
-
If you like Nette, **[please make a donation now](https://nette.org/donate)**. Thank you!
18
+
Installation:
19
19
20
-
21
-
Installation
22
-
------------
23
-
24
-
The recommended way to install is via Composer:
25
-
26
-
```
20
+
```shell
27
21
composer require nette/mail
28
22
```
29
23
30
-
It requires PHP version 7.1 and supports PHP up to 7.4.
31
-
24
+
Creating Emails
25
+
===============
32
26
33
-
Usage
34
-
-----
35
-
36
-
Example of creating an e-mail using `Nette\Mail\Message` class:
27
+
Email is a [Nette\Mail\Message](https://api.nette.org/3.0/Nette/Mail/Message.html) object:
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:
49
+
50
+
```php
51
+
$mail->setHtmlBody('<p>Hello,</p><p>Your order has been accepted.</p>');
58
52
```
59
53
60
-
In addition to specifying recipient with `addTo()`, it's possible to specify recipient of copy with `addCc()` and recipient of blind copy: `addBcc()`.
61
-
In all these methods, including `setFrom()`, we can specifiy addressee in three ways:
54
+
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.
55
+
56
+
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:
// automatically adds /path/to/images/background.gif to the email
60
+
$mail->setHtmlBody(
61
+
'<b>Hello</b> <imgsrc="background.gif">',
62
+
'/path/to/images'
63
+
);
64
+
```
65
+
66
+
The image embedding algorithm supports the following patterns: `<img src=...>`, `<body background=...>`, `url(...)` inside the HTML attribute `style` and special syntax `[[...]]`.
67
+
68
+
Can sending emails be even easier?
69
+
70
+
Emails are like postcards. Never send passwords or other credentials via email.
71
+
72
+
73
+
74
+
Attachments
75
+
-----------
76
+
77
+
You can, of course, attach attachments to email. Use the `addAttachment(string $file, string $content = null, string $contentType = null)`.
78
+
79
+
```php
80
+
// inserts the file /path/to/example.zip into the email under the name example.zip
81
+
$mail->addAttachment('/path/to/example.zip');
82
+
83
+
// inserts the file /path/to/example.zip into the email under the name info.zip
<p>Your order number {$orderId} has been accepted.</p>
128
+
</body>
129
+
</html>
73
130
```
74
131
75
-
Embedded images can be inserted using `$mail->addEmbeddedFile('background.gif')`, but it is not necessary.
76
-
Why? Because Nette Framework finds and inserts all files referenced in the HTML code automatically.
77
-
This behavior can be supressed by adding `false` as a second parameter of the `setHtmlBody()` method.
132
+
Nette automatically inserts all images, sets the subject according to the `<title>` element, and generates text alternative for HTML body.
133
+
78
134
79
-
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.
80
135
81
-
Of course, it's possible to add attachments to the e-mail:
136
+
Sending Emails
137
+
==============
138
+
139
+
Mailer is class responsible for sending emails. It implements the [Nette\Mail\Mailer](https://api.nette.org/3.0/Nette/Mail/Mailer.html) interface and several ready-made mailers are available which we will introduce.
140
+
141
+
142
+
143
+
SendmailMailer
144
+
--------------
145
+
146
+
The default mailer is SendmailMailer which uses PHP function `mail()`. Example of use:
82
147
83
148
```php
84
-
$mail->addAttachment('example.zip');
149
+
$mailer = new Nette\Mail\SendmailMailer;
150
+
$mailer->send($mail);
85
151
```
86
152
87
-
Can e-mail sending be even easier?
153
+
If you want to set `returnPath` and the server still overwrites it, use `$mailer->commandArgs = '[email protected]'`.
88
154
89
155
90
-
Custom mailer
91
-
-------------
156
+
SmtpMailer
157
+
----------
92
158
93
-
Default mailer uses PHP function `mail`. If you need to send mail through a SMTP server, you can use `SmtpMailer`.
159
+
To send mail via the SMTP server, use `SmtpMailer`.
You can also create your own mailer - it's a class implementing Nette\Mail\Mailer interface.
171
+
If you do not specify `host`, the value from php.ini will be used. The following additional keys can be used in the options:
172
+
173
+
*`port` - if not set, the default 25 or 465 for `ssl` will be used
174
+
*`context` - allows you to set [SSL context options](https://www.php.net/manual/en/context.ssl.php) for connection
175
+
*`timeout` - timeout for SMTP connection
176
+
*`persistent` - use persistent connection
177
+
*`clientHost` - client designation
178
+
179
+
180
+
FallbackMailer
181
+
--------------
182
+
183
+
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.
184
+
185
+
```php
186
+
$mailer = new Nette\Mail\FallbackMailer([
187
+
$smtpMailer,
188
+
$backupSmtpMailer,
189
+
$sendmailMailer
190
+
]);
191
+
$mailer->send($mail);
192
+
```
193
+
194
+
Other parameters in the constructor include the number of repeat and waiting time in miliseconds.
195
+
196
+
197
+
DKIM
198
+
====
199
+
200
+
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.
201
+
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.
0 commit comments