twindots/email-service

此软件包最新版本(v1.2.0)没有可用的许可信息。

编译短代码并发送预定义模板的电子邮件。

v1.2.0 2021-05-29 13:17 UTC

This package is auto-updated.

Last update: 2024-09-29 05:48:45 UTC


README

此软件包简化了编译短代码和发送电子邮件的过程。

要求

安装

使用 composer 安装此软件包

$ composer require twindots/email-service

服务提供者和外观将由 laravel 自动发现。 发布配置文件和视图文件夹: (必需)

$ php artisan vendor:publish --provider="TwinDots\EmailService\EmailServiceProvider"

这将发布以下内容

  • config/email_service.php
  • views/email_service/views.blade.php
  • views/email_service/layout.blade.php
  • views/email_service/partials/

使用方法

此软件包包含 2 个类:EmailService 和 EmailShortCodes。

1- EmailShortCodes

  • 导入库
   use EmailShortCodes; 
  • 在函数中加载它
   public function compileCodes( EmailShortCodes $shortcodes )
   { 
      // Or create a new instance
      // $shortcodes = new EmailShortCodes();

      $compiled = $shortcodes->objects([
                  'user' => $user,
               ])
               ->withUser()
               ->body( request('body') )
               ->compile(); 
   }

2- EmailService

  • 导入库
   use EmailService; 
  • 在函数中加载它
   public function sendEmail( EmailService $emailService )
   { 
      // Or create a new instance
      // $emailService = new EmailService();

      $result = $emailService->email(['email1@example.com', 'email2@example.com'])
                  ->subject( $subject )
                  ->body( $compiled ) // Send the compiled body or any html
                  ->attach([
                     'file-1.png' => 'path/to/file-1.png',
                     'file-2.pdf' => 'path/to/file-2.png'
                  ])
                  ->send();
   }
  • 此库将使用文件 email_service/view.blade 作为电子邮件模板,您可以从配置文件中更改它。
  • send() 函数将返回一个包含以下内容的结果数组
    • 状态(布尔值):True 表示成功,false 表示发送失败。
    • 消息(文本):显示发送失败的消息。

短代码

短代码在配置文件 config/email_service.php 中的 shortcodes 数组内定义。您可以在不同的电子邮件模板中定义短代码组。

   'group_name' => [
     'short_code_1' => [...],
     'short_code_2' => [...],
   ]   

短代码可以使用此命令插入到您喜欢的文本编辑器中

 {shortcode_unique_name}

提示:此库将编译大括号 {} 内的任何内容

您可以添加 3 种类型的短代码

  • 变量:
  'user_first_name' => [        // shortcode unique name
     'title' => 'First name',   // shortcode friendly name
     'type' => 'variable',      // type is variable 
     'object' => 'user',        // object can be any class, ex: $user
     'param' => 'first_name'    // parameter, ex first_name: $user->first_name
  ], 
  • 函数:
  'user_full_name' => [        // shortcode unique name
     'title' => 'Full name',   // shortcode friendly name
     'type' => 'function',     // type is function 
     'object' => 'user',       // object can be any class, ex: $user
     'param' => 'getFullName'  // parameter, ex: getFullName: $user->getFullName()
  ], 
  • Blade 视图:
  'user_image' => [                      // shortcode unique name
     'title' => 'User image',            // shortcode friendly name
     'type' => 'view',                   // type is view 
     'object' => 'users.profile-image',  // object is the view path
  ], 

对于 blade 视图短代码,您不需要传递任何对象,因为它将继承从 $shortcodes->objects() 函数传递的对象。

如何检查所需的对象

如果您在一个模板中工作,该模板包含大量短代码,并且有多个变量和视图,您需要知道所需的对象,只需调用函数 $shortcodes->objectsNeeded() 即可,它将返回一个数组,告诉您需要哪些对象。

参考

EmailShortCodes

EmailService

许可

MIT 许可证(MIT)。请参阅 许可文件 了解更多信息。