tigrov/yii2-email-reply

为 Yii2 提供邮件回复功能 - 将回复消息传递到预定义对象。

安装: 8

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放性问题: 0

类型:yii2-extension

1.0.4 2019-07-16 09:11 UTC

This package is auto-updated.

Last update: 2024-09-20 00:51:25 UTC


README

它提供帮助来配置 Yii2 与 Tigrov/email-reply 一起工作。

Tigrov/email-reply 库将来自电子邮件的回复消息传递到预定义对象。它使用 IMAP 连接到电子邮件服务器。

Latest Stable Version Build Status

限制

该库使用 ddeboer/imap,并且需要

  • PHP >= 7.1
  • 扩展 iconvIMAPmbstring

安装

安装此扩展的首选方式是通过 composer

运行以下命令:

php composer.phar require --prefer-dist tigrov/yii2-email-reply

或添加以下内容到您的 composer.json 文件的 require 部分:

"tigrov/yii2-email-reply": "~1.0"

使用

使用方法

首先阅读 Tigrov/email-reply/README

然后您可以按照以下步骤操作:

  1. 创建一个实现 ModelInterface 接口的 ActiveRecord 模型类,并实现 emailReply 方法(见 examples/Model.php)。

    class Model extends yii\db\ActiveRecord implements tigrov\emailReply\ModelInterface
    {
        use tigrov\yii2\emailReply\ActiveRecordThread;
    
        public function emailReply($message)
        {
            /** @var string $fromEmail email address of the sender */
            $fromEmail = $message->getFrom()->getAddress();
            
            /** @var string $fromName name of the sender */
            $fromName = $message->getFrom()->getName();
            
            /** @var string $content content from the replied message */
            $content = $message->getBodyHtml() ?: $message->getBodyText() ?: $message->getDecodedContent();
            
            // Parse the content to get only answer
            $content = EmailReplyParser::parseReply($content);
            
            // To do something with $content
            // e.g. add comment from $fromEmail to the object 
        }
    }
  2. EmailReply 添加到 Yii2 配置文件中。

    return [
        ...
        'emailReply' => [
            'class' => 'tigrov\emailReply\EmailReply',
            'classesMap' => [
                // key will be used as prefix for email address
                'm' => \Model::class,
            ],
        ],
        ...
    ];
  3. 使用特殊的回复电子邮件地址发送电子邮件消息。

    $replyEmail = \Yii::$app->emailReply->getReplyEmail($model);
    
    // Send an email to somebody using the reply email address
    $message = \Yii::$app->mailer->compose($view)
        ->setReplyTo([$replyEmail => \Yii::$app->name])
        ->setFrom([$replyEmail => \Yii::$app->name]);
     
    // Set message subject, body, recipients and etc
    ...
    
    $message->send();
  4. 创建控制台控制器。

    class EmailReplyController extends tigrov\yii2\emailReply\EmailReplyController
    {
    }
  5. 使用控制台命令 yii email-reply 读取您的邮箱。例如,作为 cron 任务

    15 * * * * yii email-reply

  6. 每条消息将被传递到 Model::emailReply($message),您可以在那里预先处理它们。

请参阅 examples 目录中的示例。

建议

您可以使用 willdurand/email-reply-parser 从电子邮件消息中解析仅回复文本。

$reply = EmailReplyParser::parseReply($content);

许可证

MIT