RAD Mailer
, (*1)
Send Twig templated email with Swiftmailer at speed of light. Dead simple., (*2)
, (*3)
Installation
Composer
Add this to your composer.json, (*4)
{
"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:, (*5)
* 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
., (*6)
Symfony
Register the mailer as service, (*7)
services:
rad_mailer:
class: Rad\Mailer
arguments: [@mailer, @twig, 'rob@example.com']
Why not a bundle? Because its overkill. period., (*8)
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, (*9)