nette / mail
📧 Nette Mail: A handy library for creating and sending emails in PHP.
Installs: 7 579 944
Dependents: 263
Suggesters: 1
Security: 0
Stars: 471
Watchers: 37
Forks: 71
Open Issues: 9
Requires
- php: 8.0 - 8.4
- ext-iconv: *
- nette/utils: ^4.0
Requires (Dev)
- nette/di: ^3.1 || ^4.0
- nette/tester: ^2.4
- phpstan/phpstan-nette: ^1.0
- tracy/tracy: ^2.8
Suggests
- ext-fileinfo: to detect type of attached files
- ext-openssl: to use Nette\Mail\DkimSigner
- dev-master / 4.0.x-dev
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- v3.1.x-dev
- v3.1.11
- v3.1.10
- v3.1.9
- v3.1.8
- v3.1.7
- v3.1.6
- v3.1.5
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.x-dev
- v3.0.1
- v3.0.0
- v2.4.x-dev
- v2.4.6
- v2.4.5
- v2.4.4
- v2.4.3
- v2.4.2
- v2.4.1
- v2.4.0
- v2.3.x-dev
- v2.3.5
- v2.3.4
- v2.3.3
- v2.3.2
- v2.3.1
- v2.3.0
- v2.2.x-dev
- v2.2.5
- v2.2.4
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
This package is auto-updated.
Last update: 2024-12-05 03:44:02 UTC
README
Â
Introduction
Are you going to send emails such as newsletters or order confirmations? Nette Framework provides the necessary tools with a very nice API.
Documentation can be found on the website.
Â
Support Me
Do you like Nette Mail? Are you looking forward to the new features?
Thank you!
Â
Installation
composer require nette/mail
It requires PHP version 8.0 and supports PHP up to 8.4.
Â
Creating Emails
Email is a Nette\Mail\Message object:
$mail = new Nette\Mail\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 an email written in HTML is passed using the setHtmlBody()
method:
$mail->setHtmlBody('<p>Hello,</p><p>Your order has been accepted.</p>');
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:
// automatically adds /path/to/images/background.gif to the email $mail->setHtmlBody( '<b>Hello</b> <img src="background.gif">', '/path/to/images', );
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)
.
// inserts the file /path/to/example.zip into the email under the name example.zip $mail->addAttachment('/path/to/example.zip'); // inserts the file /path/to/example.zip into the email under the name info.zip $mail->addAttachment('info.zip', file_get_contents('/path/to/example.zip')); // attaches new example.txt file contents "Hello John!" $mail->addAttachment('example.txt', 'Hello John!');
Templates
If you send HTML emails, it's a great idea to write them in the Latte template system. How to do it?
$latte = new Latte\Engine; $params = [ 'orderId' => 123, ]; $mail = new Nette\Mail\Message; $mail->setFrom('John <john@example.com>') ->addTo('jack@example.com') ->setHtmlBody( $latte->renderToString('/path/to/email.latte', $params), '/path/to/images', );
File email.latte
:
<html> <head> <meta 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>
Nette automatically inserts all images, sets the subject according to the <title>
element, and generates text alternative for HTML body.
Â
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.
SendmailMailer
The default mailer is SendmailMailer which uses PHP function mail()
. Example of use:
$mailer = new Nette\Mail\SendmailMailer; $mailer->send($mail);
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
.
$mailer = new Nette\Mail\SmtpMailer( host: 'smtp.gmail.com', username: 'franta@gmail.com', password: '*****', encryption: Nette\Mail\SmtpMailer::EncryptionSSL, ); $mailer->send($mail);
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.
$mailer = new Nette\Mail\FallbackMailer([ $smtpMailer, $backupSmtpMailer, $sendmailMailer, ]); $mailer->send($mail);
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.
$signer = new Nette\Mail\DkimSigner( domain: 'nette.org', selector: 'dkim', privateKey: file_get_contents('../dkim/dkim.key'), passPhrase: '****', ); $mailer = new Nette\Mail\SendmailMailer; // or SmtpMailer $mailer->setSigner($signer); $mailer->send($mail);