-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAwsSesTransport.php
180 lines (158 loc) · 6.58 KB
/
AwsSesTransport.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/*
* This file requires SwiftMailer and Aws Ses PHP Api (V2 or V3)
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use AwsSesWrapper\AwsSesWrapper;
/**
* Build transports for the various AWS SES API.
*
* @package Swift
* @subpackage Transport
* @author Francesco Gabbrielli
*/
abstract class Swift_AwsSesTransport extends Swift_Transport_AwsSesTransport
{
public function __construct($client, $catch_exception=false, $debug=false)
{
call_user_func_array(
array($this, 'Swift_Transport_AwsSesTransport::__construct'),
array_merge(
Swift_DependencyContainer::getInstance()
->createDependenciesFor('transport.aws_ses'),
[$client, $catch_exception, $debug]
));
}
private static function wrapClient($ses_client, $configuration_set) {
return $ses_client instanceof AwsSesWrapper
? $ses_client
: AwsSesWrapper($ses_client, $configuration_set);
}
/**
* Utility method to directly build an Aws SesClient wrapper
*
* @param string $region Set the correct endpoint region.
* http://docs.aws.amazon.com/general/latest/gr/rande.html#ses_region
* @param string $profile AWS IAM profile
* @param string $configuration_set Configuration Set on AWS SES (or null for v2 api)
* @return AwsSesClient
*/
public static function newClient($region, $profile, $configuration_set) {
return AwsSesWrapper::factory($region, $profile, $configuration_set);
}
/**
* Create a new sendRawEmail transport.
*
* This is the only one to allow attachments.
*
* @param SesClient $ses_client Aws SesClient or its wrapper
* @param string $configuration_set Configuration Set on AWS SES (or null for v2 api)
* @return \Swift_AwsSesTransport the transport
*/
public static function newRawInstance($ses_client, $configuration_set=null)
{
return new Swift_AwsSesRawTransport(
Swift_AwsSesTransport::wrapClient($ses_client, $configuration_set));
}
/**
* Create a new sendEmail transport. The easiest type. Requires Html2Text.
*
* @param SesClient $ses_client Aws SesClient or its wrapper
* @param string $configuration_set Configuration Set on AWS SES (or null for v2 api)
* @return \Swift_AwsSesTransport the transport
*/
public static function newFormattedInstance($ses_client, $configuration_set = null)
{
return new Swift_AwsSesFormattedTransport(
Swift_AwsSesTransport::wrapClient($ses_client, $configuration_set));
}
/**
* Create a new sendTemplatedEmail transport
*
* @param SesClient $ses_client Aws SesClient or its wrapper
* @param string $configuration_set Configuration Set on AWS SES (not null)
* @param mixed $template The name of the template or its json definition
* (only the contents of the 'Template' element) to force creation
* if template does not exists.
* @return \Swift_AwsSesTemplatedTransport
*/
public static function newTemplatedInstance($ses_client, $configuration_set, $template)
{
return new Swift_AwsSesTemplatedTransport(
Swift_AwsSesTransport::wrapClient($ses_client, $configuration_set),
$template);
}
/**
* Create a new sendBulkTemplatedEmail transport
*
* @param SesClient $ses_client Aws SesClient or its wrapper
* @param string $configuration_set Configuration Set on AWS SES
* (only the contents of the 'Template' element) to force creation
* if template does not exists.
* @param mixed $template The name of the template or its json definition
* @return \Swift_AwsSesTemplatedTransport
*/
public static function newBulkInstance($ses_client, $configuration_set, $template)
{
return new Swift_AwsSesBulkTransport(
Swift_AwsSesTransport::wrapClient($ses_client, $configuration_set),
$template);
}
/**
* Send the given Message.
*
* Recipient/sender data will be retrieved from the Message API if necessary
*
* @param Swift_Mime_SimpleMessage $message
* @param string[] &$failedRecipients to collect failures by-reference
* @return int number of recipients who were accepted for delivery (or Promise if async)
* @throws Exception on any errors if $catch_exception is false
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
{
$failedRecipients = (array) $failedRecipients;
$this->beforeSendPerformed($message);
$count = 0;
try
{
// enforce from
$from = $message->getSender() ?: $message->getFrom();
$this->client->setFrom(join(",", $this->mail_string($from)));
$result = $this->do_send($message, $failedRecipients);
if ($this->client->isAsync())
{
$result->then(function($result) use ($message) {
$this->onResponse($message, $result, $failedRecipients);
});
return $result;
}
else
$count = $this->onResponse($message, $result, $failedRecipients);
} catch (Exception $e) {
$failedRecipients = $this->getDestinations($message);
if (!$this->catch_exception)
throw $e;
}
return $count;
}
/**
* Executed when the response from AWS is received
*
* @param Swift_Mime_SimpleMessage $message the message
* @param Aws\Result $response the AWS response
* @param array $failedRecipients failed recipients
* @return int the total number of recipients
*/
protected function onResponse($message, $response, &$failedRecipients)
{
$this->_debug("=== Start AWS Response ===");
$this->_debug($response);
$this->_debug("=== End AWS Response ===");
$ret = $this->do_sent($message, $response, $failedRecipients);
$this->sendPerformed($message, $response, $failedRecipients);
return $ret;
}
protected abstract function do_sent($message, $response, &$failedRecipients);
}