定制 hmones/laravel-digest 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hmones/laravel-digest

Composer 安装命令:

composer require hmones/laravel-digest

包简介

A simple package to create and send digest emails every certain period or when the amount reaches a certain threshold.

README 文档

README

Build Status Style CI Total Downloads Latest Stable Version License

A simple package to create and send digest emails every certain period or when the amount reaches a certain threshold. Usage Examples:

  • Sending a digest email to website administrator every new 100 registrations on the website.
  • Sending a daily email with logged error messages on the website.
  • Sending users a monthly newsletter that contains all posts issued in the month.

Installation

Via Composer

composer require hmones/laravel-digest

Configuration

To publish the package configuration

php artisan vendor:publish --tag=laravel-digest-config

The configuration file contains the following parameters:

  • method: the method that you would like to use to send emails, it takes two values, queue or send
    • Env variable: DIGEST_METHOD
    • Default value: queue
  • frequency.enabled whether you would like to enable sending emails every certain period, if not enabled emails will not be scheduled
    • Env variable: DIGEST_FREQUENCY_ENABLED
    • Default value: true
  • frequency.daily.time if frequency is enabled this parameter is used as the time to send the daily digest emails
    • Env variable: DIGEST_DAILY_TIME
    • Default value: 00:00
  • frequency.weekly.time if frequency is enabled this parameter is used as the time to send the weekly digest emails
    • Env variable: DIGEST_WEEKLY_TIME
    • Default value: 00:00
  • frequency.monthly.time if frequency is enabled this parameter is used as the time to send the monthly digest emails
    • Env variable: DIGEST_MONTHLY_TIME
    • Default value: 00:00
  • frequency.weekly.day if frequency is enabled this parameter is used as the day to send the weekly digest emails (1 is Sunday and 7 is Saturday)
    • Env variable: DIGEST_WEEKLY_DAY
    • Default value: 1
  • frequency.monthly.day if frequency is enabled this parameter is used as the day to send the monthly digest emails
    • Env variable: DIGEST_MONTHLY_DAY
    • Default value: 1
  • frequency.custom you can set as much custom frequency definitions as you want and the parameter takes a valid cron expression
  • amount.enabled whether you would like to enable sending emails every certain amount per batch
    • Env variable: DIGEST_AMOUNT_ENABLED
    • Default value: true
  • amount.threshold the number of emails after which an digest email should be sent.
    • Env variable: DIGEST_AMOUNT_THRESHOLD
    • Default value: 10

Usage

To create an email digest, make sure you have the following first:

  • A mailable, configured with the sending addresses and email views and subject.
  • The mailable should accept an array variable in its constructor, this array variable will contain all the records of data passed to individual emails concatenated and sent automatically by the package to the mailable to compile the views for sending the digest.

Example: Sending a digest email every time 10 new users register on the website with a summary of their names.

  1. Adjust the configuration variable in config\laravel-digest.php by setting amount.enabled => true and frequency.enabled => false
  2. Create the following mailable
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class UserCreatedMailable extends Mailable
{
    use Queueable, SerializesModels;

    public $data;

    public function __construct(array $data)
    {
        $this->data = $data;
    }

    public function build(): Mailable
    {
        return $this->view('userCreated')->subject('10 new users are registered')->to('email@test.com');
    }
}
  1. Create a view to render the names of the users resources\userCreated.blade.php
<html>
<head><title>Sample Email</title></head>
<body>
<h1>The following users have just registered:</h1>
<ol>
  @foreach($data as $record)
  <li>{{$record['name']}}</li>
  @endforeach
</ol>
</body>
</html>
  1. Create an observer for user creation and add an record to the digest every time a user is created:
<?php

namespace App\Observers;

use App\Mail\UserCreatedMailable;
use App\Models\User;
use Hmones\LaravelDigest\Facades\Digest;

class UserObserver
{
    public function created(User $user)
    {
        $batchId = 'userCreated';
        $mailable = UserCreatedMailable::class;
        //Data can also be set to null if you don't want to attach any data to the email
        $data = ['name' => $user->name];
        //Frequency can take values such as daily, weekly, monthly, custom or an integer threshold 10, 20 ...etc 
        $frequency = 10;
        
        Digest::add($batchId, $mailable, $data, $frequency);
    }
}
  1. The package will take care of everything else and send emails once the number of registered users reach 10.

Change log

Please see the changelog for more information on what has changed recently.

Testing

composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email author email instead of using the issue tracker.

Credits

License

Please see the license file for more information.

hmones/laravel-digest 适用场景与选型建议

hmones/laravel-digest 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 76.93k 次下载、GitHub Stars 达 158, 最近一次更新时间为 2022 年 02 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「LaravelDigest」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 hmones/laravel-digest 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hmones/laravel-digest 我们能提供哪些服务?
定制开发 / 二次开发

基于 hmones/laravel-digest 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 76.93k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 158
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 158
  • Watchers: 1
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-04