juanparati/brevosuite

完整的 Brevo 集成到 Laravel 中

11.0.1 2024-04-22 08:26 UTC

This package is auto-updated.

Last update: 2024-09-22 09:23:45 UTC


README

这是什么?

一个完整的Brevo套件,适用于Laravel。

它提供以下功能

  • Laravel 原生邮件传输。
  • 事务性模板传输。
  • 事务性短信传输。
  • 原生 Brevo 服务(联系人、营销、账户、销售等)。

安装

适用于 Laravel 11.x

  composer require juanparati/brevosuite "^11.0"

适用于 Laravel 10.x

  composer require juanparati/brevosuite "^10.0"

对于更旧的 Laravel 版本,请检查Sendinblue v3 for Laravel

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

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

      'driver' => env('MAIL_MAILER', 'brevo'),
    
      'mailers' => [
          // ...
          'brevo' => [
              'transport' => 'brevo'
          ]
          // ...
      ];
    

使用方法

事务性邮件传输

只需使用Laravel Mail 门面的事务性电子邮件。

一旦 Brevo 被配置为原生邮件传输,就可以使用以下代码来测试它

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

事务性邮件模板传输

事务性邮件模板传输允许使用 Brevo 发送模板作为事务性电子邮件。

现在可以在 "config/app.php" 中注册邮件模板传输门面

    'MailTemplate' => Juanparati\BrevoSuite\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.brevo.com/hc/en-us/articles/360000268730-How-to-customize-your-transactional-emails
        $message->attributes(['placeholder1' => 'one', 'placeholder2' => 'two']);
        ...
    });        

事务性短信

事务性短信允许使用 Brevo SMS 传输发送短信。

现在可以在 "config/app.php" 中注册 SMS 传输门面

    'SMS' => Juanparati\BrevoSuite\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 客户端

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

要与应用程序库交互,可以按以下方式注入自定义 API

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

使用 Sendinblue 模型的另一个示例

    $apiClient = app()->make(\Juanparati\BrevoSuite\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());
    }

有关更多详细信息,请参阅GetBrevo PHP 库

支持

此项目由Matchbanker.no支持。