/
index.php
317 lines (282 loc) · 9.24 KB
/
index.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
require_once 'vendor/autoload.php';
use Aws\Ses\SesClient;
use Aws\Ses\Exception\MessageRejectedException;
class SesRawMailer
{
/*
* Our SES client
*/
protected $sesClient;
/**
* Create a new mailer
* @param SesClient $sesClient The SES Client to send via
*/
public function __construct(SesClient $sesClient)
{
$this->client = $sesClient;
}
/**
* Getter for the ses client
* @return SesClient our client
*/
public function getClient()
{
return $this->client;
}
/**
* Sends out a raw email and returns the message id on success
* @param string $to The recipients email address
* @param string $subject The subject of the email
* @param string $message The plain text version of the email
* @param string $from The senders address
* @param string $html The HTML version of the email
* @param array $attachments The full file path of all the attachments
* @param string $returnPath The return path of the email, although in reality this is ignored in favour of from?
* @return string The message id returned from Amazon
*/
public function send($to, $subject, $message, $from, $html = '', $attachments = array(), $returnPath = '')
{
if ($returnPath)
{
$returnPath = <<<EOF
Return-Path: {$returnPath}
EOF;
}
//Create a random boundary
$boundary = sha1(rand() . time() . 'Arron');
//lets get started, the headers come first. Pay attention to the blank lines, they are important.
//Following the headers is our first part of the email. The plain text version.
$rawEmail = <<<EOE
Subject: {$subject}
MIME-Version: 1.0
Content-type: multipart/alternative; boundary="{$boundary}"{$returnPath}
To: {$to}
From: {$from}
--{$boundary}
Content-Type: text/plain;
{$message}
EOE;
//if we have some html set, lets create a new part and add it
if ($html)
{
$rawEmail .= <<<EOE
--{$boundary}
Content-Type: text/html; charset=iso-8859-1
{$html}
EOE;
}
//loop through our attachments
foreach ($attachments as $attachment)
{
//ensure we can access the file
if (file_exists($attachment))
{
//get all the meta information we need
$contentType = $this->mimeType($attachment);
$size = filesize($attachment);
$attachmentName = basename($attachment);
//base64 encode our attachment content
$attachmentContent = base64_encode(file_get_contents($attachment));
$rawEmail .= <<<EOE
--{$boundary}
Content-Type: {$contentType}; name="{$attachmentName}"
Content-Description: "{$attachmentName}"
Content-Disposition: attachment; filename="{$attachmentName}"; size={$size};
Content-Transfer-Encoding: base64
{$attachmentContent}
EOE;
}
}
//finish off our email with the boundary
$rawEmail .= <<<EOE
--{$boundary}--
EOE;
//set up the arguments to pass to the client. You can set 'Source' in
//here, but I encountered errors. So found setting it in the headers worked
//best.
$args = array(
'RawMessage' => array(
'Data' => base64_encode($rawEmail),
),
);
try
{
$response = $this->getClient()->sendRawEmail($args);
return $response->get('MessageId');
}
catch (MessageRejectedException $mrEx)
{
$this->log('Unable to send email: Rejected. ' . $mrEx->getMessage());
}
catch (\Exception $ex)
{
$this->log('Unable to send email: Unknown. ' . $ex->getMessage());
}
return false;
}
/**
* Really bad method to log stuff. Just a proof of concept really.
* You should definately replace this with a decent log.
*
* @param string $message the message to log
*/
protected function log($message)
{
echo '<p class="error">' . strip_tags($message) . '</p>';
}
/**
* Simple, method to get mime type based on extension. Again, you really
* want to replace this with something better.
*
* @param string $file the full path to the file
* @return string the mime type, or null on failure
*/
protected function mimeType($file)
{
if (file_exists($file) && $extension = pathinfo($file, PATHINFO_EXTENSION))
{
//taken from https://github.com/laravel/laravel/blob/3.0/application/config/mimes.php
$mimes = array(
'hqx' => 'application/mac-binhex40',
'cpt' => 'application/mac-compactpro',
'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream'),
'bin' => 'application/macbinary',
'dms' => 'application/octet-stream',
'lha' => 'application/octet-stream',
'lzh' => 'application/octet-stream',
'exe' => array('application/octet-stream', 'application/x-msdownload'),
'class' => 'application/octet-stream',
'psd' => 'application/x-photoshop',
'so' => 'application/octet-stream',
'sea' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'oda' => 'application/oda',
'pdf' => array('application/pdf', 'application/x-download'),
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'smi' => 'application/smil',
'smil' => 'application/smil',
'mif' => 'application/vnd.mif',
'xls' => array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'),
'ppt' => array('application/powerpoint', 'application/vnd.ms-powerpoint'),
'wbxml' => 'application/wbxml',
'wmlc' => 'application/wmlc',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dxr' => 'application/x-director',
'dvi' => 'application/x-dvi',
'gtar' => 'application/x-gtar',
'gz' => 'application/x-gzip',
'php' => array('application/x-httpd-php', 'text/x-php'),
'php4' => 'application/x-httpd-php',
'php3' => 'application/x-httpd-php',
'phtml' => 'application/x-httpd-php',
'phps' => 'application/x-httpd-php-source',
'js' => 'application/x-javascript',
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'tar' => 'application/x-tar',
'tgz' => array('application/x-tar', 'application/x-gzip-compressed'),
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'zip' => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'),
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => array('audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'),
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'ra' => 'audio/x-realaudio',
'rv' => 'video/vnd.rn-realvideo',
'wav' => 'audio/x-wav',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'jpeg' => array('image/jpeg', 'image/pjpeg'),
'jpg' => array('image/jpeg', 'image/pjpeg'),
'jpe' => array('image/jpeg', 'image/pjpeg'),
'png' => 'image/png',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'shtml' => 'text/html',
'txt' => 'text/plain',
'text' => 'text/plain',
'log' => array('text/plain', 'text/x-log'),
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'xml' => 'text/xml',
'xsl' => 'text/xml',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'doc' => 'application/msword',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'word' => array('application/msword', 'application/octet-stream'),
'xl' => 'application/excel',
'eml' => 'message/rfc822',
'json' => array('application/json', 'text/json'),
);
if (array_key_exists($extension, $mimes))
{
return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
}
}
}
}
//Consider replacing this with the newer credential profile
$client = SesClient::factory(array(
'region' => 'eu-west-1', //replace with your own region
'key' => 'Your key', //replace with your own key
'secret' => 'Your secret', //as you guessed, replace with your own secret
));
$to = 'hello@arrow-web.co.uk';
$subject = 'Testing the blog';
//You'll need to be able to send as this address in SES
$from = 'sender@example.com';
//our plain text message
$message = <<<EOF
Hi Arron,
Just testing the class works that you posted on the blog.
Cheers
Dave
EOF;
//our html message
$html = <<<EOF
<html>
<body>
<p><strong>Hi Arron,</strong></p>
<p>Just testing the class works that you posted on the <a href="http://www.arrow-web.co.uk/">blog</a>.</p>
<p>Cheers</p>
<p>Dave</p>
</body>
</html>
EOF;
//our attachments - these are relative to the current file
$attachments = array(
'test-attachment1.pdf',
'test-attachment2.png',
);
$mailer = new SesRawMailer($client);
$messageId = $mailer->send($to, $subject, $message, $from, $html, $attachments);
if ($messageId)
{
echo "<p>Successfully sent message: " . strip_tags($messageId) . "</p>";
}
else
{
echo "<p>Something went wrong. :( (check the logs)</p>";
}