mailjet / mailjet-swiftmailer
为 Mailjet 实现的 SwiftMailer 传输层
2.0.0
2019-06-13 12:40 UTC
Requires
- php: >=7.0
- mailjet/mailjet-apiv3-php: ^1.2
- swiftmailer/swiftmailer: ~6.0
Requires (Dev)
- phpunit/phpunit: ^5.7
- symfony/config: >=2.0|~3.0
This package is auto-updated.
Last update: 2024-09-20 22:38:14 UTC
README
为 Mailjet 实现的 SwiftMailer 传输层 ([NEW] 现已支持发送 API v3.1) Mailjet 发送 API v3.1 兼容 Mailjet 发送 API V3 和 V3.1
如果您发现任何问题,请随时提出问题!
待办事项
- 添加 URL 标签
- 沙盒模式
- 改进单元测试(大量代码重复)
安装
使用 composer 安装包
composer require mailjet/mailjet-swiftmailer
使用示例
$transport = new MailjetTransport($dispatchEvent, $apiKey, $apiSecret); $transport->setClientOptions(['url' => "api.mailjet.com", 'version' => 'v3.1', 'call' => true]); $transport->send($message);
(默认选择发送 API v3)
Mailjet 客户端自定义配置
您可以在传输的构造函数中传递一个数组或使用 setClientOptions
函数
$clientOptions = ['url' => "api.mailjet.com", 'version' => 'v3.1', 'call' => false]; $transport = new MailjetTransport($dispatchEvent, $apiKey, $apiSecret, $clientOptions); or $transport->setClientOptions(['url' => "api.mailjet.com", 'version' => 'v3.1', 'call' => true]);
$options 属性
- url (默认: api.mailjet.com) : API 域名
- version (默认: v3) : API 版本(仅适用于 Mailjet API V3 +)
- call (默认: true) : 打开(true)/关闭 API 调用
- secured (默认: true) : 打开(true)/关闭使用 'https'
Mailjet 自定义头
通过 SwiftMailer 可以设置特定的 Mailjet 头或自定义用户定义的头
例如
$headers = $message->getHeaders(); $headers->addTextHeader('X-MJ-TemplateID', $templateId); $headers->addTextHeader('X-MJ-TemplateLanguage', true); $vars = array("myFirstVar" => "foo", "mySecondVar" => "bar"); $headers->addTextHeader('X-MJ-Vars', json_encode($vars));
注意:您需要使用 json_encode
将变量数组转换为与 SMTP 传输兼容的格式。
Mailjet 批量发送
$emails = ['f001@bar.com', 'f002@bar.com', 'f003@bar.com', 'f004@bar.com', 'f005@bar.com', 'f006@bar.com', ...] $messages = []; foreach ($emails as $email) { $message = new \Swift_Message('Test Subject', '<p>Foo bar</p>', 'text/html'); $message ->addTo($email) ->addFrom('from@example.com', 'From Name') ->addReplyTo('reply-to@example.com', 'Reply To Name') ; array_push($messages, $message); } $transport = new MailjetTransport($dispatchEvent, $apiKey, $apiSecret); $result = $transport->bulkSend($messages);
注意:与 Spool 不兼容(SwiftMailer 已从其 API 中删除 bulkSend)。
Symfony 集成
如果您想在您的 Symfony 项目中使用 MailjetTransport,请按照以下步骤操作
composer require mailjet/mailjet-swiftmailer
- 在您的
services.yml
中注册 MailjetTransport
swiftmailer.mailer.transport.mailjet: class: Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport arguments: - "@swiftmailer.transport.eventdispatcher.mailjet" - "%mailjet.api_key%" - "%mailjet.secret_key%"
注意:我们将 mailjet.api_key
和 mailjet.secret_key
设置到 parameters.yml
中
- 最后,在您的
config.yml
中配置 SwiftMailer
# Swiftmailer Configuration swiftmailer: transport: mailjet
注意:您也可以注入自己的 Mailjet\Client
mailjet.transactionnal.client: class: "%mailjet.client.class%" arguments: - "%mailjet.api_key%" - "%mailjet.secret_key%" - %mailjet.transactionnal.call% - %mailjet.transactionnal.options% swiftmailer.transport.eventdispatcher.mailjet: class: Swift_Events_SimpleEventDispatcher swiftmailer.mailer.transport.mailjet: class: Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport arguments: - "@swiftmailer.transport.eventdispatcher.mailjet" - "%mailjet.api_key%" - "%mailjet.secret_key%" - %mailjet.transactionnal.call% - %mailjet.transactionnal.options% calls: - method: setExternalMailjetClient arguments: - '@mailjet.transactionnal.client'
Mailjet 参考
执行测试
vendor/bin/phpunit -c .
贡献
如果您想为此项目做出贡献,请查看 这里