forked from ActiveCampaign/postmark-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-mail.php
211 lines (178 loc) · 7.04 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
<?php
require_once(dirname( __FILE__ ).'/postmark.php');
function postmark_determine_mime_content_type( $filename ){
if (function_exists( 'mime_content_type' )) {
return mime_content_type($filename);
}
else if( 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';
}
}
function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
// Compact the input, apply the filters, and extract them back out
extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );
$settings = json_decode( get_option( 'postmark_settings' ), true );
if( $settings['test'] ) {
$settings['api_key'] = 'POSTMARK_API_TEST';
}
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()
);
$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'];
if ( isset( $recognized_headers['From'] ) ) {
$from = $recognized_headers['From'];
}
$body = array(
'To' => is_array( $to ) ? implode( ',', $to ) : $to,
'From' => $from,
'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'];
}
$track_opens = (int) $settings['track_opens'];
if ( isset($recognized_headers['X-PM-Track-Opens'])){
if ( $recognized_headers['X-PM-Track-Opens'] ) {
$track_opens = 1;
}else {
$track_opens = 0;
}
}
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';
}
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
==================================================
*/
$args = array(
'headers' => array(
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'X-Postmark-Server-Token' => $settings['api_key']
),
'body' => json_encode( $body )
);
$response = wp_remote_post( 'https://api.postmarkapp.com/email', $args );
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);
return true;
}