├── .github
├── CODEOWNERS
└── workflows
│ └── php.yml
├── .gitignore
├── config
└── mailersend-driver.php
├── .editorconfig
├── phpunit.xml.dist
├── tests
├── TestCase.php
└── MailerSendTransportTest.php
├── LICENSE.md
├── src
├── LaravelDriverServiceProvider.php
├── MailerSendTrait.php
└── MailerSendTransport.php
├── composer.json
└── README.md
/.github/CODEOWNERS:
--------------------------------------------------------------------------------
1 | * @mailersend/php-sdk-maintainers
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | composer.phar
2 | composer.lock
3 | vendor
4 | .idea
5 | .phpunit.result.cache
6 |
--------------------------------------------------------------------------------
/config/mailersend-driver.php:
--------------------------------------------------------------------------------
1 | env('MAILERSEND_API_KEY'),
5 | 'host' => env('MAILERSEND_API_HOST', 'api.mailersend.com'),
6 | 'protocol' => env('MAILERSEND_API_PROTO', 'https'),
7 | 'api_path' => env('MAILERSEND_API_PATH', 'v1'),
8 | ];
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | ; This file is for unifying the coding style for different editors and IDEs.
2 | ; More information at http://editorconfig.org
3 |
4 | root = true
5 |
6 | [*]
7 | charset = utf-8
8 | indent_size = 4
9 | indent_style = space
10 | end_of_line = lf
11 | insert_final_newline = true
12 | trim_trailing_whitespace = true
13 |
14 | [*.md]
15 | trim_trailing_whitespace = false
16 |
--------------------------------------------------------------------------------
/phpunit.xml.dist:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
14 | tests
15 |
16 |
17 |
18 |
19 | src/
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/tests/TestCase.php:
--------------------------------------------------------------------------------
1 | getMessage());
25 | }
26 |
27 | $method = $reflection->getMethod($method);
28 | $method->setAccessible(true);
29 |
30 | return $method->invokeArgs($object, $parameters);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 MailerSend
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/.github/workflows/php.yml:
--------------------------------------------------------------------------------
1 | name: PHP Composer
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ main ]
8 |
9 | jobs:
10 | run:
11 |
12 | runs-on: ubuntu-latest
13 | strategy:
14 | matrix:
15 | operating-system: [ubuntu-latest]
16 | php-versions: ['8.0', '8.1']
17 | name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}
18 |
19 | steps:
20 | - uses: actions/checkout@v2
21 |
22 | - name: Setup PHP
23 | uses: shivammathur/setup-php@v2
24 | with:
25 | php-version: ${{ matrix.php-versions }}
26 | extensions: mbstring, pdo, pdo_mysql, intl, zip
27 | coverage: none
28 |
29 | - name: Cache Composer packages
30 | id: composer-cache
31 | uses: actions/cache@v2
32 | with:
33 | path: vendor
34 | key: ${{ runner.os }}-php-${{ hashFiles('**/composer.json') }}
35 | restore-keys: |
36 | ${{ runner.os }}-php-
37 | - name: Install dependencies
38 | if: steps.composer-cache.outputs.cache-hit != 'true'
39 | run: composer install --prefer-dist --no-progress --no-suggest
40 |
41 | - name: Run test suite
42 | run: composer run-script test
43 |
--------------------------------------------------------------------------------
/src/LaravelDriverServiceProvider.php:
--------------------------------------------------------------------------------
1 | app->make(MailManager::class)->extend('mailersend', function () {
15 | $config = $this->app['config']->get('mailersend-driver', []);
16 |
17 | $mailersend = new MailerSend([
18 | 'api_key' => Arr::get($config, 'api_key'),
19 | 'host' => Arr::get($config, 'host'),
20 | 'protocol' => Arr::get($config, 'protocol'),
21 | 'api_path' => Arr::get($config, 'api_path'),
22 | ]);
23 |
24 | return new MailerSendTransport($mailersend);
25 | });
26 |
27 | if ($this->app->runningInConsole()) {
28 | $this->publishes([
29 | __DIR__.'/../config/config.php' => config_path('laravel-driver.php'),
30 | ], 'config');
31 | }
32 | }
33 |
34 | public function register()
35 | {
36 | $this->mergeConfigFrom(__DIR__.'/../config/mailersend-driver.php', 'mailersend-driver');
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "mailersend/laravel-driver",
3 | "description": "MailerSend Laravel Driver",
4 | "keywords": [
5 | "MailerSend",
6 | "mailersend",
7 | "email",
8 | "transactional",
9 | "laravel-driver"
10 | ],
11 | "homepage": "https://github.com/mailersend/mailersend-laravel-driver",
12 | "license": "MIT",
13 | "type": "library",
14 | "authors": [
15 | {
16 | "name": "Tautvydas Tijūnaitis",
17 | "email": "tautvydas@mailersend.com",
18 | "homepage": "https://mailersend.com",
19 | "role": "Developer"
20 | }
21 | ],
22 | "require": {
23 | "php": "^8.0",
24 | "illuminate/support": "^9.0 || ^10.0",
25 | "php-http/guzzle7-adapter": "^1.0",
26 | "mailersend/mailersend": "^0.8.0",
27 | "nyholm/psr7": "^1.5",
28 | "symfony/mailer": "^6.0",
29 | "ext-json": "*"
30 | },
31 | "require-dev": {
32 | "orchestra/testbench": "^7.0",
33 | "phpunit/phpunit": "^9.0"
34 | },
35 | "autoload": {
36 | "psr-4": {
37 | "MailerSend\\LaravelDriver\\": "src"
38 | }
39 | },
40 | "autoload-dev": {
41 | "psr-4": {
42 | "MailerSend\\LaravelDriver\\Tests\\": "tests"
43 | }
44 | },
45 | "scripts": {
46 | "test": "vendor/bin/phpunit"
47 | },
48 | "config": {
49 | "sort-packages": true
50 | },
51 | "extra": {
52 | "laravel": {
53 | "providers": [
54 | "MailerSend\\LaravelDriver\\LaravelDriverServiceProvider"
55 | ],
56 | "aliases": {
57 | "LaravelDriver": "MailerSend\\LaravelDriver\\LaravelDriverFacade"
58 | }
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/src/MailerSendTrait.php:
--------------------------------------------------------------------------------
1 | driver() === 'mailersend') {
22 | $this->withSymfonyMessage(function (Email $message) use (
23 | $tags,
24 | $variables,
25 | $template_id,
26 | $personalization,
27 | $sendAt,
28 | $precedenceBulkHeader
29 | ) {
30 | $mailersendData = [];
31 |
32 | Arr::set($mailersendData, MailerSendTransport::MAILERSEND_DATA_TEMPLATE_ID, $template_id);
33 | Arr::set($mailersendData, MailerSendTransport::MAILERSEND_DATA_VARIABLES, $variables);
34 | Arr::set($mailersendData, MailerSendTransport::MAILERSEND_DATA_TAGS, $tags);
35 | Arr::set($mailersendData, MailerSendTransport::MAILERSEND_DATA_PERSONALIZATION, $personalization);
36 | Arr::set($mailersendData, MailerSendTransport::MAILERSEND_DATA_PRECENDECE_BULK_HEADER, $precedenceBulkHeader);
37 | Arr::set($mailersendData, MailerSendTransport::MAILERSEND_DATA_SEND_AT, $sendAt?->timestamp);
38 |
39 | $message->attachPart(new DataPart(
40 | json_encode($mailersendData, JSON_THROW_ON_ERROR),
41 | MailerSendTransport::MAILERSEND_DATA_SUBTYPE.'.json',
42 | MailerSendTransport::MAILERSEND_DATA_TYPE.'/'.MailerSendTransport::MAILERSEND_DATA_SUBTYPE
43 | ));
44 | });
45 |
46 | if ($template_id !== null) {
47 | $this->html('');
48 | }
49 | }
50 |
51 | return $this;
52 | }
53 |
54 | protected function driver(): string
55 | {
56 | return function_exists('config') ? config('mail.default') : env('MAIL_MAILER');
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | MailerSend Laravel Driver
4 |
5 | [](./LICENSE.md)
6 |
7 | # Table of Contents
8 |
9 | * [Installation](#installation)
10 | * [Usage](#usage)
11 | * [Support and Feedback](#support-and-feedback)
12 | * [License](#license)
13 |
14 |
15 | # Installation
16 |
17 | ## Requirements
18 |
19 | - Laravel 9.0+
20 | - PHP 8.0+
21 | - Guzzle 7.0+
22 | - An API Key from [mailersend.com](https://www.mailersend.com)
23 |
24 | **For Laravel 7.x - 8.x support see [1.x branch](https://github.com/mailersend/mailersend-laravel-driver/tree/1.x)**
25 |
26 | ## Setup
27 |
28 | You can install the package via composer:
29 |
30 | ```bash
31 | composer require mailersend/laravel-driver
32 | ```
33 |
34 | After that, you need to set `MAILERSEND_API_KEY` in your `.env` file:
35 |
36 | ```dotenv
37 | MAILERSEND_API_KEY=
38 | ```
39 |
40 | Add MailerSend as a Laravel Mailer in `config/mail.php` in `mailers` array:
41 |
42 | ```php
43 | 'mailersend' => [
44 | 'transport' => 'mailersend',
45 | ],
46 | ```
47 |
48 | And set environment variable `MAIL_MAILER` in your `.env` file
49 |
50 | ```dotenv
51 | MAIL_MAILER=mailersend
52 | ```
53 |
54 | Also, double check that your `FROM` data is filled in `.env`:
55 |
56 | ```dotenv
57 | MAIL_FROM_ADDRESS=app@yourdomain.com
58 | MAIL_FROM_NAME="App Name"
59 | ```
60 |
61 |
62 | # Usage
63 |
64 | This is an example [mailable](https://laravel.com/docs/9.x/mail#writing-mailables) that you can use to send an email with.
65 |
66 | `app/Mail/TestEmail.php`
67 |
68 | ```php
69 | namespace App\Mail;
70 |
71 | use Illuminate\Bus\Queueable;
72 | use Illuminate\Mail\Mailable;
73 | use Illuminate\Queue\SerializesModels;
74 | use Illuminate\Support\Arr;
75 | use MailerSend\Helpers\Builder\Variable;
76 | use MailerSend\Helpers\Builder\Personalization;
77 | use MailerSend\LaravelDriver\MailerSendTrait;
78 |
79 | class TestEmail extends Mailable
80 | {
81 | use Queueable, SerializesModels, MailerSendTrait;
82 |
83 | public function build()
84 | {
85 | // Recipient for use with variables and/or personalization
86 | $to = Arr::get($this->to, '0.address');
87 |
88 | return $this
89 | ->view('emails.test_html')
90 | ->text('emails.test_text')
91 | ->attachFromStorageDisk('public', 'example.png')
92 | // Additional options for MailerSend API features
93 | ->mailersend(
94 | template_id: null,
95 | variables: [
96 | new Variable($to, ['name' => 'Your Name'])
97 | ],
98 | tags: ['tag'],
99 | personalization: [
100 | new Personalization($to, [
101 | 'var' => 'variable',
102 | 'number' => 123,
103 | 'object' => [
104 | 'key' => 'object-value'
105 | ],
106 | 'objectCollection' => [
107 | [
108 | 'name' => 'John'
109 | ],
110 | [
111 | 'name' => 'Patrick'
112 | ]
113 | ],
114 | ])
115 | ],
116 | precedenceBulkHeader: true,
117 | sendAt: new Carbon('2022-01-28 11:53:20'),
118 | );
119 | }
120 | }
121 | ```
122 |
123 | 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.
124 |
125 | After creating the mailable, you can send it using:
126 |
127 | ```php
128 | use App\Mail\TestEmail;
129 | use Illuminate\Support\Facades\Mail;
130 |
131 | Mail::to('recipient@domain.com')
132 | ->cc('cc@domain.com')
133 | ->bcc('bcc@domain.com')
134 | ->send(new TestEmail());
135 | ```
136 |
137 | Please refer to [Laravel Mail documenation](https://laravel.com/docs/9.x/mail) and [MailerSend API documentation](https://developers.mailersend.com) for more information.
138 |
139 |
140 | # Support and Feedback
141 |
142 | In case you find any bugs, submit an issue directly here in GitHub.
143 |
144 | If you have any troubles using our driver, feel free to contact our support by email [info@mailersend.com](mailto:info@mailersend.com)
145 |
146 | Official API documentation is at [https://developers.mailersend.com](https://developers.mailersend.com)
147 |
148 |
149 | # License
150 |
151 | [The MIT License (MIT)](LICENSE.md)
152 |
--------------------------------------------------------------------------------
/tests/MailerSendTransportTest.php:
--------------------------------------------------------------------------------
1 | mailersend = new MailerSend([
26 | 'api_key' => 'key',
27 | 'host' => '',
28 | 'protocol' => '',
29 | 'api_path' => '',
30 | ]);
31 |
32 |
33 | $this->transport = new MailerSendTransport($this->mailersend);
34 | }
35 |
36 | public function test_basic_message_is_sent(): void
37 | {
38 | $response = [
39 | 'response' => $this->mock(ResponseInterface::class, function (MockInterface $mock) {
40 | $mock->expects('getHeaderLine')->withArgs(['X-Message-Id'])->andReturn('messageId');
41 |
42 | $stream = $this->mock(StreamInterface::class, function (MockInterface $mock) {
43 | $mock->expects('getContents')->withNoArgs()->andReturn('{"json":"value"}');
44 | });
45 |
46 | $mock->expects('getBody')->withNoArgs()->andReturn($stream);
47 | }),
48 | ];
49 |
50 | $emailParams = $this->partialMock(EmailParams::class, function (MockInterface $mock) {
51 | $mock->expects('setFrom')->withArgs(['test@mailersend.com'])->andReturnSelf();
52 | $mock->expects('setFromName')->withArgs(['John Doe'])->andReturnSelf();
53 | $mock->expects('setRecipients')->withAnyArgs()->andReturnSelf();
54 | $mock->expects('setSubject')->withArgs(['Subject'])->andReturnSelf();
55 | $mock->expects('setText')->withArgs(['Here is the text message'])->andReturnSelf();
56 | });
57 |
58 | /** @noinspection PhpFieldAssignmentTypeMismatchInspection */
59 | $this->mailersend->email = $this->mock(Email::class,
60 | function (MockInterface $mock) use ($emailParams, $response) {
61 | $mock->allows('send')->withArgs([$emailParams])->andReturn($response);
62 | });
63 |
64 | $message = (new \Symfony\Component\Mime\Email())
65 | ->subject('Subject')
66 | ->from('John Doe ')
67 | ->to('test-receive@mailersend.com')
68 | ->text('Here is the text message');
69 |
70 | $transport = new MailerSendTransport($this->mailersend);
71 | $sentMessage = $transport->send($message, Envelope::create($message));
72 |
73 | self::assertNotNull($sentMessage);
74 |
75 | $sentMessageString = $sentMessage->getMessage()->toString();
76 |
77 | self::assertStringContainsString('X-MailerSend-Message-Id: messageId', $sentMessageString);
78 | self::assertStringContainsString('X-MailerSend-Body: {"json":"value"}', $sentMessageString);
79 | }
80 |
81 | public function test_get_from(): void
82 | {
83 | $message = (new \Symfony\Component\Mime\Email())
84 | ->from('John Doe ');
85 |
86 | $getFrom = $this->callMethod($this->transport, 'getFrom', [$message]);
87 |
88 | self::assertEquals(['email' => 'test@mailersend.com', 'name' => 'John Doe'], $getFrom);
89 | }
90 |
91 | public function test_get_reply_to(): void
92 | {
93 | $message = (new \Symfony\Component\Mime\Email())
94 | ->replyTo('John Doe ');
95 |
96 | $getReplyTo = $this->callMethod($this->transport, 'getReplyTo', [$message]);
97 |
98 | self::assertEquals(['email' => 'test@mailersend.com', 'name' => 'John Doe'], $getReplyTo);
99 | }
100 |
101 | public function test_get_recipients(): void
102 | {
103 | $message = (new \Symfony\Component\Mime\Email())
104 | ->to('test-receive@mailersend.com');
105 |
106 | $getTo = $this->callMethod($this->transport, 'getRecipients', ['to', $message]);
107 |
108 | self::assertEquals('test-receive@mailersend.com', Arr::get(reset($getTo)->toArray(),
109 | 'email'));
110 | }
111 |
112 | public function test_get_attachments(): void
113 | {
114 | $attachment = new DataPart('data', 'filename', 'image/jpeg');
115 |
116 | $message = (new \Symfony\Component\Mime\Email())
117 | ->attachPart($attachment);
118 |
119 | $getAttachments = $this->callMethod($this->transport, 'getAttachments', [$message]);
120 |
121 | $attachmentResult = reset($getAttachments)->toArray();
122 |
123 | self::assertEquals('data', Arr::get($attachmentResult,
124 | 'content'));
125 | self::assertEquals('filename', Arr::get($attachmentResult,
126 | 'filename'));
127 | self::assertEquals('attachment', Arr::get($attachmentResult,
128 | 'disposition'));
129 | }
130 |
131 | public function test_get_additional_data(): void
132 | {
133 | $message = (new \Symfony\Component\Mime\Email())
134 | ->attachPart(new DataPart(
135 | json_encode([
136 | 'template_id' => 'id'
137 | ], JSON_THROW_ON_ERROR),
138 | MailerSendTransport::MAILERSEND_DATA_SUBTYPE.'.json',
139 | MailerSendTransport::MAILERSEND_DATA_TYPE.'/'.MailerSendTransport::MAILERSEND_DATA_SUBTYPE
140 | ));
141 |
142 | $getAdditionalData = $this->callMethod($this->transport, 'getAdditionalData', [$message]);
143 |
144 | self::assertEquals('id', Arr::get($getAdditionalData,
145 | 'template_id'));
146 | self::assertEquals([], Arr::get($getAdditionalData,
147 | 'variables'));
148 | self::assertEquals([], Arr::get($getAdditionalData,
149 | 'tags'));
150 | self::assertEquals([], Arr::get($getAdditionalData,
151 | 'personalization'));
152 | }
153 | }
154 |
--------------------------------------------------------------------------------
/src/MailerSendTransport.php:
--------------------------------------------------------------------------------
1 | mailersend = $mailersend;
35 | }
36 |
37 | /**
38 | * @throws \Assert\AssertionFailedException
39 | * @throws \JsonException
40 | * @throws \Psr\Http\Client\ClientExceptionInterface
41 | * @throws TransportExceptionInterface
42 | * @throws \MailerSend\Exceptions\MailerSendAssertException
43 | */
44 | public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage
45 | {
46 | ['email' => $fromEmail, 'name' => $fromName] = $this->getFrom($message);
47 | ['email' => $replyToEmail, 'name' => $replyToName] = $this->getReplyTo($message);
48 |
49 | $text = $message->getTextBody();
50 | $html = $message->getHtmlBody();
51 |
52 | $to = $this->getRecipients('to', $message);
53 | $cc = $this->getRecipients('cc', $message);
54 | $bcc = $this->getRecipients('bcc', $message);
55 |
56 | $subject = $message->getSubject();
57 |
58 | $attachments = $this->getAttachments($message);
59 |
60 | [
61 | 'template_id' => $template_id,
62 | 'variables' => $variables,
63 | 'tags' => $tags,
64 | 'personalization' => $personalization,
65 | 'precedence_bulk_header' => $precedenceBulkHeader,
66 | 'send_at' => $sendAt,
67 | ] = $this->getAdditionalData($message);
68 |
69 | $emailParams = app(EmailParams::class)
70 | ->setFrom($fromEmail)
71 | ->setFromName($fromName)
72 | ->setReplyTo($replyToEmail)
73 | ->setReplyToName($replyToName)
74 | ->setRecipients($to)
75 | ->setCc($cc)
76 | ->setBcc($bcc)
77 | ->setSubject($subject)
78 | ->setHtml($html)
79 | ->setText($text)
80 | ->setTemplateId($template_id)
81 | ->setVariables($variables)
82 | ->setPersonalization($personalization)
83 | ->setAttachments($attachments)
84 | ->setTags($tags)
85 | ->setPrecedenceBulkHeader($precedenceBulkHeader)
86 | ->setSendAt($sendAt);
87 |
88 | $response = $this->mailersend->email->send($emailParams);
89 |
90 | /** @var ResponseInterface $respInterface */
91 | $respInterface = $response['response'];
92 |
93 | if ($messageId = $respInterface->getHeaderLine('X-Message-Id')) {
94 | $message->getHeaders()?->addTextHeader('X-MailerSend-Message-Id', $messageId);
95 | }
96 |
97 | if ($body = $respInterface->getBody()->getContents()) {
98 | $message->getHeaders()?->addTextHeader('X-MailerSend-Body', $body);
99 | }
100 |
101 | return new SentMessage($message, $envelope);
102 | }
103 |
104 | protected function getFrom(RawMessage $message): array
105 | {
106 | $from = $message->getFrom();
107 |
108 | if (count($from) > 0) {
109 | return ['name' => $from[0]->getName(), 'email' => $from[0]->getAddress()];
110 | }
111 |
112 | return ['email' => '', 'name' => ''];
113 | }
114 |
115 | protected function getReplyTo(RawMessage $message): array
116 | {
117 | $from = $message->getReplyTo();
118 |
119 | if (count($from) > 0) {
120 | return ['name' => $from[0]->getName(), 'email' => $from[0]->getAddress()];
121 | }
122 |
123 | return ['email' => '', 'name' => ''];
124 | }
125 |
126 | /**
127 | * @throws \MailerSend\Exceptions\MailerSendAssertException
128 | */
129 | protected function getRecipients(string $type, RawMessage $message): array
130 | {
131 | $recipients = [];
132 |
133 | if ($addresses = $message->{'get'.ucfirst($type)}()) {
134 | foreach ($addresses as $address) {
135 | $recipients[] = new Recipient($address->getAddress(), $address->getName());
136 | }
137 | }
138 |
139 | return $recipients;
140 | }
141 |
142 | protected function getAttachments(RawMessage $message): array
143 | {
144 | $attachments = [];
145 |
146 | foreach ($message->getAttachments() as $attachment) {
147 | /** @var DataPart $attachment */
148 |
149 | if ($attachment->getMediaSubtype() === self::MAILERSEND_DATA_SUBTYPE) {
150 | continue;
151 | }
152 |
153 | $attachments[] = new Attachment(
154 | $attachment->getBody(),
155 | $attachment->getPreparedHeaders()->get('content-disposition')?->getParameter('filename'),
156 | $attachment->getPreparedHeaders()->get('content-disposition')?->getBody(),
157 | $attachment->getPreparedHeaders()->get('content-id')?->getBodyAsString()
158 | );
159 | }
160 |
161 | return $attachments;
162 | }
163 |
164 | /**
165 | * @param RawMessage $message
166 | * @param array $payload
167 | * @throws \JsonException
168 | */
169 | protected function getAdditionalData(RawMessage $message): array
170 | {
171 | $defaultValues = [
172 | 'template_id' => null,
173 | 'variables' => [],
174 | 'personalization' => [],
175 | 'tags' => [],
176 | 'precedence_bulk_header' => null,
177 | 'send_at' => null,
178 | ];
179 |
180 | foreach ($message->getAttachments() as $attachment) {
181 | /** @var DataPart $attachment */
182 |
183 | if ($attachment->getMediaSubtype() !== self::MAILERSEND_DATA_SUBTYPE) {
184 | continue;
185 | }
186 |
187 | return array_merge($defaultValues,
188 | json_decode($attachment->getBody(), true, 512, JSON_THROW_ON_ERROR));
189 | }
190 |
191 | return $defaultValues;
192 | }
193 |
194 | public function __toString(): string
195 | {
196 | return 'mailersend';
197 | }
198 | }
199 |
--------------------------------------------------------------------------------