Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Update for Laravel 9 #19

Merged
merged 6 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: ['7.4','8.0']
php-versions: ['8.0', '8.1']
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}

steps:
Expand All @@ -39,4 +39,4 @@ jobs:
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run test suite
run: composer run-script test
run: composer run-script test
45 changes: 17 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ MailerSend Laravel Driver
# Table of Contents

* [Installation](#installation)
* [Upgrade and Guzzle 6 support](#upgrade)
* [Usage](#usage)
* [Support and Feedback](#support-and-feedback)
* [License](#license)
Expand All @@ -17,11 +16,13 @@ MailerSend Laravel Driver

## Requirements

- Laravel 7.0+
- PHP 7.4+
- Laravel 9.0+
- PHP 8.0+
- Guzzle 7.0+
- An API Key from [mailersend.com](https://www.mailersend.com)

**For Laravel 7.x - 8.x support see [1.x branch](https://github.com/mailersend/mailersend-laravel-driver/tree/1.x)**

## Setup

You can install the package via composer:
Expand Down Expand Up @@ -57,17 +58,10 @@ MAIL_FROM_ADDRESS=app@yourdomain.com
MAIL_FROM_NAME="App Name"
```

<a name="upgrade"></a>
# Upgrade and Guzzle 6 support

## Upgrading from v0.1

If you are upgrading from `v0.1` branches, please do note that you will need to upgrade Guzzle to atleast version 7. [Please consult official guide for more info](https://github.com/guzzle/guzzle/blob/master/UPGRADING.md).

<a name="usage"></a>
# Usage

This is an example [mailable](https://laravel.com/docs/7.x/mail#writing-mailables) that you can use to send an email with.
This is an example [mailable](https://laravel.com/docs/9.x/mail#writing-mailables) that you can use to send an email with.

`app/Mail/TestEmail.php`

Expand All @@ -88,22 +82,21 @@ class TestEmail extends Mailable

public function build()
{
// Recipient for use with variables and/or personalization
$to = Arr::get($this->to, '0.address');

return $this->view('emails.test_html')
return $this
->view('emails.test_html')
->text('emails.test_text')
->attachFromStorageDisk('public', 'example.png')
// Additional options for MailerSend API features
->mailersend(
// Template ID
null,
// Variables for simple personalization
[
template_id: null,
variables: [
new Variable($to, ['name' => 'Your Name'])
],
// Tags
['tag'],
// Advanced personalization
[
tags: ['tag'],
personalization: [
new Personalization($to, [
'var' => 'variable',
'number' => 123,
Expand All @@ -120,18 +113,14 @@ class TestEmail extends Mailable
],
])
],
// Precedence bulk header
true,
// Send at
new Carbon('2022-01-28 11:53:20'),
precedenceBulkHeader: true,
sendAt: new Carbon('2022-01-28 11:53:20'),
);
}
}
```

Attachments are added through standard Laravel methods.

We provide a `MailerSendTrait` trait that adds a `mailersend` method to the mailable and allows you to use templates, variables & tags support available through our API.
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:

Expand All @@ -145,7 +134,7 @@ Mail::to('recipient@domain.com')
->send(new TestEmail());
```

Please refer to [Laravel Mail documenation](https://laravel.com/docs/7.x/mail) and [MailerSend API documentation](https://developers.mailersend.com) for more information.
Please refer to [Laravel Mail documenation](https://laravel.com/docs/9.x/mail) and [MailerSend API documentation](https://developers.mailersend.com) for more information.

<a name="support-and-feedback"></a>
# Support and Feedback
Expand Down
12 changes: 7 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
}
],
"require": {
"php": "^7.4|^8.0",
"illuminate/support": "^7.0|^8.0",
"php-http/guzzle7-adapter": "^0.1",
"php": "^8.0",
"illuminate/support": "^9.0",
"php-http/guzzle7-adapter": "^1.0",
"mailersend/mailersend": "^0.5.0",
"nyholm/psr7": "^1.3"
"nyholm/psr7": "^1.5",
"symfony/mailer": "^6.0",
"ext-json": "*"
},
"require-dev": {
"orchestra/testbench": "^6.0",
"orchestra/testbench": "^7.0",
"phpunit/phpunit": "^9.0"
},
"autoload": {
Expand Down
27 changes: 17 additions & 10 deletions src/MailerSendTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Carbon\Carbon;
use Illuminate\Mail\Mailable;
use Illuminate\Support\Arr;
use Swift_Message;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\DataPart;

trait MailerSendTrait
{
Expand All @@ -16,24 +17,30 @@ public function mailersend(
array $personalization = [],
?bool $precedenceBulkHeader = null,
Carbon $sendAt = null
)
{
): static {
if ($this instanceof Mailable && $this->driver() === 'mailersend') {
$this->withSwiftMessage(function (Swift_Message $message) use ($tags, $variables, $template_id, $personalization, $sendAt, $precedenceBulkHeader) {
$this->withSymfonyMessage(function (Email $message) use (
$tags,
$variables,
$template_id,
$personalization,
$sendAt,
$precedenceBulkHeader
) {
$mailersendData = [];

Arr::set($mailersendData, MailerSendTransport::MAILERSEND_DATA_TEMPLATE_ID, $template_id);
Arr::set($mailersendData, MailerSendTransport::MAILERSEND_DATA_VARIABLES, $variables);
Arr::set($mailersendData, MailerSendTransport::MAILERSEND_DATA_TAGS, $tags);
Arr::set($mailersendData, MailerSendTransport::MAILERSEND_DATA_PERSONALIZATION, $personalization);
Arr::set($mailersendData, MailerSendTransport::MAILERSEND_DATA_PRECENDECE_BULK_HEADER, $precedenceBulkHeader);
Arr::set($mailersendData, MailerSendTransport::MAILERSEND_DATA_SEND_AT, $sendAt?->timestamp);

if ($sendAt) {
Arr::set($mailersendData, MailerSendTransport::MAILERSEND_DATA_SEND_AT, $sendAt->timestamp);
}

$message->addPart(json_encode($mailersendData, JSON_THROW_ON_ERROR),
MailerSendTransport::MAILERSEND_DATA);
$message->attachPart(new DataPart(
json_encode($mailersendData, JSON_THROW_ON_ERROR),
MailerSendTransport::MAILERSEND_DATA_SUBTYPE.'.json',
MailerSendTransport::MAILERSEND_DATA_TYPE.'/'.MailerSendTransport::MAILERSEND_DATA_SUBTYPE
));
});

if ($template_id !== null) {
Expand Down
Loading