frankspress/sg-parser-bundle
Composer 安装命令:
composer require frankspress/sg-parser-bundle
包简介
Parses and validates SendGrid inbound email content sent to the WebHook - Symfony 5
README 文档
README
Parses and validates SendGrid email/attachments sent to the WebHook ( Symfony 5 )
Requirements
- PHP 7.1.3 or higher;
- Symfony 5.*.
Installation
composer require frankspress/sg-parser-bundle
Usage
Create a new route and set the prefix to configure your new Webhook point.
# app/routes/sg_api_parser.yml sg_api_parsers: resource: '@FrankspressSgParserBundle/Resources/config/routes.xml' prefix: '/api/sendgrid/inbound' trailing_slash_on_root: false
Create a new EventSubscriber. This will automatically be called when a new email is sent to your Api point.
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 } }
Default Config
To modify any of the default parameters create a config file and change any of the following.
For instance, if you want the bundle to allow only specific mime types then you can list them in 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' # ]
Basic Example
// ... 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(); */ } } }
Methods are pretty much self explanatory. $email->getAttachments() returns an array with the attachment titles not the actual attachments.
$attachments comes as an array of attachments. From the example above you can see that for each attachment you can load its actual file with $attachment->getFile() or get it's filename with $attachment->getFileName(). One important note about $attachment->error(), if this is empty the file has passed validation, if it's not empty then only the first encountered error will be recorded.
frankspress/sg-parser-bundle 适用场景与选型建议
frankspress/sg-parser-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 03 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 frankspress/sg-parser-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 frankspress/sg-parser-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-28