Git Product home page Git Product logo

nette-mail's Introduction

Nette Mail: Sending E-mails

Downloads this Month Build Status Coverage Status Latest Stable Version License

Almost every web application needs to send e-mails, whether newsletters or order confirmations. That's why Nette Framework provides necessary tools.

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

use Nette\Mail\Message;

$mail = new Message;
$mail->setFrom('John <[email protected]>')
	->addTo('[email protected]')
	->addTo('[email protected]')
	->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('[email protected]');
$mail->setFrom('[email protected]', 'John Doe');
$mail->setFrom('John Doe <[email protected]>');

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' => '[email protected]',
        '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.

nette-mail's People

Contributors

dg avatar kravco avatar vrana avatar jantvrdik avatar dundee avatar jakubboucek avatar enumag avatar xificurk avatar janedbal avatar fprochazka avatar grongor avatar jkrcma avatar janbuchar avatar juzna avatar pracj3am avatar pepakriz avatar f3l1x avatar andrewsville avatar pavolbiely avatar petrkle avatar robbertkl avatar zzromanzz avatar storkdork avatar masicek avatar hrach avatar petasittek avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.