devster/rad-mailer

Symfony rad mailer

Installs: 40

Dependents: 0

Suggesters: 0

Security: 0

Stars: 6

Watchers: 2

Forks: 1

Open Issues: 0

Type:symfony-bundle

1.0.0 2014-08-29 13:21 UTC

This package is auto-updated.

Last update: 2024-10-29 02:07:27 UTC


README

Build Status Scrutinizer Code Quality Code Coverage PHP version

Send Twig templated email with Swiftmailer at speed of light. Dead simple.

Installation

Composer

Add this to your composer.json

{
    "require": {
        "devster/rad-mailer": "~1.0"
    }
}

Usage

Global usage

// twig is optional, like from
$mailer = new Rad\Mailer($swiftmailer, $twig, $from = 'john@example.com');

// Send a simple email
$nbEmailsSent = $mailer->send(array(
    // Optional. By default the value set in the constructor.
    // 'bob@example.com', array('bob@example.com' => 'Bob', ...)
    // or an object (see more details below)
    'from'      => 'bob@example.com',
    // Same as from
    'to'        => 'robert@example.com',
    // A twig template as string or a twig file template (ex: email.html.twig)
    'subject'   => 'Hello {{name}}!',
    // Same as subject
    'body'      => 'body.html.twig',
    // Optional. The data used in both templates subject and body
    'data'      => array('name' => 'Rob'),
    // Optional, default: text/html. 'text/html' or 'text/plain'
    'body_type' => 'text/plain'
));

// Send a more complex email
// Create a \Swift_Message pre set with data
$message = $mailer->createMessage(array(
    'to'        => 'robert@example.com',
    'subject'   => 'Hello {{name}}!',
    'body'      => 'body.html.twig',
    'data'      => array('name' => 'Rob'),
));

$message->attach(\Swift_Attachment::fromPath('/path/to/image.jpg'));

$nbEmailsSent = $mailer->sendMessage($message);

This library is aims to work with symfony, so you can pass an object as from and to option. This object must has:

* an `email` public property
* or `mail` public property
* or a `getEmail` public method
* or a `getMail` public method
* and a `name` public property
* or a `getName` public method
* or a `__toString` public method

Or you can extends the getDataFromObject method from the Rad\Mailer.

Symfony

Register the mailer as service

services:
    rad_mailer:
        class: Rad\Mailer
        arguments: [@mailer, @twig, 'rob@example.com']

Why not a bundle? Because its overkill. period.

Silex

$app = new \Silex\Application;

$app->register(new \Silex\Provider\SwiftmailerServiceProvider, ...);
$app->register(new \Silex\Provider\TwigServiceProvider, ...);

$app->register(new \Rad\Silex\MailerServiceProvider, array(
    'rad_mailer.from'  => 'rob@example.com', // Optional
    'rad_mailer.class' => 'MyMailer\That\Extends\Rad\Mailer', // Optional. By default 'Rad\Mailer' of course
));

$app['rad_mailer']->send(array(...));

License

This plugin is licensed under the DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE