Skip to content

Commit c4e3ae3

Browse files
committed
Compatibility update for Laravel 9
1 parent 69a9d75 commit c4e3ae3

File tree

4 files changed

+99
-117
lines changed

4 files changed

+99
-117
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
Laravel Elastic Email is a wrapper for Elastic Email.
44
You can send e-mails in your project just like you usually do with Laravel's native mailers, the package makes sure the e-mails are send via the Elastic Email API using your Elastic Email account.
55

6-
7-
86
## Requires
9-
Laravel version 8.12 or higher.
7+
V1 - Laravel version 8.12 or higher.
8+
V2 - Laravel version 9.0 or higher.
109

1110
### Installation ###
1211

@@ -18,7 +17,6 @@ composer require flexflux/laravel-elastic-email
1817

1918
* Step 2: Add your account and API keys to your **.env file**.
2019
```
21-
ELASTIC_ACCOUNT=<Your public account key>
2220
ELASTIC_KEY=<Your API key>
2321
```
2422

@@ -33,8 +31,7 @@ MAIL_MAILER=elastic_email
3331
...
3432
'elastic_email' => [
3533
'transport' => 'elasticemail',
36-
'key' => env('ELASTIC_KEY'),
37-
'account' => env('ELASTIC_ACCOUNT')
34+
'key' => env('ELASTIC_KEY')
3835
],
3936
...
4037
],
@@ -47,7 +44,6 @@ MAIL_MAILER=elastic_email
4744
* Laravel Framework Service Providers...
4845
*/
4946
...
50-
// Illuminate\Mail\MailServiceProvider::class,
5147
\FlexFlux\LaravelElasticEmail\LaravelElasticEmailServiceProvider::class,
5248
...
5349
],
@@ -57,4 +53,10 @@ MAIL_MAILER=elastic_email
5753

5854
Read Laravels documentation on how to send E-mails with the Laravel Framework.
5955

60-
https://laravel.com/docs/8.x/mail
56+
https://laravel.com/docs/8.x/mail
57+
58+
### Upgrade Guide from V1 to V2 ###
59+
Upgrade guide from V1 to V2:
60+
- Turn the MailServiceProvider back on in your app.php config file.
61+
- Remove your account ID environment variable.
62+
- Update your elastic_email mailer attribute in your mail.php configuration.

src/ElasticTransport.php

Lines changed: 78 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -3,129 +3,99 @@
33

44
namespace FlexFlux\LaravelElasticEmail;
55

6-
use GuzzleHttp\ClientInterface;
7-
use Illuminate\Mail\Transport\Transport;
6+
use Symfony\Component\Mime\Email;
87
use Illuminate\Support\Facades\Storage;
9-
use Swift_Mime_SimpleMessage;
8+
use Symfony\Component\Mailer\SentMessage;
9+
use Symfony\Component\Mime\Part\DataPart;
10+
use Symfony\Component\Mime\MessageConverter;
11+
use Symfony\Component\Mailer\Transport\AbstractTransport;
1012

11-
class ElasticTransport extends Transport
13+
class ElasticTransport extends AbstractTransport
1214
{
13-
protected $client;
1415
protected $key;
15-
protected $account;
1616
protected $url = "https://api.elasticemail.com/v2/email/send";
1717

18-
public function __construct(ClientInterface $client, $key, $account)
18+
public function __construct($key)
1919
{
20-
$this->client = $client;
2120
$this->key = $key;
22-
$this->account = $account;
2321
}
2422

25-
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
23+
public function __toString(): string
2624
{
27-
$this->beforeSendPerformed($message);
25+
return 'elasticemail';
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function doSend(SentMessage $message) : void
32+
{
33+
$email = MessageConverter::toEmail($message->getOriginalMessage());
2834

2935
$data = [
3036
'apikey' => $this->key,
31-
'account' => $this->account,
32-
'msgTo' => $this->getEmailAddresses($message),
33-
'msgCC' => $this->getEmailAddresses($message, 'getCc'),
34-
'msgBcc' => $this->getEmailAddresses($message, 'getBcc'),
35-
'msgFrom' => $this->getFromAddress($message)['email'],
36-
'msgFromName' => $this->getFromAddress($message)['name'],
37-
'from' => $this->getFromAddress($message)['email'],
38-
'fromName' => $this->getFromAddress($message)['name'],
39-
'to' => $this->getEmailAddresses($message),
40-
'subject' => $message->getSubject(),
41-
'bodyHtml' => $message->getBody(),
42-
'bodyText' => $this->getText($message),
43-
'isTransactional' => true,
37+
'msgTo' => $this->getEmailAddresses($email),
38+
'msgCC' => $this->getEmailAddresses($email, 'getCc'),
39+
'msgBcc' => $this->getEmailAddresses($email, 'getBcc'),
40+
'msgFrom' => $email->getFrom()[0]->getAddress(),
41+
'msgFromName' => $email->getFrom()[0]->getName(),
42+
'from' => $email->getFrom()[0]->getAddress(),
43+
'fromName' => $email->getFrom()[0]->getName(),
44+
'replyTo' => $this->getEmailAddresses($email, 'getReplyTo'),
45+
'to' => $this->getEmailAddresses($email),
46+
'subject' => $email->getSubject(),
47+
'bodyHtml' => $email->getHtmlBody(),
48+
'bodyText' => $email->getTextBody(),
49+
'isTransactional' => $email->getHeaders()->getHeaderBody('x-metadata-transactional') ? true : false,
4450
];
4551

46-
$attachments = $message->getChildren();
47-
$attachmentCount = $this->checkAttachmentCount($attachments);
52+
$attachments = $email->getAttachments();
53+
$attachmentCount = count($attachments);
4854

4955
if ($attachmentCount > 0) {
5056
$data = $this->attach($attachments, $data);
5157
}
58+
5259
$ch = curl_init();
5360

54-
curl_setopt_array($ch, array(
61+
curl_setopt_array($ch, [
5562
CURLOPT_URL => $this->url,
5663
CURLOPT_POST => true,
5764
CURLOPT_POSTFIELDS => $data,
5865
CURLOPT_RETURNTRANSFER => true,
5966
CURLOPT_HEADER => false,
60-
CURLOPT_SSL_VERIFYPEER => false
61-
));
67+
CURLOPT_SSL_VERIFYPEER => false,
68+
]);
6269

63-
$result = curl_exec($ch);
70+
curl_exec($ch);
6471
curl_close($ch);
6572

6673
if ($attachmentCount > 0) {
6774
$this->deleteTempAttachmentFiles($data, $attachmentCount);
6875
}
69-
70-
return $result;
71-
}
72-
73-
protected function getEmailAddresses(Swift_Mime_SimpleMessage $message, $method = 'getTo')
74-
{
75-
$data = call_user_func([$message, $method]);
76-
77-
if (is_array($data)) {
78-
return implode(',', array_keys($data));
79-
}
80-
return '';
81-
}
82-
83-
protected function getFromAddress(Swift_Mime_SimpleMessage $message)
84-
{
85-
return [
86-
'email' => array_keys($message->getFrom())[0],
87-
'name' => array_values($message->getFrom())[0],
88-
];
89-
}
90-
91-
protected function getText(Swift_Mime_SimpleMessage $message)
92-
{
93-
$text = null;
94-
95-
foreach ($message->getChildren() as $child) {
96-
if ($child->getContentType() == 'text/plain') {
97-
$text = $child->getBody();
98-
}
99-
}
100-
101-
return $text;
10276
}
10377

104-
public function checkAttachmentCount($attachments)
105-
{
106-
$count = 0;
107-
foreach ($attachments AS $attachment) {
108-
if ($attachment instanceof \Swift_Attachment) {
109-
$count++;
110-
}
111-
}
112-
return $count;
113-
}
78+
/**
79+
* Add attachments to post data array.
80+
* @param $attachments
81+
* @param $data
82+
* @return mixed
83+
*/
11484

11585
public function attach($attachments, $data)
11686
{
11787
if (is_array($attachments) && count($attachments) > 0) {
11888
$i = 1;
119-
foreach ($attachments AS $attachment) {
120-
if ($attachment instanceof \Swift_Attachment) {
89+
foreach ($attachments as $attachment) {
90+
if ($attachment instanceof DataPart) {
12191
$attachedFile = $attachment->getBody();
122-
$fileName = $attachment->getFilename();
92+
$fileName = $attachment->getPreparedHeaders()->getHeaderParameter('Content-Disposition', 'filename');
12393
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
124-
$tempName = uniqid() . '.' . $ext;
94+
$tempName = uniqid().'.'.$ext;
12595
Storage::put($tempName, $attachedFile);
126-
$type = $attachment->getContentType();
96+
$type = $attachment->getMediaType().'/'.$attachment->getMediaSubtype();
12797
$attachedFilePath = storage_path($tempName);
128-
$data['file_' . $i] = new \CURLFile($attachedFilePath, $type, $fileName);
98+
$data['file_'.$i] = new \CurlFile($attachedFilePath, $type, $fileName);
12999
$i++;
130100
}
131101
}
@@ -134,10 +104,37 @@ public function attach($attachments, $data)
134104
return $data;
135105
}
136106

137-
protected function deleteTempAttachmentFiles($data, $count)
107+
/**
108+
* Retrieve requested emailaddresses from email.
109+
* @param Email $email
110+
* @param string $method
111+
* @return string
112+
*/
113+
114+
protected function getEmailAddresses(Email $email, $method = 'getTo')
115+
{
116+
$data = call_user_func([$email, $method]);
117+
118+
$addresses = [];
119+
if (is_array($data)) {
120+
foreach ($data as $address) {
121+
$addresses[] = $address->getAddress();
122+
}
123+
}
124+
125+
return implode(',', $addresses);
126+
}
127+
128+
/**
129+
* Delete temporary attachment files.
130+
* @param $data
131+
* @param $count
132+
*/
133+
134+
protected function deleteTempAttachmentFiles($data, $count) : void
138135
{
139136
for ($i = 1; $i <= $count; $i++) {
140-
$file = $data['file_' . $i]->name;
137+
$file = $data['file_'.$i]->name;
141138
if (file_exists($file)) {
142139
unlink($file);
143140
}
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
<?php
22

3-
43
namespace FlexFlux\LaravelElasticEmail;
54

6-
use Illuminate\Mail\MailServiceProvider as LaravelMailServiceProvider;
5+
use Illuminate\Mail\MailManager;
6+
use Illuminate\Support\ServiceProvider;
77

8-
class LaravelElasticEmailServiceProvider extends LaravelMailServiceProvider
8+
class LaravelElasticEmailServiceProvider extends ServiceProvider
99
{
10-
protected function registerIlluminateMailer()
10+
public function register()
1111
{
12-
$this->app->singleton('mail.manager', function ($app) {
13-
return new MailManager($app);
14-
});
12+
$this->app->afterResolving(MailManager::class, function (MailManager $manager) {
13+
$manager->extend('elastic_email', function () {
14+
$config = $this->app['config']->get('mail.mailers.elastic_email', []);
1515

16-
$this->app->bind('mailer', function ($app) {
17-
return $app->make('mail.manager')->mailer();
16+
return new ElasticTransport(
17+
$config['key'],
18+
);
19+
});
1820
});
1921
}
2022
}

src/MailManager.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)