-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostmarkapp.php
277 lines (244 loc) · 7.55 KB
/
Postmarkapp.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
<?php
/**
* Postmarkapp is an application designed to work with http://www.postmarkapp.com for sending out emails to clients. The application can be used
* as a standalone and also comes with an adapter.
*
*/
class Postmarkapp extends PVStaticInstance {
protected $_mail;
protected $_options;
protected $_configuration;
/**
* The constructor for the Postmarkapp configureds how the postmarkapp will operate
*
* @param array $data The initial data that can be used to set the values without using a mutator method. The values can be as values:
* -'HtmlBody' _string_: The html body of the message
* -'TextBody' T_string_ The text body of the message
* -'To' _string_: The person that is receiving the message
* -'Cc ' _string_: Who the carbon copy will be sent too.
* -'Bcc' _string_: Who will receive a blind copy
* -'Subject' _string_ The subject of the email going out
* @param array $configuration Configuration options that can be used to determ how postmark will execute
* -'host' _string_: Determines to use regular connection or an ssl connection. Default is normal, change to ssl for ssl connections
* -'protocol' _string_: The protocol used to send the mail. Default is curl for sending through curl. Other is SMTP but that is not recommended
* @param array $options Options that are used to find the sending
* -'host' _string_: The host that the message will be sent too
* -'token' _string: The security token provided by postmarkapp. Set as a define before application is called.
* -'auth_header' The authorization header sent too postmark. Default is X-Postmark-Server-Token:
* -'application_type' : The application type used to set the format. Default is 'application/json'
*
* @return void
* @access public
*/
public function __construct(array $data = array(), array $configuration = array(), array $options = array()) {
$this -> _mail = $data;
$configuration_defaults = array(
'host' => 'normal',
'protocol' => 'curl',
);
$this -> _configuration = $configuration += $configuration_defaults;
$option_defaults = array(
'host' => array('normal' => 'http://api.postmarkapp.com/email', 'ssl' => 'https://api.postmarkapp.com/email'),
'token' => POSTMARK_API_KEY,
'auth_header' => 'X-Postmark-Server-Token: ',
'application_type' => 'application/json'
);
$this -> _options = $options += $option_defaults;
}
/**
* Sets the HTML body of the email to be sent for clients that are able to receieve html emails
*
* @param string $message
*
* @return void
* @access public
*/
public function setHtmlMessage($message) {
$this -> _mail['HtmlBody'] = $message;
}
/**
* Sets the text message for clients that are not able to receieve html emails.
*
* @param string $message
*
* @return void
* @access public
*/
public function setTextMessage($message) {
$this -> _mail['TextBody'] = $message;
}
/**
* Set the email address that will be receive the email. In others words, who the email is going too.
*
* @param string $email
*
* @return void
* @access public
*/
public function setReceiver($message) {
$this -> _mail['To'] = $message;
}
/**
* Set email address that will receive carbon copy of the emails.
*
* @param string $email
*
* @return void
* @access public
*/
public function setCarbonCopy($message) {
$this -> _mail['Cc'] = $message;
}
/**
* Set email address that will receive blind copies of the email.
*
* @param string $email
*
* @return void
* @access public
*/
public function setBlindCopy($message) {
$this -> _mail['Bcc'] = $message;
}
/**
* Set the sender email who the email will be coming from.
*
* @param string $email
*
* @return void
* @access public
*/
public function setSender($message) {
$this -> _mail['From'] = $message;
}
/**
* Set the email address to go in the ReplyTo option.
*
* @param string $email
*
* @access public
* @return void
*/
public function setReplyTo($message) {
$this -> _mail['ReplyTo'] = $message;
}
/**
* Sets the subject line in the email
*
* @param string $subject
*
* @return void
* @access public
*/
public function setSubject($message) {
$this -> _mail['Subject'] = $message;
}
/**
* Sets a tag that is used by Postmarkapp, maybe for something else
*
* @param string $tag
*
* @return void
* @access public
* @todo find out more about tags
*/
public function setTag($message) {
$this -> _mail['Tag'] = $message;
}
/**
* Adds an attachment to the email to be sent
*
* @param string $name The name of the attachement
* @param string $file The location of the file to be sent. Make sure file is accessible
* @param string $content_type The content type of the file
*
* @return void
* @access public
*/
public function addAttachment($name, $file, $content_type) {
if(!isset($this -> _mail['Attachments']) && !is_array($this -> _mail['Attachments']))
$this -> _mail['Attachments'] = array();
$this -> _mail['Attachments'][]= array(
'Name' => $name,
'Content' => $file,
'ContentType' => $content_type
);
}
/**
* Adds a custom header to the email that will be sent.
*
* @param string $name The name of header (ex: 'Content-Type' )
* @param string $value The value associated with the header (ex: 'text/html')
*
* @return void
* @access public
*/
public function addHeader($name, $value) {
if(!isset($this -> _mail['Headers']) && !is_array($this -> _mail['Headers']))
$this -> _mail['Headers'] = array();
$this -> _mail['Headers'][]= array(
'Name' => $name,
'Value' => $value,
);
}
/**
* Only use send after the required parameters have been set. The sender, receiver, html message and text message should all be sent. Uses a notifer
* for finding out results or returns the result
*
* @return void
* @access
*/
public function send() {
$this -> _convertFormat();
$data = $this -> _sendCurl();
$this->_notify('Postmarkapp::send', $this -> _mail, $data);
return $data;
}
/**
* Sends the email through using curl. Curl is the default option set in the protocol when the class is instantiated.
*
* @return void
* @access public
*/
protected function _sendCurl() {
$headers = array(
'Accept: '.$this -> _options['application_type'],
'Content-Type: '. $this -> _options['application_type'],
$this -> _options['auth_header'].' '.$this -> _options['token'],
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this -> _options['host'][$this-> _configuration['host']]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this -> _mail));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/**
* Ensures that all string data entered into Postmark is UTF-8 format. Postmarkapp only allows utf-8
* data to be sent.
*
* @return void
* @access protected
*/
protected function _convertFormat($format = 'utf-8') {
if(is_array($this -> _mail)){
foreach($this -> _mail as $key => $value) {
if(is_string($value)) {
$this -> _mail[$key] = utf8_encode($value);
}
}//end foreach
}
}
/**
* Sends the email using SMTP. According to the Postmarkapp documention, this method is not robust as curl.
*
* @todo implement method
*/
protected function _sendSmtp() {
}
}