mailersend / laravel-driver
MailerSend Laravel Driver
Installs: 295 581
Dependents: 3
Suggesters: 0
Security: 0
Stars: 72
Watchers: 7
Forks: 21
Open Issues: 6
Requires
- php: >=8.0
- ext-json: *
- illuminate/support: ^9.0 || ^10.0 || ^11.0
- mailersend/mailersend: ^0.8.0 || ^0.23.0 || ^0.24.0
- nyholm/psr7: ^1.5
- php-http/guzzle7-adapter: ^1.0
- symfony/mailer: ^6.0 || ^7.0
Requires (Dev)
- orchestra/testbench: ^7.0 || ^9.0
- phpunit/phpunit: ^9.0 || ^10.5
- dev-main
- v2.7.0
- v2.6.1
- v2.6.0
- v2.5.0
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.0
- 1.x-dev
- v1.1.1
- v1.1.0
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0
- v0.1.1
- v0.1.0
- dev-feature/MSD-11239/remove-personalization
- dev-feature/issue-6473/update_to_PHP_8_3
- dev-feature/issue-44/support-for-failover
- dev-feature/issue-46/laravel-11-support
- dev-feature/no-ref/update-php-sdk-0-22-0
- dev-feature/issue-21-alternative-syntax
- dev-feature/issue-34/latest-php-sdk
This package is auto-updated.
Last update: 2024-11-28 10:08:30 UTC
README
MailerSend Laravel Driver
Table of Contents
Installation
Requirements
- Laravel 9.0+
- PHP 8.0+
- Guzzle 7.0+
- An API Key from mailersend.com
For Laravel 7.x - 8.x support see 1.x branch
Setup
You can install the package via composer:
composer require mailersend/laravel-driver
After that, you need to set MAILERSEND_API_KEY
in your .env
file:
MAILERSEND_API_KEY=
Add MailerSend as a Laravel Mailer in config/mail.php
in mailers
array:
'mailersend' => [ 'transport' => 'mailersend', ],
And set environment variable MAIL_MAILER
in your .env
file
MAIL_MAILER=mailersend
Also, double check that your FROM
data is filled in .env
:
MAIL_FROM_ADDRESS=app@yourdomain.com MAIL_FROM_NAME="App Name"
Usage
Old Syntax:
This is an example using the build mailable that you can use to send an email with.
app/Mail/TestEmail.php
namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Arr; use MailerSend\Helpers\Builder\Variable; use MailerSend\Helpers\Builder\Personalization; use MailerSend\LaravelDriver\MailerSendTrait; class TestEmail extends Mailable { use Queueable, SerializesModels, MailerSendTrait; public function build() { // Recipient for use with variables and/or personalization $to = Arr::get($this->to, '0.address'); return $this ->view('emails.test_html') ->text('emails.test_text') ->attachFromStorageDisk('public', 'example.png') // Additional options for MailerSend API features ->mailersend( template_id: null, tags: ['tag'], personalization: [ new Personalization($to, [ 'var' => 'variable', 'number' => 123, 'object' => [ 'key' => 'object-value' ], 'objectCollection' => [ [ 'name' => 'John' ], [ 'name' => 'Patrick' ] ], ]) ], precedenceBulkHeader: true, sendAt: new Carbon('2022-01-28 11:53:20'), ); } }
New Syntax:
This is an example using the new mailable syntax that you can use to send an email with.
app/Mail/TestEmail.php
<?php namespace App\Mail; use Carbon\Carbon; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailables\Attachment; use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Arr; use MailerSend\Helpers\Builder\Personalization; use MailerSend\Helpers\Builder\Variable; use MailerSend\LaravelDriver\MailerSendTrait; class TestEmail extends Mailable { use Queueable, SerializesModels, MailerSendTrait; /** * Create a new message instance. */ public function __construct() { // } /** * Get the message envelope. */ public function envelope(): Envelope { return new Envelope( subject: 'Test Email', ); } /** * Get the message content definition. */ public function content(): Content { $to = Arr::get($this->to, '0.address'); // Additional options for MailerSend API features $this->mailersend( template_id: null, tags: ['tag'], personalization: [ new Personalization($to, [ 'var' => 'variable', 'number' => 123, 'object' => [ 'key' => 'object-value' ], 'objectCollection' => [ [ 'name' => 'John' ], [ 'name' => 'Patrick' ] ], ]) ], precedenceBulkHeader: true, sendAt: new Carbon('2022-01-28 11:53:20'), ); return new Content( view: 'emails.test_html', text: 'emails.test_text' ); } /** * Get the attachments for the message. * * @return array<int, \Illuminate\Mail\Mailables\Attachment> */ public function attachments(): array { return [ Attachment::fromStorageDisk('public', 'example.png') ]; } }
We provide a MailerSendTrait
trait that adds a mailersend
method to the mailable and allows you to use additional options that are available through our API.
After creating the mailable, you can send it using:
use App\Mail\TestEmail; use Illuminate\Support\Facades\Mail; Mail::to('recipient@domain.com') ->cc('cc@domain.com') ->bcc('bcc@domain.com') ->send(new TestEmail());
Please refer to Laravel Mail documenation and MailerSend API documentation for more information.
Support and Feedback
In case you find any bugs, submit an issue directly here in GitHub.
If you have any troubles using our driver, feel free to contact our support by email info@mailersend.com
Official API documentation is at https://developers.mailersend.com