fusonic / messenger-mailer-bundle
Symfony 扩展包,用于通过 Symfony Messenger 异步发送带附件的电子邮件。
2.3.0
2024-08-21 06:50 UTC
Requires
- php: >=8.2
- symfony/config: ^6.2 || ^7.0
- symfony/dependency-injection: ^6.2 || ^7.0
- symfony/filesystem: ^6.2 || ^7.0
- symfony/http-kernel: ^6.2 || ^7.0
- symfony/mailer: ^6.2 || ^7.0
- symfony/messenger: ^6.2 || ^7.0
- symfony/mime: ^6.2 || ^7.0
- symfony/twig-bridge: ^6.2 || ^7.0
- symfony/yaml: ^6.2 || ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.58
- infection/infection: ^0.29
- phpstan/phpstan: ^1.11
- phpstan/phpstan-deprecation-rules: ^1.2
- phpstan/phpstan-phpunit: ^1.4
- phpstan/phpstan-strict-rules: ^1.6
- phpstan/phpstan-symfony: ^1.4
- phpunit/phpunit: ^11.2
- symfony/framework-bundle: ^6.2 || ^7.0
- tomasvotruba/type-coverage: ^0.3
README
关于
如果您的项目满足以下条件,可能需要打包:
- 您正在使用 Symfony Mailer 与 Symfony Messenger。
- 包含 Mailer 消息的消息队列正在异步运行。
- 您的电子邮件包含 附件。
此扩展包解决了以下可能出现的问题:
- 如果您使用
Symfony\Component\Mime\Email::attach
,消息将包含整个文件。在消息传输中使用 blob 数据不建议,并且可能导致问题。 - 如果您使用
Symfony\Component\Mime\Email::attachFromPath
,路径可能在处理消息时不存在(取决于您的实现)。例如,当您正在生成临时文件(如 PDF)并希望将其附加到电子邮件时。如果这是一个临时文件,它可能在处理消息之前被删除。
安装
使用 composer 从 Packagist 安装扩展包。
composer require fusonic/messenger-mailer-bundle
需求
- PHP 8.2+
- Symfony 6.2+
如果 Symfony 没有将扩展包添加到扩展包配置中,请添加以下内容(默认位于 config/bundles.php
)
<?php
return [
// ...
Fusonic\MessengerMailerBundle\MessengerMailerBundle::class => ['all' => true],
];
配置(可选)
您需要配置的只有处理 SendEmailMessage
事件的提供的中间件。
# Your messenger configuration framework: # ... messenger: # ... buses: default: # or your own bus that handled the SendEmailMessage event - Fusonic\MessengerMailerBundle\Middleware\AttachmentEmailMiddleware # Bundle default configuration messenger_mailer: # Use the included filesystem implementation or implement your own service # by implementing the `Fusonic\MessengerMailerBundle\Contracts\EmailAttachmentHandlerInterface` interface. attachment_handler: Fusonic\MessengerMailerBundle\EmailAttachmentHandler\FilesystemAttachmentHandler services: # Configure the services used as the `attachment_handler` above. This service is configured by default. Fusonic\MessengerMailerBundle\EmailAttachmentHandler\FilesystemAttachmentHandler: arguments: $attachmentsDirectory: "%kernel.project_dir%/var/email-attachments"
如果您想使用不同的服务来处理附件,您可以创建自己的并覆盖默认配置中的服务配置。例如,您可以创建一个将附件保存到抽象文件系统中的处理程序(例如:thephpleague/flysystem
)。
使用
此扩展包提供了两个用于创建电子邮件的类 AttachmentEmail(Symfony Email
类的扩展)和 TemplatedAttachmentEmail(Symfony TemplatedEmail
类的扩展)。
您应该使用 addPersistedPart
代替 attach
、attachFromPath
和 addPart
。这将根据 FilesystemAttachmentHandler
持久化内容。这样,电子邮件可以安全地异步处理。
$email = (new TemplatedAttachmentEmail()) ->from('hello@example.com') ->addPersistedPart(new DataPart('Email text', 'filename.txt', 'plain/text')) // ... ->html('...');
贡献
这是从 fusonic/php-extensions 仓库中分割的子树。请在那里创建您的拉取请求。