silktide / templated-emailer
此软件包已被放弃且不再维护。未建议替代软件包。
模板邮件发送器
0.1.1
2015-09-10 16:55 UTC
Requires
- php: >=5.5.0
- swiftmailer/swiftmailer: ^5.4
- symfony/templating: ^2.7
Requires (Dev)
- codeclimate/php-test-reporter: ^0.2.0
- phpunit/phpunit: ^4.8
This package is auto-updated.
Last update: 2021-06-09 06:20:09 UTC
README
模板邮件发送器
这个库简单地将 Symfony 强大的模板引擎与 Swiftmailer 的灵活性结合起来,使您能够轻松发送模板邮件。
设置
不使用 DI
如果您不使用 DI(或者不知道 DI 是什么)有一个有用的工厂来创建邮件发送器
$emailer = \Silktide\TemplatedEmailer\TemplatedEmailerFactory::create('/path/to/templates');
唯一必需的参数是包含您的邮件模板的文件夹的路径(请参阅下文“创建模板”)。
默认情况下,工厂将设置类使用 PHP 的 mail() 函数发送邮件和 Symfony 的 PhpEngine 进行模板化。如果您想使用不同的传输方式(例如 SMTP),可以可选地提供 Swiftmailer 传输类 作为第二个参数。以下是一个使用替代传输的示例
$transport = \Swift_SmtpTransport::newInstance('smtp.example.org', 25)
->setUsername('your username')
->setPassword('your password');
$emailer = \Silktide\TemplatedEmailer\TemplatedEmailerFactory::create('/example', $transport);
依赖注入/手动实例化
TemplatedEmailer 类只需要两个参数,一个 Symfony 模板引擎和一个 Swift_Mailer 实例
/**
* @var \Symfony\Component\Templating\EngineInterface
*/
$templateEngine;
/**
* @var \Swift_Mailer
*/
$emailClient;
$emailer = new \Silktide\TemplatedEmailer\TemplatedEmailer($templateEngine, $emailClient);
创建模板
模板使用 Symfony 的文档齐全的模板库。以下是一个非常基本的示例
<h1>An email from myApp</h1>
<p>Dear <?php echo $recipientName; ?></p>
<p><?php echo $message; ?>
消息中的变量作为数组传递给 send() 方法(请参阅下文使用说明)。
使用
在发送任何电子邮件之前,您必须设置一个发件人
$emailer->setSender('pat@postman.com', 'Postman Pat');
现在发送电子邮件只需要一个收件人、主题、模板文件名和上下文
$this->mailer->send(
'mrsgoggins@greendalepo.com',
'Jess the black and white cat',
'anEmail.php',
[
'recipientName' => 'Mrs Goggins',
'message' => 'Hello!'
]
);
收件人也可以通过使用数组来设置一个名称,格式为 ['email@host.tld' => 'Friendly Name']
$this->mailer->send(
['mrsgoggins@greendalepo.com' => 'Mrs Goggins'],
'Jess the black and white cat',
'anEmail.php',
[
'recipientName' => 'Mrs Goggins',
'message' => 'Hello!'
]
);