juanparati/sendinblue

为 Laravel 提供 Sendinblue v3 接口的服务器

10.0 2023-05-07 18:28 UTC

This package is auto-updated.

Last update: 2024-08-29 10:32:15 UTC


README

这是什么?

一个 Laravel 扩展包,提供如下的交易功能

  • Laravel 原生邮件传输
  • 交易模板传输
  • 交易短信传输

注意

Sendinblue 已更名为 Brevo。对于新安装的 Laravel 10+,请使用 Brevo Suite 库

安装

对于 Laravel 10.x

  composer require juanparati/sendinblue "^10.0"

对于 Laravel 9.x

  composer require juanparati/sendinblue "^9.0"

对于 Laravel 8.x

  composer require juanparati/sendinblue "^8.0"

对于 Laravel 7.x

  composer require juanparati/sendinblue "^4.0"

对于 Laravel 6.x

  composer require juanparati/sendinblue "^3.0"

对于 Laravel 5.5 到 5.8

  composer require juanparati/sendinblue "^2.4"

对于 Laravel 5.5 及以下版本,需要在 "config/app.php" 中注册服务提供者

    Juanparati\Sendinblue\ServiceProvider::class,

对于 Laravel 5.6 及以上版本,服务提供者将自动注册

在 Laravel 7+ 中设置原生邮件传输

  1. 将以下配置片段添加到 "config/services.php" 文件中

      'sendinblue' => [        
             'v3'    => [
                 'key'   => '[your v3 api key]'                    
             ]
      ],
    
  2. 在 "config/mail.php" 文件或 ".env" 文件中将邮件驱动更改为 "sendinblue.v3"(请注意,".env" 的值将覆盖配置值)。示例

      'driver' => env('MAIL_MAILER', 'sendinblue'),
    
      'mailers' => [
              // ...
              'sendinblue' => [
                     'transport' => 'sendinblue.v3'
              ]
              // ...
      ];
    
  3. 将以下配置片段添加到 "config/services.php" 文件中

      'sendinblue' => [        
             'v3'    => [
                 'key'   => '[your v3 api key]'                    
             ]
      ],
    

在 Laravel 5.x/6.x 中设置原生邮件传输

  1. 将以下配置片段添加到 "config/services.php" 文件中

      'sendinblue' => [        
             'v3'    => [
                 'key'   => '[your v3 api key]'                    
             ]
      ],
    
  2. 在 "config/mail.php" 文件或 ".env" 文件中将邮件驱动更改为 "sendinblue.v3"(请注意,".env" 的值将覆盖配置值)文件。示例

      'driver' => env('MAIL_DRIVER', 'sendinblue.v3'),
    

使用方法

交易邮件传输

只需使用 Laravel Mail 门面发送交易电子邮件。

一旦 Sendinblue 被配置为原生邮件传输,您可以使用以下代码进行测试

// Paste this code inside "artisan tinker" console.
Mail::raw('Test email', function ($mes) { 
    $mes->to('[youremail@example.tld]'); 
    $mes->subject('Test'); 
});

交易邮件模板传输

交易邮件模板传输允许使用 Sendinblue 发送模板作为交易电子邮件。

您可以将邮件模板传输门面注册到 "config/app.php" 中

     'MailTemplate' => Juanparati\Sendinblue\Facades\Template::class,

现在您可以以以下方式发送模板

    MailTemplate::to('user@example.net');           // Recipient
    MailTemplate::cc('user2@example.net');          // CC
    MailTemplate::bcc('user3@example.net');         // BCC
    MailTemplate::replyTo('boss@example.net');      // ReplyTo
    MailTemplate::attribute('NAME', 'Mr User');     // Replace %NAME% placeholder into the template 
    MailTemplate::attach('file.txt');               // Attach file
    MailTemplate::attachURL('http://www.example.com/file.txt'); // Attach file from URL
    MailTemplate::send(100);                        // Send template ID 100 and return message ID in case of success

您可以使用 "reset" 方法重置模板消息

    MailTemplate::to('user@example.net');           // Recipient
    MailTemplate::cc('user5@example.net');          // Second recipient
    MailTemplate::attribute('TYPE', 'Invoice');     // Replace %TYPE% placeholder
    MailTemplate::send(100);                        // Send template
    
    MailTemplate::to('user2@example.net');          // Another recipient
    MailTemplate::send(100);                        // Send template but attribute "type" and second recipient from previous e-mail is used
    
    MailTemplate::reset();                          // Reset message
    
    MailTemplate::to('user3@example.net');          
    MailTemplate::send(100);                        // Send template but previous attribute and second recipient is not used.

您也可以将邮件消息包含在闭包中,这样就不需要调用 "reset" 方法

    MailTemplate::send(100, function ($message) {
        $message->to('user2@example.net');
        
        // Note: Your template should contains the placeholder attributes surrounded by "%" symbol.
        // @see: https://help.sendinblue.com/hc/en-us/articles/209557065-Customize-transactional-email-templates
        $message->attributes(['placeholder1' => 'one', 'placeholder2' => 'two']);
        ...
    });        

交易短信

交易短信允许使用 Sendinblue SMS 传输发送短信。

您可以将 SMS 传输门面注册到 "config/app.php" 中

    'SMS' => Juanparati\Sendinblue\Facades\SMS::class,

使用示例

    SMS::sender('TheBoss');         // Sender name (Spaces and symbols are not allowed)
    SMS::to('45123123123');         // Mobile number with internal code (ES)
    SMS::message('Come to work!');  // SMS message
    SMS::tag('lazydev');            // Tag (Optional)
    SMS::webUrl('http://example.com/endpoint'); // Notification webhook (Optional);
    SMS::send();

与交易模板传输类似,您也可以使用 "reset" 方法重置状态,或者直接使用闭包

    SMS::send(function($sms) {
        $sms->to('45123123123');
        $sms->sender('Mr Foo');
        $sms->message('Hello Mr Bar');
        ...
    });

Laravel 通知

以下类被提供作为 Laravel 通知的消息构建器

  • TemplateMessage
  • SMSMessage

API 客户端

默认情况下,此库使用官方的 Sendinblue PHP 库

为了与官方库交互,您可以以下方式注入自定义 API

    // Obtain APIClient
    $api_client = app()->make(\Juanparati\Sendinblue\Client::class);
    
    // Use the APIClient with the Sendinblue ContactsAPI
    $contacts_api = $api_client->getApi('ContactsApi');
    
    // Retrieve the first 10 folders
    $folders = $contacts_api->getFolders(10, 0);  

使用 Sendinblue 模型的另一个示例

    $apiClient = app()->make(\Juanparati\Sendinblue\Client::class);
    $contactsApi = $apiClient->getApi('ContactsApi');

    // Use CreateContact model
    $contact = $apiClient->getModel('CreateContact', ['email' => 'test@example.net', 'attributes' => ['TYPE' => 4, 'NOM' => 'test', 'PRENOM' => 'test'], 'listIds' => [22]]);

    try {
            $contactsApi->createContact($contact);
    }
    catch(\Exception $e){
            dd($e->getMessage());
    }

有关详细信息,请参阅 Sendinblue v3 API

支持者

此项目得以实现得益于 Matchbanker.no