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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
<?php
if (! class_exists ( "PostmanWpMail" )) {
/**
* Moved this code into a class so it could be used by both wp_mail() and PostmanSendTestEmailController
*
* @author jasonhendriks
*
*/
class PostmanWpMail {
private $exception;
private $transcript;
private $totalTime;
private $logger;
/**
* Load the dependencies
*/
public function init() {
$this->logger = new PostmanLogger ( get_class ( $this ) );
require_once 'Postman-Mail/PostmanMessage.php';
require_once 'Postman-Email-Log/PostmanEmailLogService.php';
require_once 'Postman-Mail/PostmanMailEngine.php';
require_once 'Postman-Auth/PostmanAuthenticationManagerFactory.php';
require_once 'PostmanState.php';
}
/**
* This methods follows the wp_mail function interface, but implements it Postman-style.
* Exceptions are held for later inspection.
* An instance of PostmanState updates the success/fail tally.
*
* @param unknown $to
* @param unknown $subject
* @param unknown $body
* @param unknown $headers
* @param unknown $attachments
* @return boolean
*/
public function send($to, $subject, $message, $headers = '', $attachments = array()) {
// initialize for sending
$this->init ();
// build the message
$postmanMessage = $this->processWpMailCall ( $to, $subject, $message, $headers, $attachments );
// build the email log entry
$log = new PostmanEmailLog ();
$log->originalTo = $to;
$log->originalSubject = $subject;
$log->originalMessage = $message;
$log->originalHeaders = $headers;
// send the message and return the result
return $this->sendMessage ( $postmanMessage, $log );
}
/**
* Builds a PostmanMessage based on the WordPress wp_mail parameters
*
* @param unknown $to
* @param unknown $subject
* @param unknown $message
* @param unknown $headers
* @param unknown $attachments
*/
private function processWpMailCall($to, $subject, $message, $headers, $attachments) {
$this->logger->trace ( 'wp_mail parameters before applying WordPress wp_mail filter:' );
$this->traceParameters ( $to, $subject, $message, $headers, $attachments );
/**
* Filter the wp_mail() arguments.
*
* @since 1.5.4
*
* @param array $args
* A compacted array of wp_mail() arguments, including the "to" email,
* subject, message, headers, and attachments values.
*/
$atts = apply_filters ( 'wp_mail', compact ( 'to', 'subject', 'message', 'headers', 'attachments' ) );
if (isset ( $atts ['to'] )) {
$to = $atts ['to'];
}
if (isset ( $atts ['subject'] )) {
$subject = $atts ['subject'];
}
if (isset ( $atts ['message'] )) {
$message = $atts ['message'];
}
if (isset ( $atts ['headers'] )) {
$headers = $atts ['headers'];
}
if (isset ( $atts ['attachments'] )) {
$attachments = $atts ['attachments'];
}
if (! is_array ( $attachments )) {
$attachments = explode ( "\n", str_replace ( "\r\n", "\n", $attachments ) );
}
$this->logger->trace ( 'wp_mail parameters after applying WordPress wp_mail filter:' );
$this->traceParameters ( $to, $subject, $message, $headers, $attachments );
// Postman API: register the response hook
add_filter ( 'postman_wp_mail_result', array (
$this,
'postman_wp_mail_result'
) );
// create the message
$postmanMessage = $this->createNewMessage ();
$this->populateMessageFromWpMailParams ( $postmanMessage, $to, $subject, $message, $headers, $attachments );
// return the message
return $postmanMessage;
}
/**
* Creates a new instance of PostmanMessage with a pre-set From and Reply-To
*
* @return PostmanMessage
*/
public function createNewMessage() {
$message = new PostmanMessage ();
$options = PostmanOptions::getInstance ();
// the From is set now so that it can be overridden
$transport = PostmanTransportRegistry::getInstance ()->getActiveTransport ();
$message->setFrom ( $transport->getFromEmailAddress (), $transport->getFromName () );
// the Reply-To is set now so that it can be overridden
$message->setReplyTo ( $options->getReplyTo () );
$message->setCharset ( get_bloginfo ( 'charset' ) );
return $message;
}
/**
* A convenient place for any code to inject a constructed PostmanMessage
* (for example, from MyMail)
*
* The body parts may be set already at this time.
*
* @param PostmanMessage $message
* @return boolean
*/
public function sendMessage(PostmanMessage $message, PostmanEmailLog $log) {
// get the Options and AuthToken
$options = PostmanOptions::getInstance ();
$authorizationToken = PostmanOAuthToken::getInstance ();
// get the transport and create the transportConfig and engine
$transport = PostmanTransportRegistry::getInstance ()->getActiveTransport ();
// create the Mail Engine
$engine = $transport->createMailEngine ();
// add plugin-specific attributes to PostmanMessage
$message->addHeaders ( $options->getAdditionalHeaders () );
$message->addTo ( $options->getForcedToRecipients () );
$message->addCc ( $options->getForcedCcRecipients () );
$message->addBcc ( $options->getForcedBccRecipients () );
// apply the WordPress filters
// may impact the from address, from email, charset and content-type
$message->applyFilters ();
// create the body parts (if they are both missing)
if ($message->isBodyPartsEmpty ()) {
$message->createBodyParts ();
}
// is this a test run?
$testMode = apply_filters ( 'postman_test_email', false );
if ($this->logger->isDebug ()) {
$this->logger->debug ( 'testMode=' . $testMode );
}
// start the clock
$startTime = microtime ( true ) * 1000;
try {
// prepare the message
$message->validate ( $transport );
// send the message
if ($options->getRunMode () == PostmanOptions::RUN_MODE_PRODUCTION) {
if ($transport->isLockingRequired ()) {
PostmanUtils::lock ();
// may throw an exception attempting to contact the OAuth2 provider
$this->ensureAuthtokenIsUpdated ( $transport, $options, $authorizationToken );
}
$this->logger->debug ( 'Sending mail' );
// may throw an exception attempting to contact the SMTP server
$engine->send ( $message );
// increment the success counter, unless we are just tesitng
if (! $testMode) {
PostmanState::getInstance ()->incrementSuccessfulDelivery ();
}
}
// clean up
$this->postSend ( $engine, $startTime, $options, $transport );
if ($options->getRunMode () == PostmanOptions::RUN_MODE_PRODUCTION || $options->getRunMode () == PostmanOptions::RUN_MODE_LOG_ONLY) {
// log the successful delivery
PostmanEmailLogService::getInstance ()->writeSuccessLog ( $log, $message, $engine->getTranscript (), $transport );
}
// return successful
return true;
} catch ( Exception $e ) {
// save the error for later
$this->exception = $e;
// write the error to the PHP log
$this->logger->error ( get_class ( $e ) . ' code=' . $e->getCode () . ' message=' . trim ( $e->getMessage () ) );
// increment the failure counter, unless we are just tesitng
if (! $testMode && $options->getRunMode () == PostmanOptions::RUN_MODE_PRODUCTION) {
PostmanState::getInstance ()->incrementFailedDelivery ();
}
// clean up
$this->postSend ( $engine, $startTime, $options, $transport );
if ($options->getRunMode () == PostmanOptions::RUN_MODE_PRODUCTION || $options->getRunMode () == PostmanOptions::RUN_MODE_LOG_ONLY) {
// log the failed delivery
PostmanEmailLogService::getInstance ()->writeFailureLog ( $log, $message, $engine->getTranscript (), $transport, $e->getMessage () );
}
// return failure
return false;
}
}
/**
* Clean up after sending the mail
*
* @param PostmanZendMailEngine $engine
* @param unknown $startTime
*/
private function postSend(PostmanMailEngine $engine, $startTime, PostmanOptions $options, PostmanModuleTransport $transport) {
// save the transcript
$this->transcript = $engine->getTranscript ();
// log the transcript
if ($this->logger->isTrace ()) {
$this->logger->trace ( 'Transcript:' );
$this->logger->trace ( $this->transcript );
}
// delete the semaphore
if ($transport->isLockingRequired ()) {
PostmanUtils::unlock ();
}
// stop the clock
$endTime = microtime ( true ) * 1000;
$this->totalTime = $endTime - $startTime;
}
/**
* Returns the result of the last call to send()
*
* @return multitype:Exception NULL
*/
function postman_wp_mail_result() {
$result = array (
'time' => $this->totalTime,
'exception' => $this->exception,
'transcript' => $this->transcript
);
return $result;
}
/**
*/
private function ensureAuthtokenIsUpdated(PostmanModuleTransport $transport, PostmanOptions $options, PostmanOAuthToken $authorizationToken) {
assert ( ! empty ( $transport ) );
assert ( ! empty ( $options ) );
assert ( ! empty ( $authorizationToken ) );
// ensure the token is up-to-date
$this->logger->debug ( 'Ensuring Access Token is up-to-date' );
// interact with the Authentication Manager
$wpMailAuthManager = PostmanAuthenticationManagerFactory::getInstance ()->createAuthenticationManager ();
if ($wpMailAuthManager->isAccessTokenExpired ()) {
$this->logger->debug ( 'Access Token has expired, attempting refresh' );
$wpMailAuthManager->refreshToken ();
$authorizationToken->save ();
}
}
/**
* Aggregates all the content into a Message to be sent to the MailEngine
*
* @param unknown $to
* @param unknown $subject
* @param unknown $body
* @param unknown $headers
* @param unknown $attachments
*/
private function populateMessageFromWpMailParams(PostmanMessage $message, $to, $subject, $body, $headers, $attachments) {
$message->addHeaders ( $headers );
$message->setBody ( $body );
$message->setSubject ( $subject );
$message->addTo ( $to );
$message->setAttachments ( $attachments );
return $message;
}
/**
* Trace the parameters to aid in debugging
*
* @param unknown $to
* @param unknown $subject
* @param unknown $body
* @param unknown $headers
* @param unknown $attachments
*/
private function traceParameters($to, $subject, $message, $headers, $attachments) {
$this->logger->trace ( 'to:' );
$this->logger->trace ( $to );
$this->logger->trace ( 'subject:' );
$this->logger->trace ( $subject );
$this->logger->trace ( 'headers:' );
$this->logger->trace ( $headers );
$this->logger->trace ( 'attachments:' );
$this->logger->trace ( $attachments );
$this->logger->trace ( 'message:' );
$this->logger->trace ( $message );
}
}
}
|