frankspress / sg-parser-bundle
解析并验证发送到 WebHook 的 SendGrid 入站电子邮件内容 - Symfony 5
1.0.0
2020-03-28 06:49 UTC
Requires
- php: ^7.1.3
- symfony/dependency-injection: 5.*
- symfony/event-dispatcher: 5.*
- symfony/validator: 5.*
- symfony/yaml: 5.0.*
This package is auto-updated.
Last update: 2024-09-29 05:42:36 UTC
README
解析并验证发送到 WebHook 的 SendGrid 电子邮件/附件( Symfony 5 )
需求
- PHP 7.1.3 或更高版本;
- Symfony 5.*.
安装
composer require frankspress/sg-parser-bundle
使用
创建一个新的路由,并将 prefix
设置为配置您的新的 Webhook 点。
# app/routes/sg_api_parser.yml sg_api_parsers: resource: '@FrankspressSgParserBundle/Resources/config/routes.xml' prefix: '/api/sendgrid/inbound' trailing_slash_on_root: false
创建一个新的 EventSubscriber。当新的电子邮件发送到您的 API 点时,它将自动被调用。
namespace App\EventSubscriber; use Frankspress\SgParserBundle\Attachment\NewAttachment; use Frankspress\SgParserBundle\Event\ParserApiCompleteEvent; use Frankspress\SgParserBundle\Event\SgParserEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class SgParseEmail implements EventSubscriberInterface { public static function getSubscribedEvents() { return [ SgParserEvents::PARSER_API => 'onParserApiComplete' ]; } public function onParserApiComplete(ParserApiCompleteEvent $event ) { $email = $event->getNewEmail(); $attachments = $event->getNewAttachment(); // SAVE YOUR EMAIL/ATTACHMENTS } }
默认配置
要修改任何默认参数,创建一个配置文件并更改以下任何一项。例如,如果您想让包只允许特定的 mime 类型,则可以在 mime_types
中列出它们。
# app/config/sg_parser.yml frankspress_sg_parser: email: max_body_length: 6000 # Returns the email body or response body without the reply history and tags. raw_response: false # Returns the subject without RE:, FWD: etc raw_subject: false attachment: # Enables or disables attachment. handle_attachment: true # Searches for php injection in every attachment. php_injection: false file_upload_size: '6M' # Lists the mime types allowed. If empty or commented out "all" mime types will be allowed. # mime_types: [ 'image/*', # 'application/pdf', # 'application/msword', # 'text/plain' # ]
基本示例
// ... public function onParserApiComplete(ParserApiCompleteEvent $event ) { $email = $event->getNewEmail(); $attachments = $event->getNewAttachment(); /* $email->getAttachment(); $email->getSubject(); $email->getSenderEmail(); $email->getSenderName(); $email->getBody(); */ if ( !empty($email->getAttachment() ) ) { foreach ($attachments as $attachment ) { /* $attachment->getFile(); $attachment->getFileName(); $attachment->getOriginalFileName(); $attachment->error(); $attachment->getFilePath(); $attachment->getSize(); $attachment->getType(); */ } } }
方法基本上是自我解释的。 $email->getAttachments()
返回一个包含附件标题的数组,而不是实际的附件。
$attachments
以附件数组的形式出现。从上面的示例中,您可以看到对于每个附件,您可以使用 $attachment->getFile()
加载其实际文件,或者使用 $attachment->getFileName()
获取其文件名。关于 $attachment->error()
的重要注意事项,如果它为空,则文件已通过验证;如果不为空,则仅记录遇到的第一个错误。