lecodeurdudimanche/laravel-email-listener

一个简单的库,用于在收到电子邮件时执行操作,设计用于与Laravel一起使用

2.0.1 2019-11-19 21:15 UTC

This package is auto-updated.

Last update: 2024-09-20 09:01:16 UTC


README

Build status GitHub license Package version Downloads

一个简单的库,用于在收到电子邮件时执行操作,设计用于与Laravel一起使用

安装

通过composer

composer require lecodeurdudimanche/laravel-email-listener

您需要安装并启用一些php扩展(php-imapphp-mbstringphp-mcrypt)。在Ubuntu上

sudo apt install php*-imap php*-mbstring php*-mcrypt

配置

您需要设置您的IMAP或POP账户的配置。首先运行以下命令以将配置发布到您的config文件夹

php artisan vendor:publish --provider="Webklex\IMAP\Providers\LaravelServiceProvider"

接下来,修改accounts配置以添加您自己的账户。

此库使用laravel-imap进行IMAP和POP通信,因此如果您需要更多关于IMAP和POP配置的帮助,请查看他们的文档

基本用法

在您的App\Console\Kernel类的schedule()方法中,添加以下行

$schedule->call(new EmailListener)->everyfiveMinutes(); // or any other [frequency option](https://laravel.net.cn/docs/5.7/scheduling#schedule-frequency-options)

这将导致EmailListener从配置文件(其路径必须在email-listener.config-file配置值中提供)加载操作和过滤器,然后运行。

为了以编程方式创建配置或在不加载任何内容的情况下使用库,您需要注册Action。首先,您需要创建一个包含要执行的回调和确定哪个电子邮件将触发该操作的过滤器Action对象。

$action = (new Action())
    ->filter((new EmailFilter())
        ->from("an.adress@example.com")
        )
    ->callback($callback);
// callback can be a closure, a array with class and method or a string with format 'class@method'
// Tou can also use the constructor
$action = new Action(
    (new EmailFilter())->from("an.adress@example.com"),
    $callback);

现在,您可以将其绑定到EmailListener

$emailListener = (new EmailListener())
    ->addAction('imap_client', $action);

// Where 'imap_client' is the name of your IMAP or POP account in config/imap.php file

然后运行它或保存它。

$emailListener->run();
$emailListener->save();

例如,这是将操作添加到现有监听器并保存它的代码

(new EmailListener())
    ->load()
    ->addAction('imap_client', $action)
    ->save();

您可以使用Filter::load()函数从JSON中单独加载过滤器,并使用Filter::save()函数保存它们,如下所示

// You must either set config("email-listener.config-file") to the path of your JSON file
// or pass the path as the second argument of Filter::load
$filter = Filter::load('fancyFilterName', 'path/to/file.json');
$filter->save('path/to/file.json');
// You can also save the filter with another name like so :
$filter->saveAs('another name', 'path/to/file.json');

预期格式的描述

  {
      "actions" : [
        {
            "client" : <client_name>,
            "filter" : <filter_name>,
            "callback" : "class@method"
        },
        ...
      ],
      "filters" : [
          <filterName> : {
            "type": "email" | "attachment",
            "filters": [
              {
                  "method" : <filter>,
                  "args" : <argument> | <array of arguments>
              },
              ...
            ]
            ("attachments" : <filterDescription>)(only if type == "email", optional)
          },
          ...
      ]
}

示例可以在测试目录中找到。

完整API

即将推出