This repository was archived by the owner on Jan 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathemail_service.php
More file actions
211 lines (176 loc) · 5.21 KB
/
email_service.php
File metadata and controls
211 lines (176 loc) · 5.21 KB
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
<?php
/**
* Email Service Component class for Amazon SES
*
* Send e-mails with Amazon Simple Email Service
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* Based on original class from: http://bakery.cakephp.org/articles/dankroad/2011/01/30/integrate_amazon_simple_email_service_ses_into_existing_application
*
* Usage:
*
* Load EmailService in your controller components
* var $components = array('EmailService');
*
* In your actions choose aws_ses as delivery method
* $this->EmailService->delivery = 'aws_ses'; // or 'aws_ses_raw' if you want complex e-mail with attachments
*
* @author Lucas Ferreira
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @copyright Copyright 2011, Burn web.studio - http://www.burnweb.com.br/
* @version 1.1b
*/
App::import('Vendor', 'aws-sdk', array('file' => 'sdk.class.php'));
App::import('Component', 'Email');
class EmailServiceComponent extends EmailComponent {
var $ses_options = array();
var $response = null;
function getAmazonSES()
{
$ses = new AmazonSES();
return $ses;
}
function _aws_ses_raw()
{
$ses = $this->getAmazonSES();
// remove unnecessary headers...
$uhs[] = '0: This part of the E-mail should never be seen. If';
$uhs[] = '1: you are reading this, consider upgrading your e-mail';
$uhs[] = '2: client to a MIME-compatible client.';
foreach($uhs as $uh)
{
if(($p=array_search($uh, $this->__header)))
{
unset($this->__header[$p]);
}
}
if(!is_array($this->to)) $this->to = array($this->to);
$this->__header[] = 'To: ' . implode(', ', array_map(array($this, '_formatAddress'), $this->to));
$this->__header[] = 'Subject: ' . $this->_encode($this->subject);
$header = implode("\r\n", $this->__header);
$message = implode("\r\n", $this->__message);
$data = base64_encode($header . "\r\n\r\n" . $message . "\r\n\r\n\r\n.");
$options = array_merge($this->ses_options, array(
'Source' => $this->from
));
$this->response = $ses->send_raw_email(array('Data' => $data), $options);
$ok = $this->response->isOK();
if(!$ok)
{
$this->log('Error sending raw email from AWS SES: ' . $this->response->body->asXML(), 'debug');
}
return $ok;
}
function _aws_ses()
{
$ses = $this->getAmazonSES();
$destination = array(
'ToAddresses' => explode(',', $this->to)
);
if($this->cc && !empty($this->cc))
{
$destination = array_merge($destination, array(
'CcAddresses' => $this->cc
));
}
if($this->bcc && !empty($this->bcc))
{
$destination = array_merge($destination, array(
'BccAddresses' => $this->bcc
));
}
$options = $this->ses_options;
if($this->replyTo && !empty($this->replyTo))
{
$options = array_merge($options, array(
'ReplyToAddresses' => explode(',', $this->replyTo)
));
}
$message = array(
'Subject' => array(
'Data' => $this->subject,
'Charset' => $this->charset
),
'Body' => array()
);
if($this->textMessage != null)
{
$message['Body']['Text'] = array(
'Data' => $this->textMessage,
'Charset' => $this->charset
);
}
if($this->htmlMessage != null)
{
$message['Body']['Html'] = array(
'Data' => $this->htmlMessage,
'Charset' => $this->charset
);
}
$this->response = $ses->send_email($this->from, $destination, $message, $options);
$ok = $this->response->isOK();
if(!$ok)
{
$this->log('Error sending email from AWS SES: ' . $this->response->body->asXML(), 'debug');
}
return $ok;
}
function verifyEmailAddress($email)
{
$ses = $this->getAmazonSES();
$this->response = $ses->verify_email_address($email, $this->ses_options);
$ok = $this->response->isOK();
if(!$ok)
{
$this->log('Error verify email address from AWS SES: ' . $this->response->body->asXML(), 'debug');
}
return $ok;
}
function deleteVerifiedEmailAddress($email)
{
$ses = $this->getAmazonSES();
$this->response = $ses->delete_verified_email_address($email, $this->ses_options);
$ok = $this->response->isOK();
if(!$ok)
{
$this->log('Error delete_verified_email_address from AWS SES: ' . $this->response->body->asXML(), 'debug');
}
return $ok;
}
function listVerifiedEmailAddresses()
{
$ses = $this->getAmazonSES();
$this->response = $ses->list_verified_email_addresses($this->ses_options);
$ok = $this->response->isOK();
if(!$ok)
{
$this->log('Error list_verified_email_addresses from AWS SES: ' . $this->response->body->asXML(), 'debug');
}
return $this->response->body;
}
function getSendQuota()
{
$ses = $this->getAmazonSES();
$this->response = $ses->get_send_quota($this->ses_options);
$ok = $this->response->isOK();
if(!$ok)
{
$this->log('Error get send quota from AWS SES: ' . $this->response->body->asXML(), 'debug');
}
return $this->response->body;
}
function getSendStatistics()
{
$ses = $this->getAmazonSES();
$this->response = $ses->get_send_statistics($this->ses_options);
$ok = $this->response->isOK();
if(!$ok)
{
$this->log('Error get send statistics from AWS SES: ' . $this->response->body->asXML(), 'debug');
}
return $this->response->body;
}
}
?>