-
Notifications
You must be signed in to change notification settings - Fork 15
/
ElasticTransport.php
130 lines (112 loc) · 2.94 KB
/
ElasticTransport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Chocoholics\LaravelElasticEmail;
use GuzzleHttp\ClientInterface;
use Illuminate\Mail\Transport\Transport;
use Swift_Mime_SimpleMessage;
class ElasticTransport extends Transport
{
/**
* Guzzle client instance.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $client;
/**
* The Elastic Email API key.
*
* @var string
*/
protected $key;
/**
* The Elastic Email username.
*
* @var string
*/
protected $account;
/**
* THe Elastic Email API end-point.
*
* @var string
*/
protected $url = 'https://api.elasticemail.com/v2/email/send';
/**
* Create a new Elastic Email transport instance.
*
* @param \GuzzleHttp\ClientInterface $client
* @param string $key
* @param string $username
*
* @return void
*/
public function __construct(ClientInterface $client, $key, $account)
{
$this->client = $client;
$this->key = $key;
$this->account = $account;
}
/**
* {@inheritdoc}
*/
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
{
$this->beforeSendPerformed($message);
$data = [
'api_key' => $this->key,
'account' => $this->account,
'msgTo' => $this->getEmailAddresses($message),
'msgCC' => $this->getEmailAddresses($message, 'getCc'),
'msgBcc' => $this->getEmailAddresses($message, 'getBcc'),
'msgFrom' => $this->getFromAddress($message)['email'],
'msgFromName' => $this->getFromAddress($message)['name'],
'from' => $this->getFromAddress($message)['email'],
'fromName' => $this->getFromAddress($message)['name'],
'to' => $this->getEmailAddresses($message),
'subject' => $message->getSubject(),
'body_html' => $message->getBody(),
'body_text' => $this->getText($message)
];
$result = $this->client->post($this->url, [
'form_params' => $data
]);
return $result;
}
/**
* Get the plain text part.
*
* @param \Swift_Mime_SimpleMessage $message
* @return text|null
*/
protected function getText(Swift_Mime_SimpleMessage $message)
{
$text = null;
foreach($message->getChildren() as $child)
{
if($child->getContentType() == 'text/plain')
{
$text = $child->getBody();
}
}
return $text;
}
/**
* @param \Swift_Mime_SimpleMessage $message
*
* @return array
*/
protected function getFromAddress(Swift_Mime_SimpleMessage $message)
{
return [
'email' => array_keys($message->getFrom())[0],
'name' => array_values($message->getFrom())[0],
];
}
protected function getEmailAddresses(Swift_Mime_SimpleMessage $message, $method = 'getTo')
{
$data = call_user_func([$message, $method]);
if(is_array($data))
{
return implode(',', array_keys($data));
}
return '';
}
}