优雅 / swiftmailer默认插件
SwiftMailer和Symfony的插件,用于设置电子邮件消息的默认属性(发件人、发送者、回复等)
v2.0.3
2018-05-01 04:54 UTC
Requires
- php: >=5.6
- swiftmailer/swiftmailer: >=4.0 <7.0
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: >=5.4.3 <8.0
README
此插件添加了设置发送消息默认属性的可能性(默认发件人地址、回复、主题等)。
// Set up a Mailer $transport = new Swift_SmtpTransport(); $mailer = new Swift_Mailer($transport); $mailer->registerPlugin(new Finesse\SwiftMailerDefaultsPlugin\SwiftMailerDefaultsPlugin([ 'from' => ['johndoe@example.com' => 'John Doe'], 'replyTo' => 'jackdoe@example.com' ])); // Use the Mailer many times $mailer->send( (new Swift_Message()) ->setTo('bjohnson@example.com', 'Bill Johnson') ->setSubject('Hi') ->setBody('This is awesome, I don\'t need to specify the from address!') );
如何安装
使用composer
在控制台中运行
composer require finesse/swiftmailer-defaults-plugin
如何使用
在设置Swift_Mailer
实例时创建并注册插件实例。
use Finesse\SwiftMailerDefaultsPlugin\SwiftMailerDefaultsPlugin; use Swift_Mailer; use Swift_SmtpTransport; // Setup an emails sending transport $transport = new Swift_SmtpTransport(); // Create a plugin instance $defaultsPlugin = new SwiftMailerDefaultsPlugin(/* default properties */); // Assemble them with a mailer $mailer = new Swift_Mailer($transport); $mailer->registerPlugin($defaultsPlugin);
对于Symfony 4,您可以这样注册插件
services: # Swift Mailer plugins Finesse\SwiftMailerDefaultsPlugin\SwiftMailerDefaultsPlugin: tags: - { name: swiftmailer.default.plugin } arguments: $defaults: from: johndoe@example.com: John Doe replyTo: jackdoe@example.com
Symfony 3示例
services: # Swift Mailer plugins app.swiftmailer.defaults_plugin: class: Finesse\SwiftMailerDefaultsPlugin\SwiftMailerDefaultsPlugin tags: - { name: swiftmailer.default.plugin } arguments: $defaults: from: johndoe@example.com: John Doe replyTo: jackdoe@example.com
当您需要发送电子邮件时,只需发送它,无需指定您为插件实例设置的参数。
use Swift_Message; $message = new Swift_Message(); $mailer->send($message);
如果您指定,则指定的参数将覆盖默认属性。
__constructor
您可以将所有可以设置到Swift_Mime_SimpleMessage
实例的属性传递给构造函数,使用set...
方法。例如
$defaultsPlugin = new SwiftMailerDefaultsPlugin([ 'from' => 'johndoe@example.com', 'subject' => 'Notification' ]);
数组键是Swift_Mime_SimpleMessage
方法名称,没有set
词,并且首字母小写。例如,body
属性对应于setBody
方法,readReceiptTo
对应于setReadReceiptTo
等等。
数组值是对应方法的第一个也是唯一的参数。值为null
的属性将被忽略。
setDefault
为属性设置默认值。
$defaultsPlugin->setDefault('sender', 'chasy@example.com', 'Chasy');
第一个参数是属性名称(参见__constructor)。其余参数是对应方法参数。
unsetDefault
移除默认值
$defaultsPlugin->unsetDefault('sender');
只有一个参数是属性名称(参见__constructor)。
版本兼容性
项目遵循语义版本化。
许可
MIT。有关详细信息,请参阅许可文件。