boundstate / yii2-mailgun
为Yii框架提供的Mailgun邮件集成
v1.4.0
2021-02-04 15:43 UTC
Requires
- mailgun/mailgun-php: ^3
- yiisoft/yii2: >=2.0.4
Requires (Dev)
- kriswallsmith/buzz: ^1.0
- nyholm/psr7: ^1.2
- phpunit/phpunit: ^8
- vlucas/phpdotenv: ^3.6
Suggests
- kriswallsmith/buzz: Mailgun API client requires an HTTP client
- nyholm/psr7: Mailgun API client requires a PSR-7 implementation
README
安装
安装此扩展的首选方式是通过composer。
composer require boundstate/yii2-mailgun
Mailgun API客户端并非紧密耦合到Guzzle、Buzz或其他发送HTTP消息的库。您还必须安装您想要使用的PSR-7实现和HTTP客户端。
如果您想快速开始,应安装Buzz和nyholm/psr7
composer require kriswallsmith/buzz nyholm/psr7
用法
要使用此扩展,只需在您的应用程序配置中添加以下代码
return [ //.... 'components' => [ 'mailer' => [ 'class' => 'boundstate\mailgun\Mailer', 'key' => 'key-example', 'domain' => 'mg.example.com', ], ], ];
然后您可以按照以下方式发送电子邮件
Yii::$app->mailer->compose('contact/html', ['contactForm' => $form]) ->setFrom('from@domain.com') ->setTo($form->email) ->setSubject($form->subject) ->send();
您还可以指定地址数组,并/或指定名称
$message->setTo(['bob@example.com' => 'Bob']);
警告:默认情况下,所有收件人的电子邮件地址都将显示在每个收件人的
to
字段中。启用批量发送以避免此问题。
批量发送
当启用批量发送时,Mailgun将为每个收件人发送单独的电子邮件,仅包含他们的电子邮件地址在to
字段中。
要使用批量发送,请将应用程序配置中的messageClass
设置为boundstate\mailgun\BatchMessage
'mailer' => [ 'class' => 'boundstate\mailgun\Mailer', 'messageClass' => 'boundstate\mailgun\BatchMessage', // ... ]
编写批量电子邮件类似于常规电子邮件,只是您可以定义和使用收件人变量
Yii::$app->mailer->compose('hello') ->setTo([ 'bob@example.com' => [ 'id': 3, 'full_name' => 'Bob' ], 'jane@example.com' => [ 'id': 4, 'full_name' => 'Jane' ], ]) ->setSubject('Hi %recipient.full_name%') ->send();
有关更多信息,请参阅Mailgun文档和Yii官方指南中的相关部分。