-
Notifications
You must be signed in to change notification settings - Fork 15
/
wp-mail.php
344 lines (288 loc) · 10.3 KB
/
wp-mail.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
require_once dirname( __FILE__ ) . '/postmark.php';
/**
* Determine MIME Content Type.
*
* @param string $filename Filename.
*/
function postmark_determine_mime_content_type( $filename ) {
if ( function_exists( 'mime_content_type' ) ) {
return mime_content_type( $filename );
} elseif ( function_exists( 'finfo_open' ) ) {
$finfo = finfo_open( FILEINFO_MIME_TYPE );
$mime_type = finfo_file( $finfo, $filename );
finfo_close( $finfo );
return $mime_type;
} else {
return 'application/octet-stream';
}
}
/**
* Builds From string (Display Name <[email protected]>) if wp_mail_from_name filter used.
*
* @param string $from From email address.
* @param string $from_name From header display name.
*/
function build_from_header_with_name( $from, $from_name ) {
// Check if email address is already in Display Name <[email protected]> format.
if ( false !== strpos( $from, '<' ) && false !== strpos( $from, '>' ) ) {
$email_address_start = strpos( $from, '<' ) + 1;
$email_address_end = strpos( $from, '>' );
$email_address = substr( $from, $email_address_start, ( $email_address_end - $email_address_start ) );
} else {
$email_address = $from;
}
return $from_name . ' <' . $email_address . '>';
}
/**
* WP Mail.
*
* @param [type] $to TO.
* @param [type] $subject Subject.
* @param [type] $message Message.
* @param string $headers Headers.
* @param array $attachments Attachments.
*/
function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
// Compact the input, apply the filters, and extract them back out.
$atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) );
extract( $atts );
// Support using pre_wp_mail filter to short-circuit email sending.
$pre_wp_mail = apply_filters( 'pre_wp_mail', null, $atts );
if ( null !== $pre_wp_mail ) {
return $pre_wp_mail;
}
$settings = include POSTMARK_DIR . '/postmark-settings.php';
$settings = $settings['settings'];
if ( ! is_array( $attachments ) ) {
$attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
}
if ( ! empty( $headers ) && ! is_array( $headers ) ) {
$headers = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
}
/*
==================================================
Parse headers
==================================================
*/
$recognized_headers = array();
$headers_list = array(
'Content-Type' => array(),
'Cc' => array(),
'Bcc' => array(),
'Reply-To' => array(),
'From' => array(),
'X-PM-Track-Opens' => array(),
'X-PM-TrackLinks' => array(),
'X-PM-Tag' => array()
);
$headers_list_lowercase = array_change_key_case( $headers_list, CASE_LOWER );
if ( ! empty( $headers ) ) {
foreach ( $headers as $key => $header ) {
$key = strtolower( $key );
if ( array_key_exists( $key, $headers_list_lowercase ) ) {
$header_key = $key;
$header_val = $header;
$segments = explode( ':', $header );
if ( 2 === count( $segments ) ) {
if ( array_key_exists( strtolower( $segments[0] ), $headers_list_lowercase ) ) {
list( $header_key, $header_val ) = $segments;
$header_key = strtolower( $header_key );
}
}
} else {
$segments = explode( ':', $header );
if ( 2 === count( $segments ) ) {
if ( array_key_exists( strtolower( $segments[0] ), $headers_list_lowercase ) ) {
list( $header_key, $header_val ) = $segments;
$header_key = strtolower( $header_key );
}
}
}
// If the key was detected, assign it.
if ( isset( $header_key ) && isset( $header_val ) ) {
if ( false === stripos( $header_val, ',' ) ) {
$headers_list_lowercase[ $header_key ][] = trim( $header_val );
} else {
$vals = explode( ',', $header_val );
foreach ( $vals as $val ) {
$headers_list_lowercase[ $header_key ][] = trim( $val );
}
}
unset( $header_key );
unset( $header_val );
}
}
foreach ( $headers_list as $key => $value ) {
$value = $headers_list_lowercase[ strtolower( $key ) ];
if ( count( $value ) > 0 ) {
$recognized_headers[ $key ] = implode( ', ', $value );
}
}
}
/*
==================================================
Content-Type hook
==================================================
*/
$content_type = 'text/plain';
if ( isset( $recognized_headers['Content-Type'] ) ) {
if ( false !== strpos( $recognized_headers['Content-Type'], 'text/html' ) ) {
$content_type = 'text/html';
}
}
$content_type = apply_filters( 'wp_mail_content_type', $content_type );
/*
==================================================
Generate POST payload
==================================================
*/
// Allow overriding the From address when specified in the headers.
$from = $settings['sender_address'];
$force_from = isset( $settings['force_from'] ) && $settings['force_from'];
if ( false === $force_from && isset( $recognized_headers['From'] ) ) {
$from = $recognized_headers['From'];
}
// Support using wp_mail_from_name filter.
$from_name = '';
$from_name = apply_filters( 'wp_mail_from_name', $from_name );
if ( ! empty ( $from_name ) ) {
$from = build_from_header_with_name( $from, $from_name );
}
$body = array(
'From' => $from,
'To' => is_array( $to ) ? implode( ',', $to ) : $to,
'Subject' => $subject,
'TextBody' => $message,
);
if ( ! empty( $recognized_headers['Cc'] ) ) {
$body['Cc'] = $recognized_headers['Cc'];
}
if ( ! empty( $recognized_headers['Bcc'] ) ) {
$body['Bcc'] = $recognized_headers['Bcc'];
}
if ( ! empty( $recognized_headers['Reply-To'] ) ) {
$body['ReplyTo'] = $recognized_headers['Reply-To'];
}
if ( isset( $settings['track_opens'] ) ) {
$track_opens = (int) $settings['track_opens'];
}
if ( isset( $settings['track_links'] ) ) {
$track_links = (int) $settings['track_links'];
} else {
$track_links = 0;
}
if ( isset( $recognized_headers['X-PM-Track-Opens'] ) ) {
if ( $recognized_headers['X-PM-Track-Opens'] ) {
$track_opens = 1;
} else {
$track_opens = 0;
}
}
if ( isset( $recognized_headers['X-PM-TrackLinks'] ) ) {
if ( 'none' !== $recognized_headers['X-PM-TrackLinks'] ) {
$body['TrackLinks'] = $recognized_headers['X-PM-TrackLinks'];
} else {
$body['TrackLinks'] = 'none';
}
}
$body['Tag'] = null;
if ( isset( $recognized_headers['X-PM-Tag'] ) ) {
$body['Tag'] = $recognized_headers['X-PM-Tag'];
}
// Support using a filter to set a tag on a message
if ( has_filter( 'postmark_tag' ) ) {
$body['Tag'] = apply_filters( 'postmark_tag', $body['Tag'] );
}
// Support using a filter to set metadata on a message
if ( has_filter( 'postmark_metadata' ) ) {
$body['Metadata'] = apply_filters( 'postmark_metadata', array() );
}
if ( 1 === (int) $settings['force_html'] || 'text/html' === $content_type || 1 === $track_opens ) {
$body['HtmlBody'] = $message;
// The user really, truly wants this sent as HTML, don't send it as text, too.
// For historical reasons, we can't "force html" and "track opens" set both html and text bodies,
// which is incorrect, but in order not to break existing behavior, we only strip out the textbody when
// the user has gone to the trouble of specifying content type of 'text/html' in their headers.
if ( 'text/html' === $content_type ) {
unset( $body['TextBody'] );
}
}
if ( 1 === $track_opens ) {
$body['TrackOpens'] = 'true';
}
if ( 1 === $track_links ) {
$body['TrackLinks'] = 'HtmlAndText';
}
if ( isset( $settings['stream_name'] ) && 'outbound' !== $settings['stream_name'] ) {
$body['MessageStream'] = $settings['stream_name'];
} else {
$body['MessageStream'] = 'outbound';
}
foreach ( $attachments as $attachment ) {
if ( is_readable( $attachment ) ) {
$body['Attachments'][] = array(
'Name' => basename( $attachment ),
'Content' => base64_encode( file_get_contents( $attachment ) ),
'ContentType' => postmark_determine_mime_content_type( $attachment ),
);
}
}
/*
==================================================
Send email
==================================================
*/
// Handle apostrophes in email address From names by escaping them for the Postmark API.
$from_regex = "/(\"From\": \"[a-zA-Z\\d]+)*[\\\\]{2,}'/";
$args = array(
'headers' => array(
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'X-Postmark-Server-Token' => $settings['api_key'],
),
'body' => preg_replace( $from_regex, "'", wp_json_encode( $body ), 1 ),
);
$response = wp_remote_post( 'https://api.postmarkapp.com/email', $args );
// Logs send attempt, if logging enabled.
if ( isset( $settings['enable_logs'] ) && 1 === $settings['enable_logs'] ) {
global $wpdb;
$table = $wpdb->prefix . 'postmark_log';
$to = $body['To'];
// Only store the To address, not the To name.
if ( ! empty( $body['To'] ) && ! empty( $to ) && false !== strpos( $body['To'], '<' ) && false !== strpos( $to, '>' ) ) {
$to = substr( $body['To'], strpos( $body['To'], '<' ), -1 );
}
// Only store the From address, not the From name.
if ( false !== strpos( $from, '<' ) && false !== strpos( $from, '>' ) ) {
$from = substr( $from, strpos( $from, '<' ), strpos( $from, '>' ) - 1 );
}
$log_entry = array(
'log_entry_date' => current_time( 'mysql' ),
'fromaddress' => sanitize_email( $from ),
'toaddress' => sanitize_email( $to ),
'subject' => sanitize_text_field( $subject ),
);
if ( is_array( $response ) ) {
$log_entry['response'] = sanitize_text_field( $response['body'] );
} elseif ( is_wp_error( $response ) ) {
$log_entry['response'] = $response->get_error_message();
}
$wpdb->insert( $table, $log_entry );
}
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
do_action( 'postmark_error', $response, $headers );
Postmark_Mail::$LAST_ERROR = $response;
return false;
}
do_action( 'postmark_response', $response, $headers );
// Support wp_mail_succeeded action
do_action('wp_mail_succeeded', array(
'to' => $body['To'],
'subject' => $body['Subject'],
'message' => $body['HtmlBody'] ?? $body['TextBody'],
'headers' => $recognized_headers,
'attachments' => $body['Attachments'] ?? null,
));
return true;
}