2017 © Pedro Peláez
 

library mail

image

nette/mail

  • Monday, July 16, 2018
  • by david@grudl.com
  • Repository
  • 44 Watchers
  • 192 Stars
  • 1,479,894 Installations
  • PHP
  • 235 Dependents
  • 1 Suggesters
  • 56 Forks
  • 10 Open issues
  • 23 Versions
  • 6 % Grown

The README.md

Nette Mail, (*1)

Downloads this Month Tests Coverage Status Latest Stable Version License, (*2)

 , (*3)

Introduction

Are you going to send emails such as newsletters or order confirmations? Nette Framework provides the necessary tools with a very nice API., (*4)

Documentation can be found on the website., (*5)

 , (*6)

Support Me

Do you like Nette Mail? Are you looking forward to the new features?, (*7)

Buy me a coffee, (*8)

Thank you!, (*9)

 , (*10)

Installation

composer require nette/mail

It requires PHP version 8.0 and supports PHP up to 8.4., (*11)

 , (*12)

Creating Emails

Email is a Nette\Mail\Message object:, (*13)

$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., (*14)

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:, (*15)

$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:, (*16)

$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., (*17)

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:, (*18)

// 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 [[...]]., (*19)

Can sending emails be even easier?, (*20)

Emails are like postcards. Never send passwords or other credentials via email., (*21)

Attachments

You can, of course, attach attachments to email. Use the addAttachment(string $file, string $content = null, string $contentType = null)., (*22)

// 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?, (*23)

$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:, (*24)

<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., (*25)

 , (*26)

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., (*27)

SendmailMailer

The default mailer is SendmailMailer which uses PHP function mail(). Example of use:, (*28)

$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'., (*29)

SmtpMailer

To send mail via the SMTP server, use SmtpMailer., (*30)

$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:, (*31)

  • port - if not set, the default 25 or 465 for ssl will be used
  • timeout - timeout for SMTP connection
  • persistent - use persistent connection
  • clientHost - client designation
  • streamOptions - 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., (*32)

$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., (*33)

 , (*34)

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., (*35)

$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);

The Versions

16/07 2018

dev-master

9999999-dev https://nette.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

mail mime mailer nette smtp

16/07 2018

v2.4.x-dev

2.4.9999999.9999999-dev https://nette.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

mail mime mailer nette smtp

18/03 2018

v2.4.5

2.4.5.0 https://nette.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

mail mime mailer nette smtp

05/12 2017

v2.4.4

2.4.4.0 https://nette.org

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

mail mime mailer nette smtp

13/07 2017

v2.4.3

2.4.3.0 https://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

02/02 2017

v3.0.0-beta1

3.0.0.0-beta1 https://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

27/01 2017

v2.4.2

2.4.2.0 https://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

12/12 2016

v2.3.x-dev

2.3.9999999.9999999-dev https://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

31/07 2016

v2.4.1

2.4.1.0 https://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

25/06 2016

v2.4.0

2.4.0.0 https://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

10/04 2016

v2.3.5

2.3.5.0 https://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

06/02 2016

v2.2.x-dev

2.2.9999999.9999999-dev https://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

03/12 2015

v2.2.5

2.2.5.0 https://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

29/11 2015

v2.3.4

2.3.4.0 https://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

05/10 2015

v2.3.3

2.3.3.0 https://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

19/07 2015

v2.2.4

2.2.4.0 http://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

03/07 2015

v2.3.2

2.3.2.0 http://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

17/06 2015

v2.3.1

2.3.1.0 http://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

24/02 2015

v2.3.0

2.3.0.0 http://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

15/10 2014

v2.2.3

2.2.3.0 http://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

24/08 2014

v2.2.2

2.2.2.0 http://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

23/06 2014

v2.2.1

2.2.1.0 http://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires

11/05 2014

v2.2.0

2.2.0.0 http://nette.org

Nette Mail: Sending E-mails

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

 

The Development Requires