-
Notifications
You must be signed in to change notification settings - Fork 2
/
SesTransport.php
126 lines (105 loc) · 3.74 KB
/
SesTransport.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
<?php
namespace UltimateGuitar\SwiftSesTransport;
use Swift_Transport;
use Swift_Mime_SimpleMessage;
use Swift_Events_EventListener;
use Swift_DependencyContainer;
use UltimateGuitar\SwiftSesTransport\Exceptions\AWSConnectionException;
use Swift_Events_SendEvent;
class SesTransport implements Swift_Transport
{
public const DEFAULT_ENDPOINT = 'https://email.us-east-1.amazonaws.com/';
private $event_dispatcher;
/** the service access key */
public $key_id;
/** the service secret key */
public $secret_key;
/** the service endpoint */
public $endpoint;
/** the response */
public $response;
public function __construct($key_id = null , $secret_key = null, $endpoint = self::DEFAULT_ENDPOINT) {
Swift_DependencyContainer::getInstance()
->register('transport.aws')
->withDependencies(array('transport.eventdispatcher'));
call_user_func_array(
array($this, 'setEventDispatcher'),
Swift_DependencyContainer::getInstance()->createDependenciesFor('transport.aws')
);
$this->key_id = $key_id;
$this->secret_key = $secret_key;
$this->endpoint = $endpoint;
}
private function setEventDispatcher(\Swift_Events_EventDispatcher $dispatcher)
{
$this->event_dispatcher = $dispatcher;
}
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
{
if ($evt = $this->event_dispatcher->createSendEvent($this, $message))
{
$this->event_dispatcher->dispatchEvent($evt, 'beforeSendPerformed');
if ($evt->bubbleCancelled())
{
return 0;
}
}
$this->response = $this->proceed($message, $failedRecipients);
$success = (200 == $this->response->code);
$resp_event = $this->event_dispatcher->createResponseEvent($this, new SwiftResponse($message, $this->response->xml), $success);
if ($resp_event)
{
$this->event_dispatcher->dispatchEvent($resp_event, 'responseReceived');
}
if ($evt)
{
$evt->setResult($success ? Swift_Events_SendEvent::RESULT_SUCCESS : Swift_Events_SendEvent::RESULT_FAILED);
$this->event_dispatcher->dispatchEvent($evt, 'sendPerformed');
}
if ($success)
{
return count((array)$message->getTo());
}
else
{
return 0;
}
}
public function registerPlugin(Swift_Events_EventListener $plugin)
{
$this->event_dispatcher->bindEventListener($plugin);
}
private function proceed( Swift_Mime_SimpleMessage $message, &$failedRecipients = null )
{
$date = date( 'D, j F Y H:i:s O' );
$hmac = base64_encode( hash_hmac( 'sha1', $date, $this->secret_key, true ) );
$auth = "AWS3-HTTPS AWSAccessKeyId=" . $this->key_id . ", Algorithm=HmacSHA1, Signature=" . $hmac;
$host = parse_url( $this->endpoint, PHP_URL_HOST );
$path = parse_url( $this->endpoint, PHP_URL_PATH );
$fp = fsockopen( 'ssl://' . $host , 443, $errno, $errstr, 30 );
if( ! $fp ) {
throw new AWSConnectionException( "$errstr ($errno)" );
}
$socket = new Socket( $fp, $host, $path );
$socket->header("Date", $date);
$socket->header("X-Amzn-Authorization", $auth);
$socket->write("Action=SendRawEmail&RawMessage.Data=");
$ais = new AWSInputByteStream($socket);
$message->toByteStream($ais);
$ais->flushBuffers();
$result = $socket->read();
return $result;
}
public function isStarted()
{
}
public function start()
{
}
public function stop()
{
}
public function ping()
{
}
}