Skip to content
forked from nette/mail

📧 Handy email creation and transfer library for PHP with both text and MIME-compliant support.

License

Notifications You must be signed in to change notification settings

karstennilsen/mail

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nette Mail: Sending E-mails

Downloads this Month Build Status Coverage Status Latest Stable Version License

Introduction

Almost every web application needs to send e-mails, whether newsletters or order confirmations. This library provides necessary tools.

Documentation can be found on the website.

If you like Nette, please make a donation now. Thank you!

Installation

The recommended way to install is via Composer:

composer require nette/mail

It requires PHP version 5.6 and supports PHP up to 7.2. The dev-master version requires PHP 7.1.

Usage

Example of creating an e-mail using Nette\Mail\Message class:

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.

And sending:

use Nette\Mail\SendmailMailer;

$mailer = new SendmailMailer;
$mailer->send($mail);

In addition to specifying recipient with addTo(), it's possible to specify recipient of copy with addCc() and recipient of blind copy: addBcc(). In all these methods, including setFrom(), we can specifiy 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>');

HTML content can be defined using setHtmlBody() method:

$mail->setHTMLBody('<b>Sample HTML</b> <img src="background.gif">');

Embedded images can be inserted using $mail->addEmbeddedFile('background.gif'), but it is not necessary. Why? Because Nette Framework finds and inserts all files referenced in the HTML code automatically. This behavior can be supressed by adding false as a second parameter of the setHtmlBody() method.

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.

Of course, it's possible to add attachments to the e-mail:

$mail->addAttachment('example.zip');

Can e-mail sending be even easier?

Custom mailer

Default mailer uses PHP function mail. If you need to send mail through a SMTP server, you can use SmtpMailer.

$mailer = new Nette\Mail\SmtpMailer([
        'host' => 'smtp.gmail.com',
        'username' => 'john@gmail.com',
        'password' => '*****',
        'secure' => 'ssl',
        'context' =>  [
            'ssl' => [
                'capath' => '/path/to/my/trusted/ca/folder',
             ],
        ],
]);
$mailer->send($mail);

You can also create your own mailer - it's a class implementing Nette\Mail\IMailer interface.

About

📧 Handy email creation and transfer library for PHP with both text and MIME-compliant support.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%