-
Notifications
You must be signed in to change notification settings - Fork 0
/
postmarkmail.php
310 lines (243 loc) · 10.8 KB
/
postmarkmail.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
<?php
/*
Plugin Name: Emailmanager Mail Replacement
Plugin URI: http://tmertz.com/projects/emailmanagermailreplacement/
Description: A simple plugin that replaces the default wp_mail function with Emailmanager (<a href="http://emailmanager.com/">http://emailmanager.com/</a>). This plugin is proudly sponsored by <a href="http://stupid-studio.com/">Stupid Studio</a>.
Version: 1.3.2
Author: Thomas Mertz
Author URI: http://tmertz.com/
*/
require_once("functions.php");
$pm_wp_mail_db_version = "1.3.2";
####################################################################
#
# INSTALLATION
#
####################################################################
function pm_wp_mail_install () {
global $wpdb;
global $pm_wp_mail_db_version;
$table_name = $wpdb->prefix . "pm_wp_mail_log";
if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
$sql = "CREATE TABLE " . $table_name . " (
`id` MEDIUMINT( 11 ) NOT NULL AUTO_INCREMENT ,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`httpcode` INT NOT NULL ,
`message` LONGTEXT ,
PRIMARY KEY ( `id` ) ,
INDEX ( `id` )
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option("pm_wp_mail_db_version", $pm_wp_mail_db_version);
}
$installed_ver = get_option( "pm_wp_mail_db_version" );
if( $installed_ver != $pm_wp_mail_db_version ) {
$table_name = $wpdb->prefix . "pm_wp_mail_log";
$sql = "CREATE TABLE " . $table_name . " (
`id` MEDIUMINT( 11 ) NOT NULL AUTO_INCREMENT ,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`httpcode` INT NOT NULL ,
`message` LONGTEXT ,
PRIMARY KEY ( `id` ) ,
INDEX ( `id` )
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
update_option( "pm_wp_mail_db_version", $pm_wp_mail_db_version );
}
}
register_activation_hook(__FILE__,'pm_wp_mail_install');
####################################################################
#
# UNINSTALLATION
#
####################################################################
function pm_wp_mail_uninstall() {
delete_option( 'pm_wp_mail_key' );
delete_option( 'pm_wp_mail_address' );
delete_option( 'pm_wp_mail_type' );
delete_option( 'pm_wp_mail_activate' );
$wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'pm_wp_mail_log;');
}
register_uninstall_hook(__FILE__,'pm_wp_mail_uninstall');
####################################################################
#
# SWITCHING OUT WP_MAIL() FOR OUR CUSTOM MAIL
#
####################################################################
if( !function_exists( 'wp_mail' ) ) {
function wp_mail($to, $subject, $body) {
global $wpdb;
if ( strstr($to, ",") ) {
$recipients = explode(",", $to);
foreach ($recipients as $recipient) {
$message = emailmanager_json_encode($recipient, $subject, $body);
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
'X-Emailmanager-Server-Token: ' . get_option( 'pm_wp_mail_key' )
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://trans.emailmanager.com/email');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/certificate/cacert.pem');
$return = curl_exec($ch);
$curlError = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
emailmanager_return_log($httpCode,$return);
}
} else {
$message = emailmanager_json_encode($to, $subject, $body);
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
'X-Emailmanager-Server-Token: ' . get_option( 'pm_wp_mail_key' )
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://trans.emailmanager.com/email');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/certificate/cacert.pem');
$return = curl_exec($ch);
$curlError = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
emailmanager_return_log($httpCode,$return);
}
if( $httpCode == 200 ) {
return true;
} else {
return false;
}
}
}
####################################################################
#
# ADMINISTRATION OUTPUT
#
####################################################################
add_action('admin_menu', 'pm_wp_mail_plugin_menu');
function pm_wp_mail_plugin_menu() {
add_menu_page('EmailManagerApp', 'EmailManagerApp', 'publish_posts', 'emailmanager-menu', 'pm_wp_mail_management','',150);
add_submenu_page('emailmanager-menu', 'Logs', 'Logs', 'publish_posts', 'emailmanager-logs', 'pm_wp_mail_logs');
}
function pm_wp_mail_management() {
if (!current_user_can('manage_options')) {
wp_die( __('You do not have permission to access this page.') );
}
global $wpdb;
global $current_user;
echo "<div class=\"wrap\">\n";
echo "<div id=\"icon-tools\" class=\"icon32\"></div><h2>EmailManagerApp</h2>\n";
if( $_GET["action"] == "test") {
$to = $current_user->user_email;
wp_mail($to, 'EmailManagerApp Mail Replacement Test', 'This is a test. If you are receiving this email Emailmanager Mail Replacement is working as intended.');
echo "<div id=\"setting-error-settings_updated\" class=\"updated settings-error\"><p><strong>Test email dispatched.</strong></p></div>";
}
if( $_POST["pm-wp-mail-update-options"] == "yes" ) {
update_option( 'pm_wp_mail_activate', $_POST["pm_wp_mail_activate"] );
update_option( 'pm_wp_mail_key', $_POST["pm_wp_mail_key"] );
update_option( 'pm_wp_mail_address', $_POST["pm_wp_mail_address"] );
update_option( 'pm_wp_mail_type', $_POST["pm_wp_mail_type"] );
echo "<div id=\"setting-error-settings_updated\" class=\"updated settings-error\"><p><strong>Settings saved.</strong></p></div>";
}
echo "<form method=\"post\" action=\"" . $_SERVER["REQUEST_URI"] . "\">\n";
echo "<table class=\"form-table\">\n";
echo "<tbody>\n";
echo "<tr valign=\"top\">\n";
echo "<th scope=\"row\"><label for=\"pm_wp_mail_key\">API key</label></th>\n";
echo "<td>\n";
echo "<input type=\"text\" name=\"pm_wp_mail_key\" value=\"" . get_option( 'pm_wp_mail_key' ) . "\" size=\"60\" />\n";
echo "</td>\n";
echo "</tr>\n";
echo "<tr valign=\"top\">\n";
echo "<th scope=\"row\"><label for=\"pm_wp_mail_address\">EmailManager email address</label></th>\n";
echo "<td>\n";
echo "<input type=\"text\" name=\"pm_wp_mail_address\" value=\"" . get_option( 'pm_wp_mail_address' ) . "\" size=\"60\" /><br /><span class=\"description\">Usually something like '[email protected]'.<br />Should match the sender signature you registered with EmailManagerApp.</span>\n";
echo "</td>\n";
echo "</tr>\n";
echo "<tr valign=\"top\">\n";
echo "<th scope=\"row\"><label for=\"pm_wp_mail_type\">Email Type</label></th>\n";
echo "<td>\n";
echo "<select name=\"pm_wp_mail_type\" id=\"pm_wp_mail_type\">\n";
if(get_option( 'pm_wp_mail_type' )==1) {
echo "<option value=\"1\" selected=\"selected\">HTML</option>\n";
echo "<option value=\"0\">Pure text</option>\n";
} else {
echo "<option value=\"1\">HTML</option>\n";
echo "<option value=\"0\" selected=\"selected\">Pure text</option>\n";
}
echo "</select>\n";
echo "</td>\n";
echo "</tr>\n";
echo "<tr valign=\"top\">\n";
echo "<th scope=\"row\"></th>\n";
echo "<td>\n";
echo "<a href=\"" . $_SERVER["REQUEST_URI"] . "&action=test\" class=\"button-secondary\">Send Test Message</a>\n";
echo "</td>\n";
echo "</tr>\n";
echo "</tbody>\n";
echo "</table>\n";
echo "<p class=\"submit\"><input type=\"submit\" name=\"Submit\" class=\"button-primary\" value=\"Save Changes\"></p>\n";
echo "<input type=\"hidden\" name=\"pm-wp-mail-update-options\" value=\"yes\" />\n";
echo "</form>\n";
echo "<hr style=\"border: 0; color: #ddd;background-color: #ddd;height: 1px;\" />";
echo "<p style=\"text-align: right;\">This plugin is proudly sponsored by <a href=\"http://www.stupid-studio.com/\">Stupid Studio</a>.</p>";
echo "<p style=\"text-align: right;\"><a href=\"http://www.stupid-studio.com/\"><img src=\"" . PluginUrl() . "stupid_logo.png\" style=\"border:0;\" alt=\"Stupid Studio\" /></a></p>";
echo "</div>\n";
}
function pm_wp_mail_logs() {
if (!current_user_can('manage_options')) {
wp_die( __('You do not have permission to access this page.') );
}
global $wpdb;
echo "<div class=\"wrap\">\n";
echo "<div id=\"icon-tools\" class=\"icon32\"></div><h2>EmailManagerApp Logs</h2>\n";
if( $_GET["action"] == "purge") {
$table_name = $wpdb->prefix . "pm_wp_mail_log";
$wpdb->query("DELETE FROM {$table_name} WHERE id <>'0'");
echo "<div id=\"setting-error-settings_updated\" class=\"updated settings-error\"><p><strong>Your logs have been purged.</strong></p></div>";
}
echo "<p><form method=\"post\" action=\"/wp-admin/admin.php?page=emailmanager-logs&action=purge\"><input type=\"submit\" class=\"button-primary\" value=\"Purge Logs\" name=\"submit\"></form></p>";
echo '<table class="widefat fixed" cellspacing="0">';
echo '<thead>';
echo '<tr class="thead">';
echo '<th scope="col" class="manage-column" style="">Timestamp</th>';
echo '<th scope="col" class="manage-column" style="">HTTP Code</th>';
echo '<th scope="col" class="manage-column" style="">Error message</th>';
echo '</tr>';
echo '</thead>';
echo '<tfoot>';
echo '<tr class="thead">';
echo '<th scope="col" class="manage-column" style="">Timestamp</th>';
echo '<th scope="col" class="manage-column" style="">HTTP Code</th>';
echo '<th scope="col" class="manage-column" style="">Error message</th>';
echo '</tr>';
echo '</tfoot>';
echo '<tbody>';
$table_name = $wpdb->prefix . "pm_wp_mail_log";
$logs = $wpdb->get_results("SELECT timestamp,httpcode,message FROM {$table_name} ORDER BY timestamp DESC LIMIT 50");
foreach($logs as $log) {
echo ' <tr>';
echo ' <td>' . date('d-m-Y H:i',strtotime($log->timestamp)) . '</td>';
echo ' <td>' . $log->httpcode . '</td>';
echo ' <td>' . $log->message . '</td>';
echo ' </tr>';
}
echo '</tbody>';
echo '</table>';
echo "<hr style=\"border: 0; color: #ddd;background-color: #ddd;height: 1px;\" />";
echo "<p style=\"text-align: right;\">This plugin is proudly sponsored by <a href=\"http://www.stupid-studio.com/\">Stupid Studio</a>.</p>";
echo "<p style=\"text-align: right;\"><a href=\"http://www.stupid-studio.com/\"><img src=\"" . PluginUrl() . "stupid_logo.png\" style=\"border:0;\" alt=\"Stupid Studio\" /></a></p>";
echo "</div>";
}
?>