neyric/inbound-email-bundle

支持 Sendgrid 和 Mailjet 的 Symfony 应用程序入站电子邮件

安装: 5,689

依赖项: 0

建议者: 0

安全: 0

星标: 1

关注者: 1

分支: 1

开放问题: 1

类型:symfony-bundle

v1.3.0 2020-07-21 09:21 UTC

This package is auto-updated.

Last update: 2024-09-21 18:39:33 UTC


README

支持 Sendgrid 和 Mailjet 的 Symfony 应用程序入站电子邮件。

原理

此捆绑包为您的应用程序提供标准化的 InboundEmailEvent

此外,使用 willdurand/email-reply-parser 解析电子邮件回复,该工具将移除引用文本和签名中的文本内容。

由可配置的 webhook 处理器控制器触发 InboundEmailEvent,当前支持两个电子邮件网关

需求

要使用此捆绑包,您至少需要以下内容

  • PHP v7.1
  • Symfony >= 4.3
  • Sendgrid 或 Mailjet 账户

使用 Symfony Flex 的应用程序

打开命令行,进入您的项目目录并执行

$ composer require neyric/inbound-email-bundle

未使用 Symfony Flex 的应用程序

步骤 1:下载捆绑包

打开命令行,进入您的项目目录并执行以下命令以下载此捆绑包的最新稳定版本

$ composer require neyric/inbound-email-bundle

此命令要求您全局安装 Composer,请参阅 Composer 文档中的安装章节

步骤 2:启用捆绑包

然后,通过将其添加到项目中 app/AppKernel.php 文件中注册的捆绑包列表中来启用捆绑包

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new \neyric\InboundEmailBundle\NeyricInboundEmailBundle(),
        ];

        // ...
    }

    // ...
}

Sendgrid 配置

首先,您需要设置 webhook 处理器。在您的 routes.yaml 文件中

neyric_inbound_email_sendgrid:
    path: /inbound_email/sendgrid/hook_handler # You can customize
    controller: neyric\InboundEmailBundle\Controller\SendgridController::hookHandlerAction

如果您使用 symfony/expression-language 组件,建议您添加以下行以限制此路由只能使用 POST 方法

    condition:  "context.getMethod() in ['POST']"

然后,设置 Sendgrid Inbound Parse Webhook:参阅 https://sendgrid.com/docs/for-developers/parsing-email/setting-up-the-inbound-parse-webhook/

Mailjet 配置

首先,您需要设置 webhook 处理器。在您的 routes.yaml 文件中

neyric_inbound_email_mailjet:
    path: /inbound_email/mailjet/hook_handler
    controller: neyric\InboundEmailBundle\Controller\MailjetController::hookHandlerAction

如果您使用 symfony/expression-language 组件,建议您添加以下行以限制此路由只能使用 POST 方法

    condition:  "context.getMethod() in ['POST']"

然后,设置 Mailjet 解析 API Webhook:参阅 https://dev.mailjet.com/email/guides/parse-api/

应用程序使用

要在应用程序中接收电子邮件,只需创建一个 EventSubscriber 类,该类侦听 InboundEmailEvent::class 事件

use neyric\InboundEmailBundle\Event\InboundEmailEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class MyInboundEmailSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            InboundEmailEvent::class => ['onInboundEmail', 10], // 10 = priority
        ];
    }

    public function onInboundEmail(InboundEmailEvent $event)
    {
        // ... do something with $event
        // Check https://github.com/neyric/inbound-email-bundle/blob/master/Event/InboundEmailEvent.php for reference

        // If this subscriber can handle this event, it is recommended to stop the propagation
        // This will prevent other subscribers with lower priorities to be executed,
        // allowing event-based routing of your incoming emails.
        $event->stopPropagation();
    }
}

然后使用 kernel.event_subscriber 注册服务(如有必要,根据您的服务配置)

    App\Event\MyInboundEmailSubscriber:
        class: App\Event\MyInboundEmailSubscriber
        tags: ['kernel.event_subscriber']

准备本地隧道

使用本地隧道可以节省大量时间,因为您可以在本地进行测试。推荐选择 ngrok。Ngrok 是一种工具,可以将我们的本地服务器隧道到网络上,使我们的本地 webhook 处理器可供电子邮件提供商的 webhook 使用。

许可证

neyric/inbound-email-bundle 在 MIT 许可证下分发,请参阅 LICENSE 文件

联系信息

使用 GitHub 上的问题跟踪器 报告错误或建议功能。