ronrademaker / mailer
Swiftmailer 的包装器
1.2.0
2017-06-08 14:23 UTC
Requires
- swiftmailer/swiftmailer: ^5.4
- twig/twig: ^1.23
Requires (Dev)
- mockery/mockery: ^0.9.4
- phpunit/phpunit: ^4.7
README
Swiftmail 的包装器。
用法
可以使用邮件包装器而不需要了解 Swift Mailer,或者通过创建自己的 Swift_Message 来使用其全部功能。
安装
composer require ronrademaker/mailer
配置
假设您想在 DIC 中注册邮件器,以下是一个 Symfony XML 示例:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Services file that should be included in any DHD project
-->
<container xmlns='https://symfony.ac.cn/schema/dic/services'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:schemaLocation='https://symfony.ac.cn/schema/dic/services https://symfony.ac.cn/schema/dic/services/services-1.0.xsd'>
<parameters>
<parameter key='mailer_twig.path'>[LOCATION OF YOUR TEMPLATES]</parameter>
<parameter key='mailer_twig.options' type='collection'>
[ADD YOUR OPTIONS]
</parameter>
</parameters>
<services>
<service id='mailer_transport' class='Swift_Transport'>
<factory class='Swift_SendmailTransport' method='newInstance'/>
</service>
<service id='mailer_swift' class='Swift_Mailer'>
<factory class='Swift_Mailer' method='newInstance'/>
<argument type='service' id='mailer_transport'/>
</service>
<service id='mailer_twig_loader' class='Twig_Loader_Filesystem'>
<argument>%mailer_twig.path%</argument>
</service>
<service id='mailer_twig' class='Twig_Environment'>
<argument type='service' id='mailer_twig_loader'/>
<argument>%mailer_twig.options%</argument>
</service>
<service id='mailer' class='RonRademaker\Mailer\Mailer'>
<argument type='service' id='mailer_twig'/>
<argument type='service' id='mailer_swift'/>
</service>
</services>
</container>
使用 twig 模板发送邮件
示例模板
{% block subject 'My Mail Subject' %}
{% block body_html %}
<p>Some mail with HTML formatting</p>
{% endblock %}
{% block body_text %}
Some mail in boring plain text
{% endblock %}
发送邮件
$mailer->sendEmail(['example@example.org' => 'Example Receiver'], 'sender@example.org' => 'Example Sender', 'my_twig_template');
发送 Swift Message
在 $message 中创建您的消息,并
$mailer->sendMessage($message);
从对象中检索接收者
可以使用 Determiner 从电子邮件配置中检索接收者(电子邮件/名称)。例如,从一个对象中:
$profile = new Profile(); $profile->setEmail('foobarz@example.com'); $profile->setFullName('Foo Barz'); $emailConfiguration = ['{{email}}' => '{{full_name'}}]; $determiner = new RonRademaker\Mailer\Receiver\Determiner(); $receiver = $determiner->getReceivers($profile, null, $emailConfiguration); // results in ['foobarz@example.com' => 'Foo Barz'];
或者具有链式字段配置的对象
$profile = new Profile(); $profile->setEmail('foobarz@example.com'); $profile->setFullName('Foo Barz'); $company = new Company(); $company->setContactperson($profile); $emailConfiguration = ['email' => '{{contactperson.email}}', 'name' => '{{contactperson.full_name'}}]; $determiner = new RonRademaker\Mailer\Receiver\Determiner(); $receiver = $determiner->getReceivers($company, null, $emailConfiguration); // results in ['email' => 'foobarz@example.com', 'name' => 'Foo Barz'];
注意:Determiner 需要getter。
或者直接使用硬编码设置
$emailConfiguration = ['email' => 'foobarz@example.com', 'name' => 'Foo Barz']; $determiner = new RonRademaker\Mailer\Receiver\Determiner(); $receiver = $determiner->getReceivers($object, null, $emailConfiguration); // results in ['email' => 'foobarz@example.com', 'name' => 'Foo Barz'];