wildbit / swiftmailer-postmark
Postmark 的 Swiftmailer 传输。
3.4.0
2022-09-05 08:46 UTC
Requires
- guzzlehttp/guzzle: ^6.0|^7.0
- swiftmailer/swiftmailer: ^6.0.0
Requires (Dev)
- phpunit/phpunit: ^6.0.0
This package is auto-updated.
Last update: 2024-09-05 12:59:54 UTC
README
官方 Swiftmailer 传输用于 Postmark。
通过您最喜欢的 PHP 框架发送邮件!
使用方法
1. 在项目中包含此包
composer require wildbit/swiftmailer-postmark
2. 使用传输发送消息
<?php //import the transport from the standard composer directory: require_once('./vendor/autoload.php'); $transport = new \Postmark\Transport('<SERVER_TOKEN>'); $mailer = new Swift_Mailer($transport); //Instantiate the message you want to send. $message = (new Swift_Message('Hello from Postmark!')) ->setFrom(['[email protected]' => 'John Doe']) ->setTo(['[email protected]']) ->setBody('<b>A really important message from our sponsors.</b>', 'text/html') ->addPart('Another important message from our sponsors.','text/plain'); //Add some attachment data: $attachmentData = 'Some attachment data.'; $attachment = new Swift_Attachment($attachmentData, 'my-file.txt', 'application/octet-stream'); $message->attach($attachment); //Send the message! $mailer->send($message); ?>
3. 在 Postmark API 错误上抛出异常
$transport = new \Postmark\Transport('<SERVER_TOKEN>'); $transport->registerPlugin(new \Postmark\ThrowExceptionOnFailurePlugin()); $message = new Swift_Message('Hello from Postmark!'); $mailer->send($message); // Exception is throw when response !== 200
4. 使用默认头信息
您可以在传输级别设置默认头信息,这些信息将应用于每条消息,除非被覆盖。
$defaultHeaders = ['X-PM-Tag' => 'my-tag']; $transport = new \Postmark\Transport('<SERVER_TOKEN>', $defaultHeaders); $message = new Swift_Message('Hello from Postmark!'); // Overwriting default headers $message->getHeaders()->addTextHeader('X-PM-Tag', 'custom-tag');
5. 设置消息流
默认情况下,发送消息时将使用 "outbound" 事务流。
// Change the default stream for every message via Default Headers $transport = new \Postmark\Transport('<SERVER_TOKEN>', ['X-PM-Message-Stream' => 'your-custom-stream']); $message = new Swift_Message('Hello from Postmark!'); // Overwrite the default stream for a specific message by setting the header $message->getHeaders()->addTextHeader('X-PM-Message-Stream', 'another-stream');
6. 发送后获取 Postmark 消息 ID
在将邮件发送到 Postmark 后,可以获取 Postmark ID。
$transport = new \Postmark\Transport('<SERVER_TOKEN>', $defaultHeaders); $mailer = new Swift_Mailer($transport); $message = new Swift_Message('Hello from Postmark!'); $mailer->send($message); $postmarkID = $message->getHeaders()->get('X-PM-Message-Id')->getValue();
发布新版本
Swiftmailer 传输使用 packagist。要发布新版本,只需在 GitHub 上创建一个带有版本号的新的发布标签。
注意
- 传输使用 Postmark API 内部发送邮件,通过 /email 端点。目前此库不支持其他发送功能,如批量发送或通过模板发送。