Mailer

[Since 0.5.6]

Introduction

Pletfix mailer is an extension of PHP's mailer like PHPMailer or Swift Mailer - but it's more KISS :-)

Configuration

The configuration is located at config/mailer.php. In this file you may specify which from and reply-to address you would like to use sending a mail.

'from' => env('MAIL_FROM'),

'reply_to' => [
    env('MAIL_REPLY_TO'),
],

If you wish to just pretend the sending, set the pretend option. In this case, the mail will be written to your application's log file so you are able to inspect the message.

'pretend' => false,

Create a Mailer

You may use the mailer() function to access a mailer instance:

$mailer = mailer();

The mailer() function is just a shortcut to get the Mailer instance supported by Dependency Injector:

  $mailer = DI::getInstance()->get('mailer');

Send a Mail

You may use the send method to send a mail, either as a simple plain text message...

$mailer = mailer();
$mailer->send('receiver@eexample.com', 'This is a subject', 'This is a simple text message!');

... or as an HTML mail:

$mailer->send('receiver@eexample.com', 'This is a subject', '<html><body><h1>Hello World!</h1></body></html>');

If you send an HTML file, a plain text is automatically generated from the HTML body so mail clients that can not read HTML (such as mutt & eudora) can also display the message. You may specify the plain text manually with the altBody method if you want a different plain text message.

$mailer
    ->altBody('*** Hello World! ***')
    ->send('receiver@eexample.com', 'This is a subject', '<html><body><h1>Hello World!</h1></body></html>');

If you prefer, you can also pass the subject, message, and recipient in separate methods, just like other optional parameters:

$mailer
    ->to('receiver@eexample.com')
    ->cc('user@eexample.com')
    ->bcc('another.user@eexample.com')
    ->replyTo('mail@eexample.com')
    ->from('webmailer@eexample.com')
    ->subject('This is a subject')
    ->body('
        <html>
            <body>
                <h1>Hello World!</h1>
                <img src="' . $mailer->embed(public_path('images/logo.png')) . '"/>
            </body>
        </html>
    ')
    ->altBody('*** Hello World! ***')
    ->attach(resource_path('files/agb.pdf'))
    ->send();

Furthermore, you may use the view method to render the body of the message:

$mailer->view('confirm', $variables);

Receivers

The Pletfix Mailer provides several ways to specify an e-mail address. Here for example, let the to method be representative of cc, bcc, replyTo and from:

$mailer->to('user@example.com');
$mailer->to('User <user@example.com>');
$mailer->to('user@example.com', 'User');

Please note that the address have to be compatible with RFC 2822. e.g. "user@example.com" or "User <user@example.com>".

Except for the from method, more than one address can be specified:

$mailer->to(['first@example.com', 'second@example.com']);
$mailer->to(['First User<first@example.com>', 'Second User<second@example.com>']);

Multiple addresses can also be specified by repeated call:

$mailer->to('first@example.com')
       ->to('second@example.com');

Of course, there are also methods to remove one or more addresses:

$mailer->removeTo('user@example.com');
$mailer->removeTo(['first@example.com', 'second@example.com']);
$mailer->clearTo(); // Remove all receiver

Attachments

Use the attach method to add a file to the mail:

$mailer->attach(resource_path('files/info.pdf'));

You may also specify the display name of the file:

$mailer->attach(resource_path('files/info.pdf'), 'Information.pdf');

You can use the detach method to remove an already attached file:

$mailer->detach(resource_path('files/info.pdf'));

Use clearAttachments to remove all attachments from the mail:

$mailer->clearAttachments();

Embedded Files

In addition to attachments, you can also embed data into HTML mails by using the embed method. Above all, this is often used for images. Pletfix offers a convenient way to embed images into your e-mails and retrieve the corresponding Content-ID. The following example shows how it works:

$mailer
    ->to('receiver@eexample.com')
    ->subject('This is a subject')
    ->body('
        <html>
            <body>
                <h1>Hello World!</h1>
                <img src="' . $mailer->embed(public_path('images/logo.png')) . '"/>
            </body>
        </html>
    ')
    ->send();

Use removeEmbeddedFile to remove an already embedded file:

$mailer->removeEmbeddedFile(public_path('images/logo.png'));

Remove all embedded files by calling clearEmbeddedFile:

$mailer->clearEmbeddedFile();

(edit on GitHub)