フレームワーク連携
Symfony
Symfony Mailer を使い、Sendery のテンプレートでトランザクションメールを送信します。
composer require sendery/symfony:^0.1.4動作環境
Symfony Mailer 7.4、PHP 8.3 以上と cURL 拡張が必要です。
トランスポートを登録
プロジェクトの API キーを作成し、config/services.yaml にトランスポートを登録します。
# config/services.yaml — add to your existing services section.
services:
Sendery\Symfony\SenderyTransportFactory:
tags: ['mailer.transport_factory']API キーを設定
.env.local またはホスティングのシークレット設定に MAILER_DSN を指定します。
MAILER_DSN=sendery://[email protected]Mailer を設定
既存の Mailer 設定でこの DSN を使用します。
# config/packages/mailer.yaml
framework:
mailer:
dsn: '%env(MAILER_DSN)%'メールを送信
宛先、公開済みテンプレートのキー、変数をこのサービスに渡します。from() にはプロジェクトの送信者アドレスを指定してください。
use Sendery\Symfony\TemplateEmail;
use Symfony\Component\Mailer\MailerInterface;
final class TemplateEmails
{
public function __construct(private MailerInterface $mailer) {}
public function send(string $address, string $template, array $data): void
{
$email = (new TemplateEmail())
->from('[email protected]')
->to($address)
->template($template, $data);
$this->mailer->send($email);
}
}Messenger で送信
Symfony Messenger と利用するトランスポートをインストールし、MESSENGER_TRANSPORT_DSN を設定してメールを async にルーティングします。php bin/console messenger:consume async を実行してください。同じ TemplateEmail の再試行ではキーと変数が保持されます。
# config/packages/messenger.yaml
framework:
messenger:
transports:
async: '%env(MESSENGER_TRANSPORT_DSN)%'
routing:
'Symfony\Component\Mailer\Messenger\SendEmailMessage': asyncエラー処理
送信に失敗すると TransportException が発生します。Messenger を使う場合は、同じメールで一時的なエラーを再試行します。API キー、テンプレート、請求のエラーは原因を修正してから再送してください。
特定のバージョンを送信
公開履歴のバージョンを指定すると、テンプレートの更新後もその版を送信できます。指定しない場合は最新バージョンを使います。
use Sendery\Symfony\TemplateEmail;
$email = (new TemplateEmail())
->from('[email protected]')
->to('[email protected]')
->template('your-template', [
'name' => 'Alex',
'action_url' => 'https://example.com/start',
])
->version(3);
$mailer->send($email);添付ファイル
Symfonyの attach() メソッドで TemplateEmail にファイルを添付します。
最大10ファイル、合計5 MBまで送信できます。対応形式と上限は 添付ファイルのリファレンス を参照してください。
use Sendery\Symfony\TemplateEmail;
$email = (new TemplateEmail())
->from('[email protected]')
->to('[email protected]')
->template('your-template', [
'name' => 'Alex',
'action_url' => 'https://example.com/start',
], idempotencyKey: 'your-idempotency-key')
->attach(file_get_contents('/path/document.pdf'), 'document.pdf', 'application/pdf');
$mailer->send($email);