thebeaverhead/cakephp-email-cron

CakePHP的邮件定时任务插件

安装: 269

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 0

开放问题: 0

类型:cakephp-plugin

1.0.4 2022-03-23 15:42 UTC

This package is auto-updated.

Last update: 2024-09-23 21:25:40 UTC


README

该插件允许您创建邮件定时任务通知。让我们假设您向主题的所有者发送关于主题新评论的邮件。如果用户在一个主题中发表了5条评论,用户可能会收到5封邮件,这可能会让人感到烦恼。

此插件允许为这种情况创建一个队列,并在这些评论的创建日期之间不超过30秒(可配置)时发送一封包含5条新评论的邮件。

安装

composer require thebeaverhead/cakephp-email-cron

设置

在您的应用程序控制台

bin/cake plugin load EmailCron

应用迁移

bin/cake migrations migrate -p EmailCron

使用方法

创建要发送的邮件(不递增)

$emailCron = new EmailCron();
$emailCron
    ->setTo('example@mail.com')
    ->setTemplate('email/template/path')
    ->setSubject('Email subject')
    ->setSendDate(1580108540)
    ->setData([
        'topic' => [
            'title' => 'foo'
        ], 
        'comments' => [
            ['text' => 'foo'], 
            ['text' => 'bar']
        ]
    ])
    ->add();

创建要发送的递增邮件

$emailCron = new EmailCron();
$emailCron
    ->setTo('example@mail.com')
    ->setTemplate('email/template/path')
    ->setSubject('Email subject')
    ->setSendDate(1580108540)
    ->setData([
        'topic' => [
            'title' => 'foo'
        ],
    ])
    ->setIncrementData([
        'comments' => [
            ['text' => 'foo'], 
            ['text' => 'bar']
        ]
    ])
    ->addIncrement();

// Increment notification has been created
// in 10 sec new comment has been added to the same topic
sleep(10);

$emailCron
    ->setIncrementData([
        'comments' => [
            ['text' => 'baz'], 
        ]
    ])
    ->addIncrement();

创建email template.ctp

<?php
/**
 * @var \App\View\AppView $this 
 * @var array $comments
 * @var \App\Model\Entity\Comment $comment
 * @var \App\Model\Entity\Topic $topic
 */
?>

Hi, your topic <?= $topic['title'] ?> has new comment(s):
<ul>
  <?php foreach ($comments as $key => $comment): ?>
    <li>
      Comment #<?= $key ?>
      <?= $comment['text'] ?>
    </li>
  <?php endforeach; ?>
</ul>

要发送邮件,请运行

bin/cake EmailCron run

实际上,您需要将此命令添加到crontab中。

配置

Shell EmailCron支持配置EmailCron.send_delay(以秒为单位,默认为30秒)。如果之前的邮件在30秒内已被创建或更新,则不会发送增量邮件。

您可以在config/app.php中添加:

'EmailCron' => [
  'send_delay' => 60
],