Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Lago committed Feb 29, 2024
0 parents commit 8f6a746
Show file tree
Hide file tree
Showing 26 changed files with 2,118 additions and 0 deletions.
70 changes: 70 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea

# CMake
cmake-build-debug/
cmake-build-release/

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
### Composer template
composer.phar
composer.lock
/vendor/

# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
### macOS template
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
.phpunit.result.cache
176 changes: 176 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# Brevo suite for Laravel

## What is it?

A complete [Brevo](https://www.brevo.com/) suite for Laravel.

It provides the following features

- Laravel native mail transport.
- Transactional template transport.
- Transactional SMS transport.
- Native Brevo services (Contacts, Marketing, Accounts, Sales, etc).


## Installation

For Laravel 10.x

composer require juanparati/brevosuite

For older Laravel versions check [Sendinblue v3 for Laravel](https://github.com/juanparati/Sendinblue).


1. Add the following configuration snippet into the "config/services.php" file

'brevo' => [
'key' => '[your api key]'
],

2. Change the mail driver to "brevo" into the "config/mail.php" file or the ".env" file (Remember that ".env" values will overwrite the config values). Example:

'driver' => env('MAIL_MAILER', 'brevo'),

'mailers' => [
// ...
'brevo' => [
'transport' => 'brevo'
]
// ...
];


## Usage

### Transactional mail transport

Just use the transactional e-mails using the [Laravel Mail facade](https://laravel.com/docs/8.x/mail#sending-mail).


As soon that Brevo was configured as native mail transport you can use the following code in order to test it:

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


### Transactional mail template transport

The transactional mail template transport allow to send templates as transactional e-mails using Brevo.

It's possible to register the mail template transport facade into the "config/app.php":

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

Now it's possible to send templates in the following way:

MailTemplate::to('[email protected]'); // Recipient
MailTemplate::cc('[email protected]'); // CC
MailTemplate::bcc('[email protected]'); // BCC
MailTemplate::replyTo('[email protected]'); // 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

It's possible to reset the template message using the "reset" method:

MailTemplate::to('[email protected]'); // Recipient
MailTemplate::cc('[email protected]'); // Second recipient
MailTemplate::attribute('TYPE', 'Invoice'); // Replace %TYPE% placeholder
MailTemplate::send(100); // Send template
MailTemplate::to('[email protected]'); // 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('[email protected]');
MailTemplate::send(100); // Send template but previous attribute and second recipient is not used.

It's also possible enclose the mail message into a closure so the call to the "reset" method is not necessary:

MailTemplate::send(100, function ($message) {
$message->to('[email protected]');
// 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']);
...
});


### Transactional SMS

The transactional SMS allow to send SMS using the Brevo SMS transport.

I's possible to register the SMS transport facade into the "config/app.php":

'SMS' => Juanparati\BrevoSuite\Facades\Sms::class,

Usage examples:

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();
Like the transactional template transport, it is also possible reset the state using the "reset" method or just using a closure:

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

### Laravel notifications

The following classes are provided as message builder for Laravel notifications:

- TemplateMessage
- SmsMessage


### API Client

By default, this library uses the official [GetBrevo PHP library](https://github.com/getbrevo/brevo-php).

In order to interact with the official library it's possible to inject the custom APIs in the following way:

// 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);

Another example using Sendinblue models:

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

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

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

See the [GetBrevo PHP library](https://github.com/getbrevo/brevo-php) for more details.


### Supported by

This project was made possible by [Matchbanker.no](https://matchbanker.no/).
60 changes: 60 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "juanparati/brevosuite",
"description": "Complete Brevo integration with Laravel",
"type": "library",
"keywords": [
"brevo",
"getbrevo",
"sendinblue",
"php",
"laravel",
"mail",
"email",
"sms"
],
"require": {
"php": ">=8.1",
"laravel/framework": "^10.0.0",
"illuminate/mail": "^10.0.0",
"getbrevo/brevo-php": "~v1.0.2",
"symfony/brevo-mailer": "~7.0.3",
"symfony/http-client": "^7.0"
},
"require-dev": {
"phpunit/phpunit": "^10.0.7",
"orchestra/testbench": "^8.21"
},
"autoload": {
"psr-4": {
"Juanparati\\BrevoSuite\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Juanparati\\BrevoSuite\\Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
"Juanparati\\BrevoSuite\\Providers\\BrevoSuiteProvider"
]
}
},
"license": "MIT",
"authors": [
{
"name": "Juan Lago",
"email": "[email protected]"
}
],
"minimum-stability": "stable",
"scripts": {
"lint": [
"@php vendor/bin/phpstan analyse"
],
"test": [
"@php vendor/bin/phpunit"
]
}
}
13 changes: 13 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/test</directory>
</testsuite>
</testsuites>
<php>
<env name="BREVO_API_KEY" value="" force="true"/>
<env name="SINK_RECIPIENT" value="[email protected]" force="true"/>
<env name="SINK_SMS_RECIPIENT" value="" force="true"/>
</php>
</phpunit>
Loading

0 comments on commit 8f6a746

Please sign in to comment.