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