1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dekalee\MailjetBundle\Transport; |
4
|
|
|
|
5
|
|
|
use Dekalee\MailjetBundle\Guesser\TemplateIdGuesserManager; |
6
|
|
|
use Dekalee\MailjetBundle\Message\SwiftCustomVarsMessage; |
7
|
|
|
use Mailjet\Client; |
8
|
|
|
use Mailjet\Resources; |
9
|
|
|
use Swift_Events_EventListener; |
10
|
|
|
use Swift_Mime_Message; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class MailjetSendApiTransport |
14
|
|
|
*/ |
15
|
|
|
class MailjetSendApiTransport implements \Swift_Transport |
16
|
|
|
{ |
17
|
|
|
protected $client; |
18
|
|
|
protected $dispatcher; |
19
|
|
|
protected $started = true; |
20
|
|
|
protected $templateIdGuesserManager; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Client $client |
24
|
|
|
* @param \Swift_Events_EventDispatcher $dispatcher |
25
|
|
|
* @param TemplateIdGuesserManager $templateIdGuesserManager |
26
|
|
|
*/ |
27
|
9 |
|
public function __construct( |
28
|
|
|
Client $client, |
29
|
|
|
\Swift_Events_EventDispatcher $dispatcher, |
30
|
|
|
TemplateIdGuesserManager $templateIdGuesserManager |
31
|
|
|
) { |
32
|
9 |
|
$this->client = $client; |
33
|
9 |
|
$this->dispatcher = $dispatcher; |
34
|
9 |
|
$this->templateIdGuesserManager = $templateIdGuesserManager; |
35
|
9 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Test if this Transport mechanism has started. |
39
|
|
|
* |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
1 |
|
public function isStarted() |
43
|
|
|
{ |
44
|
1 |
|
return $this->started; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Start this Transport mechanism. |
49
|
|
|
*/ |
50
|
1 |
|
public function start() |
51
|
|
|
{ |
52
|
1 |
|
$this->started = true; |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Stop this Transport mechanism. |
57
|
|
|
*/ |
58
|
1 |
|
public function stop() |
59
|
|
|
{ |
60
|
1 |
|
$this->started = false; |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Send the given Message. |
65
|
|
|
* |
66
|
|
|
* Recipient/sender data will be retrieved from the Message API. |
67
|
|
|
* The return value is the number of recipients who were accepted for delivery. |
68
|
|
|
* |
69
|
|
|
* @param Swift_Mime_Message $message |
70
|
|
|
* @param string[] $failedRecipients An array of failures by-reference |
71
|
|
|
* |
72
|
|
|
* @return int |
73
|
|
|
*/ |
74
|
5 |
|
public function send(Swift_Mime_Message $message, &$failedRecipients = null) |
75
|
|
|
{ |
76
|
5 |
|
$failedRecipients = (array) $failedRecipients; |
77
|
|
|
|
78
|
5 |
|
if ($evt = $this->dispatcher->createSendEvent($this, $message)) { |
79
|
4 |
|
$this->dispatcher->dispatchEvent($evt, 'beforeSendPerformed'); |
80
|
4 |
|
if ($evt->bubbleCancelled()) { |
81
|
1 |
|
return 0; |
82
|
|
|
} |
83
|
3 |
|
} |
84
|
|
|
|
85
|
4 |
|
$from = $message->getFrom(); |
86
|
4 |
|
$recipients = []; |
87
|
4 |
|
$headers = []; |
88
|
|
|
|
89
|
4 |
|
foreach ($message->getTo() as $email => $name) { |
90
|
4 |
|
$recipients[] = ['Email' => $email, 'Name' => $email]; |
91
|
4 |
|
} |
92
|
|
|
|
93
|
4 |
|
foreach ($message->getHeaders()->getAll() as $header) { |
94
|
|
|
$headers[$header->getFieldName()] = $header->getFieldBody(); |
95
|
4 |
|
} |
96
|
|
|
|
97
|
4 |
|
$vars = ['content' => $message->getBody()]; |
98
|
4 |
|
if ($message instanceof SwiftCustomVarsMessage) { |
99
|
1 |
|
$vars = $message->getVars(); |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
$body = [ |
103
|
4 |
|
'FromEmail' => key($from), |
104
|
4 |
|
'Subject' => $message->getSubject(), |
105
|
4 |
|
'Vars' => $vars, |
106
|
4 |
|
'Recipients' => $recipients, |
107
|
4 |
|
'MJ-TemplateID' => $this->templateIdGuesserManager->guess($message), |
108
|
4 |
|
'MJ-TemplateLanguage' => 'True', |
109
|
4 |
|
'Headers' => $headers, |
110
|
4 |
|
]; |
111
|
|
|
|
112
|
4 |
|
if (null !== $fromName = current($from)) { |
113
|
1 |
|
$body['FromName'] = $fromName; |
114
|
1 |
|
} |
115
|
|
|
|
116
|
4 |
|
foreach ($message->getChildren() as $child) { |
117
|
2 |
|
if ($child instanceof \Swift_Attachment) { |
118
|
2 |
|
$body['Attachments'][] = [ |
119
|
2 |
|
'Content-type' => $child->getContentType(), |
120
|
2 |
|
'Filename' => $child->getFilename(), |
121
|
2 |
|
'content' => base64_encode($child->getBody()) |
122
|
2 |
|
]; |
123
|
2 |
|
} |
124
|
4 |
|
} |
125
|
|
|
|
126
|
4 |
|
$response = $this->client->post(Resources::$Email, ['body' => $body]); |
127
|
4 |
|
$success = $response->success(); |
128
|
|
|
|
129
|
4 |
|
if ($evt) { |
130
|
3 |
|
$evt->setResult($success ? \Swift_Events_SendEvent::RESULT_SUCCESS : \Swift_Events_SendEvent::RESULT_FAILED); |
131
|
3 |
|
$this->dispatcher->dispatchEvent($evt, 'sendPerformed'); |
132
|
3 |
|
} |
133
|
|
|
|
134
|
4 |
|
if ($success) { |
135
|
2 |
|
return count((array) $message->getTo()); |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
return 0; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Register a plugin in the Transport. |
143
|
|
|
* |
144
|
|
|
* @param Swift_Events_EventListener $plugin |
145
|
|
|
*/ |
146
|
1 |
|
public function registerPlugin(Swift_Events_EventListener $plugin) |
147
|
|
|
{ |
148
|
1 |
|
$this->dispatcher->bindEventListener($plugin); |
149
|
1 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
|
|
public function ping() |
155
|
|
|
{ |
156
|
|
|
return true; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|