devster/rad-mailer

Symfony rad mailer

安装: 39

依赖项: 0

建议者: 0

安全: 0

星标: 6

关注者: 2

分支: 1

公开问题: 0

类型:symfony-bundle

1.0.0 2014-08-29 13:21 UTC

This package is auto-updated.

Last update: 2024-08-29 01:35:16 UTC


README

Build Status Scrutinizer Code Quality Code Coverage PHP version

以光速发送Twig模板电子邮件,简单到极致。

安装

Composer

将以下内容添加到您的composer.json中

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

使用方法

全局使用

// 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);

这个库旨在与symfony一起工作,因此您可以将对象作为fromto选项传递。这个对象必须具有

* 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

或者您可以扩展Rad\Mailer中的getDataFromObject方法。

Symfony

注册为服务

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

为什么不使用bundle?因为它太过于冗余了。就这么简单。

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(...));

许可证

此插件受DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE许可。