netcore/module-email

此包的最新版本(v1.0.6)没有提供许可证信息。

安装量: 1,130

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 6

分支: 0

开放问题: 1

类型:模块

v1.0.6 2018-07-04 08:22 UTC

This package is not auto-updated.

Last update: 2024-09-19 09:55:32 UTC


README

此模块是为了便于管理自动化邮件和邮件活动而制作的。

安装前

此包是Netcore CMS生态系统的一部分,仅在已安装以下包的项目中功能正常

  1. https://github.com/netcore/netcore
  2. https://github.com/netcore/module-admin
  3. https://github.com/netcore/module-translate
  4. https://github.com/netcore/module-user
  5. https://github.com/netcore/module-setting

安装

  • 使用composer安装此包
    composer require netcore/module-email
  • 发布 assets/configuration/migrations
    php artisan module:publish Email
    php artisan module:publish-config Email
    php artisan module:publish-migration Email
    php artisan migrate
  • 将自动化邮件命令添加到 "app/Console/Kernel.php" 中的调度
    $schedule->command('automated-emails:send')->everyMinute();
  • 将队列驱动设置为redis

  • 在您的supervisor配置中设置 --timeout 为 3600 或更大,如果项目将向大量用户发送活动

  • Modules\User\Traits\ReplaceableAttributes 特性添加到您的 User 模型中,并设置公共属性 $replaceable,您希望在邮件模板中动态使用的属性

public $replaceable = [
    'first_name',
    'last_name',
    'email'
];

并将 $replaceablePrefix 设置为可替换属性的名称前缀。

  • 将 "getFilters" 和 "getFilterQuery" 方法添加到User模型中,如下所示
    public function getFilters()
    {
        return [
            'is_email_verified' => [
                'name'   => 'Email verified?',
                'type'   => 'select', // Available types: text, select, multi-select, from-to
                'values' => [-1 => 'Not important', 1 => 'Yes', 0 => 'No']
            ],
            'country'           => [
                'name'   => 'Country',
                'type'   => 'multi-select', // Available types: text, select, multi-select, from-to
                'values' => Country::all()->mapWithKeys(function ($country) {
                    return [
                        $country->id => $country->name
                    ];
                })
            ],
        ];
    }
    
    public function getFilterQuery()
    {
        $filters = request()->get('filters', []);
        $query = User::select('id', 'email');
        
        foreach ($this->getFilters() as $field => $filter) {
            $data = (isset($filters[$field])) ? $filters[$field] : -1;
            
            if ($data < 0) {
                continue;
            }
    
            if ($filter['type'] == 'multi-select') {
                $query->whereHas($field, function ($q) use ($data) {
                    $q->whereIn('id', $data);
                });
            } elseif ($filter['type'] == 'from-to') {

                if (($data['from'] && $data['to']) && $data['to'] > $data['from']) {
                    $query->whereBetween($field, [$data['from'], $data['to']]);
                }

            } else {
                $query->where($field, $data);
            }
        }
    
        return $query;
    }

配置

  • 配置文件位于 config/netcore/module-email.php

自动邮件播种

   use Modules\Email\Models\AutomatedEmail;
   use Netcore\Translator\Helpers\TransHelper;
   
   $emails = [
        [
            'key'          => 'verify_email',
            'period'       => 'now',
            'type'         => 'static', // Available types: static, period, interval
            'is_active'    => true,
            'translations' => [
                'name' => 'Email verification',
                'text' => 'Please verify your email by clicking on the link: <a href="[VERIFICATION_URL]">[VERIFICATION_URL]</a>'
            ]
        ]
    ];
   
    foreach ($emails as $email) {
        $emailModel = AutomatedEmail::create(array_except($email, 'translations'));
   
        $translations = [];
        foreach (TransHelper::getAllLanguages() as $language) {
            $translations[$language->iso_code] = $email['translations'];
        }
        
        $emailModel->updateTranslations($translations);
    }

使用

  • 将电子邮件添加到/从订阅列表中
    email()->subscribe('example@example.com');
    email()->unsubscribe('example@example.com');
  • 发送自动化邮件
    email()->send('verify_email', $user, [
        'VERIFICATION_URL' => $verificationUrl
    ]);