voilab/mailer

小型邮件工具。

2.1.1 2017-02-22 13:12 UTC

This package is auto-updated.

Last update: 2024-08-29 03:20:58 UTC


README

事务性邮件发送器

基本用法

创建主要邮件传输对象

use voilab\mailer\Transport;
use voilab\mailer\adapter;
use voilab\mailer\renderer;
$mailer = new Transport();
$mailer->setAdapterFn(function (Transport $mail, array $createParams) {
    return new adapter\SomeAdapter();
});

将数据填充到邮件中...

$adapter = $mailer
    ->create(['customParam' => 1])
    ->setFrom('from@email.com')
    ->addTo('to@email.com')
    ->setSubject('A subject from James')
    ->setHtml('<p>Hello John</p>');

...或者使用全局数据、模板和渲染器来引入变化

请查看以下部分以获取关于渲染器和模板的更多解释。

$mailer->setRenderer(new renderer\File('/path'));
$adapter = $mailer
    ->create()
    ->setFrom('from@email.com')
    ->addTo('to@email.com')
    ->setTemplate('some-file.php')
    ->setGlobalData([
        'me' => 'James',
        'you' => 'John'
    ]);
<!-- /path/some-file.php -->
<h1 data-renderer-subject>A subject from <?= $me; ?></h1>
<p>Hello <?= $you; ?></p>

发送邮件

try {
    $mailer->send($adapter);
    echo "mail is sent";
} catch (\Exception $e) {
    echo $e->getMessage();
}

模板

当使用模板时,主题、HTML 或文本将被忽略。它们都在模板内部。您需要在适配器中设置模板 ID。

$adapter->setTemplate('some-template-id');

请注意,如果您使用网络模板,例如 Sparkpost 或 Sendgrid,则可以混合内联 HTML 和变量。上述限制仅适用于您使用渲染器(例如,使用 Twig)时。

使用模板和渲染器,您可以在邮件发送前自动设置内容。您可以使用这些方法随时设置 HTML 或文本内容

$adapter = $mailer
    ->setRenderer(new renderer\File('/path/files'))
    ->create();

$mailer
    ->setHtml($adapter, 'file_html.php')
    ->setText($adapter, 'file_text.php');

// then send the email

文件

文件渲染器是 PHP 文件的快速简单加载器。您提供给适配器的全局数据将注入到文件中(使用 extract),您可以使用原始 PHP、包含等来使用它们。

对于变量没有进行任何检查。请自行承担使用此模板模式的风险。

$renderer = new renderer\File('/path/to/files');
$mailer->setRenderer($renderer);

$adapter = $mailer
    ->create()
    ->setTemplate('file.php');
<h1 data-renderer-subject>This is the mail subject  <?= $some_param; ?></h1>
<p>
    All the rest will be the mail body, with all html needed, and access to all
    params and functions from PHP.
</p>
<blockquote>
    The <strong>p</strong> tag is absolutely not required. You can start your
    message body (after the first h1) with anything you want, even an other h1
    or no html tag at all.
</blockquote>

Twig

与文件渲染器相比,Twig 渲染器更为先进。您不加载经典 PHP 文件,而是加载 twig 模板。您提供给适配器的全局数据在渲染 twig 模板时用作参数。

$twig = new \Twig_Environment();
// configure your environment as you want

$renderer = new renderer\Twig($twig);
$mailer->setRenderer($renderer);

$mailer->create()->setTemplate('some_file.twig');
<h1 data-renderer-subject>This is the mail subject {{ some_param }}</h1>
<p>
    All the rest will be the mail body, with all html needed, and access to all
    params and functions from your <em>twig</em> environment.
</p>
<blockquote>
    The <strong>p</strong> tag is absolutely not required. You can start your
    message body (after the first h1) with anything you want, even an other h1
    or no html tag at all.
</blockquote>

请注意,您需要将此依赖项添加到自己的 package.json 中。

  • "twig/twig": "1.*"

适配器

本地邮件函数

要使用本地邮件函数,您可以使用 Zend2Zend3 适配器,并使用正确的传输机制,如下所示。

$zend = new \Zend\Mail\Transport\Sendmail();

$mailer = new Transport();
$mailer->setAdapterFn(function () use ($zend) {
    return new adapter\Zend2($zend);
});

请注意,您需要将这些依赖项添加到自己的 package.json 中。

  • "zendframework/zend-mail": "2.*"
  • "zendframework/zend-servicemanager": "3.*"

Zend Mail 3

// create the zend transport that fit your needs
$zend = new \Zend\Mail\Transport\Smtp(
    new \Zend\Mail\Transport\SmtpOptions([
        // smtp config
    ])
);

$mailer = new Transport();
$mailer->setAdapterFn(function () use ($zend) {
    return new adapter\Zend3($zend);
});

请注意,您需要将这些依赖项添加到自己的 package.json 中。

  • "zendframework/zend-mail": "2.*"
  • "zendframework/zend-servicemanager": "3.*"

Zend Mail 2

// create the zend transport that fit your needs
$zend = new \Zend\Mail\Transport\Smtp(
    new \Zend\Mail\Transport\SmtpOptions([
        // smtp config
    ])
);

$mailer = new Transport();
$mailer->setAdapterFn(function () use ($zend) {
    return new adapter\Zend2($zend);
});

请注意,您需要将这些依赖项添加到自己的 package.json 中。

  • "zendframework/zend-mail": "2.*"
  • "zendframework/zend-servicemanager": "3.*"

Sparkpost

$client = new \Http\Adapter\Guzzle6\Client(new \GuzzleHttp\Client());
$sparkpost = new SparkpostTransport($client, [
    'key' => 'api key'
]);

$mailer = new Transport();
$mailer->setAdapterFn(function () use ($sparkpost) {
    return new adapter\Sparkpost2($sparkpost);
});

请注意,您需要将这些依赖项添加到自己的 package.json 中。

  • "sparkpost/sparkpost": "2.*"
  • "php-http/guzzle6-adapter": "1.*"

SendGrid

$client = new \SendGrid('api key');

$mailer = new Transport();
$mailer->setAdapterFn(function () use ($client) {
    return new adapter\Sendgrid5($client);
});

请注意,您需要将此依赖项添加到自己的 package.json 中。

  • "sendgrid/sendgrid": "5.*"

Mandrill

$client = new \Mandrill('api key');

$mailer = new Transport();
$mailer->setAdapterFn(function () use ($client) {
    return new adapter\Mandrill($client);
});

请注意,您需要将此依赖项添加到自己的 package.json 中。

  • "mandrill/mandrill": "1.*"

测试

首先,将 tests/phpunit_default.xml 复制到 tests/phpunit.xml,并填写正确的值。然后,在 vagrant ssh 中,只需这样做

cd /vagrant
./vendor/bin/phpunit -c tests/phpunit.xml

要使 phpunit 发送邮件,只需在 phpunit.xml 文件中相应的常量中设置 true