-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandrill-mailer.php
389 lines (374 loc) · 12 KB
/
mandrill-mailer.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
/**
* Plugin Name: Mandrill Mailer
* Description: Send transactional email using Mandrill easily and without hassle
* Author: biohzrdmx
* Version: 1.0
* Plugin URI: http://github.com/biohzrdmx/wp-mandrill-mailer
* Author URI: http://github.com/biohzrdmx/
*/
if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if( ! class_exists('MandrillMailer') ) {
/**
* MandrillMessage class
*/
class MandrillMessage {
public $subject;
public $from;
public $to;
public $contents;
public $template;
public $attachments;
public $images;
public $replacements;
/**
* Default constructor
*/
function __construct() {
$this->subject = '';
$this->from = '';
$this->to = '';
$this->contents = '';
$this->template = '';
$this->attachments = array();
$this->images = array();
}
/**
* MandrillMessage factory class for chaining
* @return object The newly-created MandrillMessage instance
*/
static function newInstance() {
$new = new self();
return $new;
}
/**
* Set message subject
* @param string $subject Message subject
*/
public function setSubject($subject) {
$this->subject = $subject;
return $this;
}
/**
* Set message sender
* @param array $from Array of (name => email) pairs or email addresses
*/
public function setFrom($from) {
$this->from = $from;
return $this;
}
/**
* Set message destination
* @param array $from Array of (name => email) pairs or email addresses
*/
public function setTo($to) {
$this->to = $to;
return $this;
}
/**
* Set email contents
* @param string $contents Message contents (html or plain-text)
*/
public function setContents($contents) {
$this->contents = $contents;
return $this;
}
/**
* Set template path, expects a file-system path
* @param string $template Path to the template file
*/
public function setTemplate($template) {
$this->template = $template;
return $this;
}
/**
* Set message attachments
* @param array $attachments Array of (name => path) pairs
*/
public function setAttachments($attachments) {
$this->attachments = $attachments;
return $this;
}
/**
* Set message images
* @param array $images Array of (name => path) pairs
*/
public function setImages($images) {
$this->images = $images;
return $this;
}
/**
* Set message replacements for your template using shortcodes
* @param array $replacements Array of (shortcode => value) pairs
*/
public function setReplacements($replacements) {
$this->replacements = $replacements;
return $this;
}
}
/**
* MandrillMailer class
*/
class MandrillMailer {
public static function actionAdminMenu() {
add_menu_page('Mandrill Mailer', 'Mandrill Mailer', 'manage_options', 'mandrill_mailer', 'MandrillMailer::callbackAdminPage', 'dashicons-email-alt');
}
public static function actionAdminInit() {
register_setting( 'mandrill_mailer', 'mandrill_mailer_options' );
add_settings_section( 'mandrill_mailer_settings', __( 'General settings', 'mandrill_mailer' ), 'MandrillMailer::callbackSettings', 'mandrill_mailer' );
add_settings_field( 'mandrill_mailer_field_key', __('Secret key', 'mandrill_mailer'), 'MandrillMailer::fieldPassword', 'mandrill_mailer', 'mandrill_mailer_settings', [ 'label_for' => 'mandrill_mailer_field_key', 'class' => 'mandrill_mailer_row' ] );
add_settings_field( 'mandrill_mailer_field_pool', __('Pool', 'mandrill_mailer'), 'MandrillMailer::fieldText', 'mandrill_mailer', 'mandrill_mailer_settings', [ 'label_for' => 'mandrill_mailer_field_pool', 'class' => 'mandrill_mailer_row', 'default' => 'Main Pool' ] );
}
public static function adminSettingsLink($links, $file) {
$links = (array) $links;
if ( $file === 'wp-mandrill-mailer/mandrill-mailer.php' && current_user_can( 'manage_options' ) ) {
$url = admin_url('admin.php?page=mandrill_mailer');
$link = sprintf( '<a href="%s">%s</a>', $url, __( 'Settings', 'mandrill_mailer' ) );
array_unshift($links, $link);
}
return $links;
}
public static function fieldPassword($args) {
$options = get_option( 'mandrill_mailer_options' );
?>
<input type="password" id="<?php echo esc_attr( $args['label_for'] ); ?>" name="mandrill_mailer_options[<?php echo esc_attr( $args['label_for'] ); ?>]" value="<?php echo esc_html( isset( $options[ $args['label_for'] ] ) ? $options[ $args['label_for'] ] : '' ); ?>" autocomplete="new-password">
<?php
}
public static function fieldText($args) {
$options = get_option( 'mandrill_mailer_options' );
?>
<input type="text" id="<?php echo esc_attr( $args['label_for'] ); ?>" name="mandrill_mailer_options[<?php echo esc_attr( $args['label_for'] ); ?>]" value="<?php echo esc_html( isset( $options[ $args['label_for'] ] ) ? $options[ $args['label_for'] ] : $args['default'] ); ?>">
<?php
}
public static function callbackAdminPage() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if ( isset( $_GET['settings-updated'] ) ) {
add_settings_error( 'mandrill_mailer_messages', 'mandrill_mailer_message', __( 'Settings Saved', 'mandrill_mailer' ), 'updated' );
}
settings_errors( 'mandrill_mailer_messages' );
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<form action="options.php" method="post">
<?php
settings_fields( 'mandrill_mailer' );
do_settings_sections( 'mandrill_mailer' );
submit_button( __('Save Settings', 'mandrill_mailer') );
?>
</form>
</div>
<?php
}
public static function callbackSettings() {
?>
<p><?php esc_html_e('Configure here your Mandrill parameters.'); ?></p>
<?php
}
/**
* Get an item from an array/object, or a default value if it's not set
* @param mixed $var Array or object
* @param mixed $key Key or index, depending on the array/object
* @param mixed $default A default value to return if the item it's not in the array/object
* @return mixed The requested item (if present) or the default value
*/
protected static function getItem($var, $key, $default = '') {
return is_object($var) ?
( isset( $var->$key ) ? $var->$key : $default ) :
( isset( $var[$key] ) ? $var[$key] : $default );
}
/**
* Send a message using the Mandrill transactional email service
* @param object $message The MailerMessage instance
* @return boolean True if the message was sent, False otherwise
*/
static function send($message) {
$ret = false;
# Build arrays
$to = array();
$global_vars = self::getItem($options, 'vars', array());
$vars = array();
if (! is_array($message->to) ) {
$message->to = array($message->to);
}
if ( $message->to ) {
foreach ($message->to as $key => $value) {
$name = '';
$email = '';
$usr_vars = array();
$extra = array();
# Check destination type
if ( is_object($value) ) {
# Item is an user object
$email = $value->email;
$name = "{$value->first_name} {$value->last_name}";
# Include additional merge vars
if ( isset($value->vars) ) {
foreach ($value->vars as $code => $content) {
$extra[] = array(
'name' => $code,
'content' => $content
);
}
}
} else {
# Item is an 'email' => 'name' array
$email = $key;
$name = $value;
}
# Add destination
$to[] = array(
'email' => $email,
'name' => $name,
'type' => 'to'
);
# Build merge vars
$usr_vars = array(
array(
'name' => 'email',
'content' => $email
),
array(
'name' => 'name',
'content' => $name
)
);
$usr_vars = array_merge($usr_vars, $extra);
# Add merge vars
$vars[] = array(
'rcpt' => $email,
'vars' => $usr_vars
);
}
}
$subject = $message->subject;
$from_email = key($message->from);
$from_name = $message->from[$from_email];
$headers = self::getItem($options, 'headers', array());
$contents = $message->contents;
# Load template
$template = $message->template;
if ($template) {
$html = file_get_contents( $template );
$html = str_replace('%email-site%', home_url('/'), $html);
$html = str_replace('%email-body%', $contents, $html);
if($message->replacements) {
foreach ($message->replacements as $shortcode => $value) {
$html = str_replace($shortcode, $value, $html);
}
}
} else {
$html = $contents;
}
# Attachments
$attachments = array();
foreach ($message->attachments as $name => $attachment) {
# File must exist
if (! file_exists($attachment) ) continue;
# Determine (guess by extension) mime type
$ext = strtolower( substr( $attachment, strrpos($attachment, '.') + 1 ) );
switch ($ext) {
case 'gif':
case 'png':
$mime = "image/{$ext}";
case 'jpg':
case 'jpeg':
$mime = 'image/jpeg';
break;
case 'mpeg':
case 'mp4':
case 'ogg':
case 'webm':
$mime = "video/{$ext}";
break;
case 'pdf':
case 'zip':
$mime = "application/{$ext}";
break;
case 'csv':
case 'xml':
$mime = "text/{$ext}";
break;
default:
$mime = 'application/octet-stream';
}
# Add to attachments array
$attachments[] = array(
'type' => $mime,
'name' => $name,
'content' => base64_encode( file_get_contents($attachment) )
);
}
# Images
$images = array();
foreach ( $message->images as $name => $image) {
# File must exist
if (! file_exists($image) ) continue;
# Determine (guess by extension) mime type
$ext = strtolower( substr( $image, strrpos($image, '.') + 1 ) );
switch ($ext) {
case 'gif':
case 'png':
$mime = "image/{$ext}";
case 'jpg':
case 'jpeg':
$mime = 'image/jpeg';
break;
}
# Add to images array
$images[] = array(
'type' => $mime,
'name' => $name,
'content' => base64_encode( file_get_contents($image) )
);
}
# Include library
$dir = plugin_dir_path( __FILE__ );
include_once "{$dir}/lib/Mandrill.php";
#
$options = get_option( 'mandrill_mailer_options' );
#
try {
$mandrill = new Mandrill( self::getItem($options, 'mandrill_mailer_field_key', '') );
$message = array(
'html' => $html,
'text' => strip_tags($contents),
'subject' => $subject,
'from_email' => $from_email,
'from_name' => $from_name,
'to' => $to,
'headers' => $headers,
'attachments' => $attachments,
'important' => true,
'track_opens' => true,
'track_clicks' => true,
'merge' => true,
'preserve_recipients' => false,
'global_merge_vars' => $global_vars,
'merge_vars' => $vars,
'inline_css' => true,
'images' => $images
);
# Use SSL always
curl_setopt($mandrill->ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($mandrill->ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($mandrill->ch, CURLOPT_CAINFO, "{$dir}/cacert.pem");
#
$async = self::getItem($options, 'mandrill_mailer_field_async', false);
$ip_pool = self::getItem($options, 'mandrill_mailer_field_pool', 'Main Pool');
$send_at = '';
$result = $mandrill->messages->send($message, $async, $ip_pool, $send_at);
$ret = true;
} catch(Mandrill_Error $e) {
echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
throw $e;
}
return $ret;
}
}
add_action( 'admin_init', 'MandrillMailer::actionAdminInit' );
add_action( 'admin_menu', 'MandrillMailer::actionAdminMenu' );
add_filter( 'plugin_action_links', 'MandrillMailer::adminSettingsLink', 10, 5 );
}
?>