Skip to content

Commit 81420c2

Browse files
committed
Use headers to send elastic ids and enable transactional emails
1 parent ec0ae4b commit 81420c2

File tree

4 files changed

+77
-16
lines changed

4 files changed

+77
-16
lines changed

config/elasticemail.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
*/
2222
'account' => env('ELASTIC_ACCOUNT'),
2323

24+
/*
25+
|--------------------------------------------------------------------------
26+
| Elastic Email Account Email
27+
|--------------------------------------------------------------------------
28+
|
29+
| This value is the account email provided by Elastic Email.
30+
|
31+
*/
32+
'transactional' => env('ELASTIC_IS_TRANSACTIONAL', false),
33+
2434
/*
2535
|--------------------------------------------------------------------------
2636
| Elastic Email Webhook Secret

src/ElasticTransport.php

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class ElasticTransport extends Transport
2727
*/
2828
protected $account;
2929

30+
31+
/**
32+
* If the email is transactional.
33+
* @var string
34+
*/
35+
protected $transactional;
36+
3037
/**
3138
* THe Elastic Email API end-point.
3239
* @var string
@@ -42,11 +49,12 @@ class ElasticTransport extends Transport
4249
*
4350
* @return void
4451
*/
45-
public function __construct(ClientInterface $client, $key, $account)
52+
public function __construct(ClientInterface $client, $config)
4653
{
4754
$this->client = $client;
48-
$this->key = $key;
49-
$this->account = $account;
55+
$this->key = $config['key'];
56+
$this->account = $config['account'];
57+
$this->transactional = $config['transactional'];
5058
}
5159

5260
/**
@@ -56,6 +64,11 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = NUL
5664
{
5765
$this->beforeSendPerformed($message);
5866

67+
$isTransactional = $this->transactional;
68+
if(!is_null($message->getHeaders()->get('X-Transactional'))){
69+
$isTransactional = $message->getHeaders()->get('X-Transactional')->getFieldBody();
70+
}
71+
5972
$data = [
6073
'api_key' => $this->key,
6174
'account' => $this->account,
@@ -66,13 +79,31 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = NUL
6679
'msgFromName' => $this->getFromAddress($message)['name'],
6780
'from' => $this->getFromAddress($message)['email'],
6881
'fromName' => $this->getFromAddress($message)['name'],
82+
'replyTo' => $this->getReplyToAddress($message)['email'],
83+
'replyToName' => $this->getReplyToAddress($message)['name'],
6984
'to' => $this->getEmailAddresses($message),
7085
'subject' => $message->getSubject(),
7186
'body_html' => $message->getBody(),
72-
'body_text' => $this->getText($message)
87+
'body_text' => $this->getText($message),
88+
'isTransactional' => $isTransactional
7389
];
7490

7591
$attachments = $message->getChildren();
92+
93+
$response = $this->sendEmail($data, $attachments);
94+
95+
// Inject the response data into the message headers.
96+
if($response) {
97+
$message->getHeaders()->addTextHeader('X-Message-ID', $response->messageid);
98+
$message->getHeaders()->addTextHeader('X-Transaction-ID', $response->transactionid);
99+
}
100+
101+
$this->sendPerformed($message);
102+
103+
return $response;
104+
}
105+
106+
protected function sendEmail(array $data, array $attachments = []){
76107
if (!empty($attachments)) {
77108
$options['multipart'] = $this->parseMultipart($attachments, $data);
78109
} else {
@@ -87,20 +118,25 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = NUL
87118
$url = str_replace('https:', 'http:', $url);
88119
}
89120

90-
$result = $this->client->request('POST', $url, $options);
121+
try {
122+
$result = $this->client->request('POST', $url, $options);
123+
} catch (\Exception $e) {
124+
throw $e;
125+
}
91126

92-
// Inject the response data into the message instance.
93127
if($result->getStatusCode() == 200) {
94128
$response = $result->getBody()->getContents();
95129
$response = json_decode($response);
130+
96131
if($response->success) {
97-
$message->elasticemail = $response->data;
132+
return $response->data;
133+
}
134+
else{
135+
throw new \Exception($response->error);
98136
}
99137
}
100138

101-
$this->sendPerformed($message);
102-
103-
return $result;
139+
return false;
104140
}
105141

106142
/**
@@ -153,6 +189,23 @@ protected function getEmailAddresses(Swift_Mime_SimpleMessage $message, $method
153189
return '';
154190
}
155191

192+
/**
193+
* @param \Swift_Mime_Message $message
194+
*
195+
* @return array
196+
*/
197+
protected function getReplyToAddress(Swift_Mime_SimpleMessage $message)
198+
{
199+
if (!$message->getReplyTo()) {
200+
return $this->getFromAddress($message);
201+
}
202+
203+
return [
204+
'email' => array_keys($message->getReplyTo())[0],
205+
'name' => array_values($message->getReplyTo())[0],
206+
];
207+
}
208+
156209
/**
157210
* Parse the attachments and add them to the multipart array.
158211
*

src/Listeners/OnEmailSent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public function handle(MessageSent $event)
1616
$body = $this->parseBodyText($event->message->getBody());
1717

1818
$log = ElasticEmailOutbound::create([
19-
'message_id' => $event->message->elasticemail->messageid,
20-
'transaction_id' => $event->message->elasticemail->transactionid,
19+
'message_id' => $event->message->getHeaders()->get('X-Message-ID')->getFieldBody() ?? null,
20+
'transaction_id' => $event->message->getHeaders()->get('X-Transaction-ID')->getFieldBody() ?? null,
2121
'from' => $from[0],
2222
'to' => json_encode($toArr),
2323
'cc' => $ccArr ? json_encode($ccArr) : NULL,

src/TransportManager.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ class TransportManager extends MailManager
88
{
99
protected function createElasticEmailTransport()
1010
{
11-
12-
$config = $this->app['config']->get('services.elastic_email', []);
11+
$config = array_merge($this->app['config']->get('services.elastic_email', []), $this->app['config']->get('elasticemail', []));
1312

1413
return new ElasticTransport(
1514
$this->guzzle($config),
16-
$config['key'],
17-
$config['account']
15+
$config
1816
);
1917
}
2018
}

0 commit comments

Comments
 (0)