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