uam/postmark-swiftmailer-bundle

Symfony 扩展包,提供 Postmark SwiftMailer 服务

0.3.1 2014-09-13 05:11 UTC

This package is auto-updated.

Last update: 2024-08-29 04:11:48 UTC


README

A Symfony2 扩展包,提供基于 Postmark API 的 SwiftMailer 传输实现。

虽然一个 Symfony 应用并不强制要求你使用 SwiftMailer 发送邮件,但是这样做有很多好处:发送邮件的日志,开发期间重定向到单个投递地址等。

此扩展包允许你在 Symfony 应用中使用 Postmark API 作为 SwiftMailer 传输。这为你提供了 SwiftMailer 和 Postmark 服务的好处。

此扩展包处于开发初期阶段,还不能视为可用于生产的。在生产中使用风险自负。

要求

你需要在 https://postmarkapp.com/ 上有一个活跃的账户和一个 Postmark API 密钥。

此扩展包依赖于 MZPostmarkBundle 并会自动包含它。

安装

将包添加到项目的 composer.json

"require": {
	"uam/postmark-swiftmailer-bundle": "dev-master",
	…
}

AppKernel.php 中注册此扩展包以及 MZPostmarkBundle

public function registerBundles()
{
	bundles = (
		// …
		new MZ\PostmarkBundle\MZPostmarkBundle(),
		new UAM\Bundle\PostmarkSwiftMailerBundle\UAMPostmarkSwiftMailerBundle(),
	);
	
	return bundles();
}

配置

按照该扩展包的文档配置 MZPostmarkBundle

# app/config/config.php

mz_postmark:
    api_key:    %postmark_api_key%
    from_email: %postmark_from_email%
    from_name:  %postmark_from_name%
    use_ssl:    %postmark_use_ssl%
    timeout:    %postmark_timeout%

更新你的 SwiftMailer 配置,使用此扩展包提供的 uam_postmark SwiftMailer 传输。

# app/config.php
swiftmailer:
	transport: uam_postmark

此扩展包没有特定的配置。

使用

此扩展包创建了一个名为 uam_postmark 的服务,该服务基于 Postmark API 实现了 SwiftMailer 传输。

按常规创建 SwiftMailer 消息。发送时,消息将通过 uam_postmark 传输路由到 Postmark 服务器。

SwitfMailer 插件

理论上,UAMPostmarkTransport 应该能够支持所有 swiftmailer 插件。然而,到目前为止,只有 Redirecting 插件被测试过。

重定向

按照 symfony SwiftMailerBundle 的文档编辑你的 swiftmailer 配置

# app/config.php
swiftmailer:
	delivery_address: test@example.com

已知问题

HTML 消息内容显示为纯文本

确保你的电子邮件中的 HTML 主体被设置为 MIME 部分

$message
    ->addPart($htmlBody, 'text/html');

电子邮件计数

Swift_Transport#send() 方法返回发送的消息计数。

此扩展包的实现将返回发送给包含在 'To' 头中的收件人的电子邮件数量。发送给 'Cc' 和 'Bcc' 收件人的电子邮件不会包含在返回的电子邮件计数中。

原因是 Postmark API 虽然支持 'Cc' 和 'Bcc' 收件人,但在请求发送消息时似乎不包含关于它们的任何数据。

失败的收件人

Swift_Transport#send() 方法的第二个参数旨在传递一个变量,该变量收集失败的收件人的电子邮件地址。

此扩展包对 Swift_Transport 接口的实现不支持这一点。按照设计,此实现将对所有收件人(包括 'To'、'Cc' 和 'Bcc')进行单个 Postmark API 调用(而不是对每个收件人进行一个调用)。如果其中一个电子邮件地址无效,则此单个调用将失败。

重定向自定义头信息丢失

SwiftMailer的重定向插件会在你的邮件中添加自定义头信息,以反映原始收件人('X-Swift-To', 'X-Swift-Cc', 'X-Swift-Bcc')。这些头信息在Postmark API中不被识别,并且通过Postmark发送的实际邮件中不会保留。