/
wp-ses.php
762 lines (702 loc) · 28.3 KB
/
wp-ses.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
<?php
/*
Plugin Name: WP SES
Version: 0.7.21
Plugin URI: http://wp-ses.com
Description: Uses Amazon Simple Email Service instead of local mail for all outgoing WP emails.
Author: Sylvain Deaure
Author URI: http://www.blog-expert.fr
* Text Domain: wpses
* Domain Path: /
*/
define('WPSES_VERSION', 0.721);
// TODO
// stats cache (beware of directory)
// logs of mails sent (inc details ?)
// traiter les erreurs (stocker contenu) pour les re-tenter plus tard ?
// Mailqueue
// limits (check once per hour (or faster) and stop if near limit)
// blacklist, mail delivery handling
// dashboard integration (main stats without extra page)
// Add error display for test messages
// add attachments (contact form 7) : see https://github.com/daniel-zahariev/php-aws-ses
// retrieve the security credentials from the instance metadata service for EC2 : cf Christian at tellnes
if (defined('WP_SES_ACCESS_KEY') and defined('WP_SES_SECRET_KEY')) {
define('WP_SES_RESTRICTED', true);
} else {
define('WP_SES_RESTRICTED', false);
}
if (is_admin()) {
// TODO : Ask before activate
// include_once(WP_PLUGIN_DIR.'/wp-ses/sdrssw.php');
add_action('init', 'wpses_init');
add_action('admin_menu', 'wpses_admin_menu');
register_activation_hook(__FILE__, 'wpses_install');
register_deactivation_hook(__FILE__, 'wpses_uninstall');
}
//require_once (WP_PLUGIN_DIR . '/wp-ses/ses.class.0.8.6.php');
// May be in wpmu folder, thanks to @positonic
require_once plugin_dir_path( __FILE__ ) . 'ses.class.0.8.6.php';
function wpses_init() {
load_plugin_textdomain('wpses', false, basename(dirname(__FILE__)));
wpses_admin_warnings();
}
add_filter('wp_mail_from', 'wpses_from', 1);
add_filter('wp_mail_from_name', 'wpses_from_name', 1);
function wpses_install() {
global $wpdb, $wpses_options;
if (!get_option('wpses_options')) {
add_option('wpses_options', array(
'from_email' => '',
'return_path' => '',
'from_name' => 'WordPress',
'access_key' => '',
'secret_key' => '',
'endpoint' => 'email.us-east-1.amazonaws.com',
'credentials_ok' => 0,
'sender_ok' => 0,
'last_ses_check' => 0, // timestamp of last quota check
'force' => 0,
'log' => 0,
'active' => 1, // reset to 0 if not pluggable or config change.
'version' => '0' // Version of the db
// TODO: garder liste des ids des demandes associ�es � chaque email.
// afficher : email, id demande , valid� ?
));
wpses_getoptions();
//$wpses_options = get_option('wpses_options');
@ mkdir(WP_PLUGIN_DIR . '/wp-ses/log/');
}
}
function wpses_options() {
global $wpdb, $wpses_options;
global $current_user;
//get_currentuserinfo();
wp_get_current_user(); // Thanks @jmichaelward
if (!in_array('administrator', $current_user->roles)) {
//die('Not admin');
}
$authorized = '';
if (($wpses_options['access_key'] != '') and ( $wpses_options['secret_key'] != '')) {
$authorized = wpses_getverified();
}
$senders = (array) get_option('wpses_senders');
// ajouter dans senders les verified absents
$updated = false;
if ('' != $authorized) {
if (!is_array($authorized)) {
$authorized = array($authorized);
}
foreach ($authorized as $email) {
// if (false !==strpos($email, '@')) {
// identity if full email
if (!array_key_exists($email, $senders)) {
$senders[$email] = array(
-1, // Got an Id, but not request id
TRUE
);
$updated = true;
} else {
if (!$senders[$email][1]) {
// activate the new emails
$senders[$email][1] = true;
$updated = true;
}
}
/* } else {
// identity is domain only
// Must check with all senders
// No request id here, just tag as valid
foreach($senders as $email=>$detail) {
}
} */
}
// remove old senders
foreach ($senders as $email => $info) {
if ($info[1] and ! in_array($email, $authorized)) {
$senders[$email][1] = false;
// echo 'remove '.$email.' ';
$updated = true;
}
}
}
if ($updated) {
update_option('wpses_senders', $senders);
}
$wpses_options['sender_ok'] = 0;
if (($wpses_options['from_email'] != '')) {
if ($senders[$wpses_options['from_email']][1] === TRUE) { //
// email exp enregistré non vide et listé, on peut donc supposer que credentials ok et exp ok.
if ($wpses_options['credentials_ok'] == 0) {
$wpses_options['credentials_ok'] = 1;
wpses_log('Credentials ok');
update_option('wpses_options', $wpses_options);
}
if ($wpses_options['sender_ok'] == 0) {
$wpses_options['sender_ok'] = 1;
wpses_log('Sender Ok');
update_option('wpses_options', $wpses_options);
}
} else {
//if ($senders[$wpses_options['from_email']][1] !== TRUE) { //
//$wpses_options['sender_ok'] = 0;
//wpses_log('Sender not OK');
//update_option('wpses_options', $wpses_options);
}
//if (!isset($senders[$wpses_options['from_email']])) {
if (0 == $wpses_options['sender_ok']) {
// email is not known, but domain could be listed
list($user, $domain) = explode('@', $wpses_options['from_email']);
if ($senders[$domain][1] === TRUE) {
// domain is validated
//$senders[$wpses_options['from_email']] = array(-1, true);
$wpses_options['sender_ok'] = 1;
wpses_log('Sender domain ok');
$wpses_options['credentials_ok'] = 1;
update_option('wpses_options', $wpses_options);
} else {
//$senders[$wpses_options['from_email']] = array(-1, false);
}
}
}
if ((($wpses_options['sender_ok'] != 1) and ( $wpses_options['force'] != 1)) or ( $wpses_options['credentials_ok'] != 1)) {
$wpses_options['active'] = 0;
wpses_log('Deactivate sender_ok=' . $wpses_options['sender_ok'] . ' Force=' . $wpses_options['force'] . ' credentials_ok=' . $wpses_options['credentials_ok']);
update_option('wpses_options', $wpses_options);
}
if (!empty($_POST['activate'])) {
$wpses_options['force'] = 0;
if (($wpses_options['sender_ok'] == 1) and ( $wpses_options['credentials_ok'] == 1)) {
$wpses_options['active'] = 1;
wpses_log('Normal activation');
update_option('wpses_options', $wpses_options);
echo '<div id="message" class="updated fade">
<p>' . __('Plugin is activated and functionnal', 'wpses') . '</p>
</div>' . "\n";
}
if (isset($_POST['force']) and 1 == $_POST['force']) {
// bad hack to force plugin activation with IAM credentials
$wpses_options['sender_ok'] = 1;
$wpses_options['credentials_ok'] = 1;
$wpses_options['active'] = 1;
$wpses_options['force'] = 1;
wpses_log('Forced activation');
update_option('wpses_options', $wpses_options);
}
}
if (!empty($_POST['deactivate'])) {
$wpses_options['active'] = 0;
wpses_log('Manual deactivation');
update_option('wpses_options', $wpses_options);
echo '<div id="message" class="updated fade">
<p>' . __('Plugin de-activated', 'wpses') . '</p>
</div>' . "\n";
}
if (!empty($_POST['activatelogs'])) {
@ mkdir(WP_PLUGIN_DIR . '/wp-ses/log/');
@ touch(WP_PLUGIN_DIR . '/wp-ses/log/wpses.log');
if (!file_exists(WP_PLUGIN_DIR . '/wp-ses/log/wpses.log')) {
echo '<div id="message" class="updated">
<p>' . __('Unable to create dir ', 'wpses') . WP_PLUGIN_DIR . '/wp-ses/log/' . __(' Please create it and give WP proper rights ', 'wpses') . '</p>
</div>' . "\n";
} else {
@ unlink(WP_PLUGIN_DIR . '/wp-ses/log/wpses.log');
echo '<div id="message" class="updated fade">
<p>' . __('Logs activated', 'wpses') . '</p>
</div>' . "\n";
$wpses_options['log'] = 1;
update_option('wpses_options', $wpses_options);
wpses_log('Start Logging');
}
}
if (!empty($_POST['deactivatelogs'])) {
$wpses_options['log'] = 0;
update_option('wpses_options', $wpses_options);
@ unlink(WP_PLUGIN_DIR . '/wp-ses/log/wpses.log');
echo '<div id="message" class="updated fade">
<p>' . __('Logs deactivated and cleared', 'wpses') . '</p>
</div>' . "\n";
}
if (!empty($_POST['viewlogs'])) {
if (file_exists(WP_PLUGIN_DIR . '/wp-ses/log/wpses.log')) {
echo nl2br(file_get_contents(WP_PLUGIN_DIR . '/wp-ses/log/wpses.log'));
die();
} else {
echo '<div id="message" class="updated fade">
<p>' . __('No log file', 'wpses') . '</p>
</div>' . "\n";
}
}
if (!empty($_POST['save'])) {
//check_admin_referer();
//$wpses_options['active'] = trim($_POST['active']);
if ($wpses_options['from_email'] != trim($_POST['from_email'])) {
wpses_log('From Email changed, reset state');
$wpses_options['sender_ok'] = 0;
$wpses_options['active'] = 0;
}
if (!defined('WP_SES_FROM')) {
$wpses_options['from_email'] = trim($_POST['from_email']);
}
if (!defined('WP_SES_RETURNPATH')) {
$wpses_options['return_path'] = trim($_POST['return_path']);
}
if ($wpses_options['return_path'] == '') {
$wpses_options['return_path'] = $wpses_options['from_email'];
}
if (!defined('WP_SES_REPLYTO')) {
$wpses_options['reply_to'] = trim($_POST['reply_to']);
if ($wpses_options['reply_to'] == '') {
$wpses_options['reply_to'] = $wpses_options['from_email'];
}
}
$wpses_options['from_name'] = trim($_POST['from_name']); //
$wpses_options['endpoint'] = trim($_POST['endpoint']); //
// TODO si mail diff�re, relancer proc�dure check => resetter sender_ok si besoin
if (($wpses_options['access_key'] != trim($_POST['access_key'])) or ( $wpses_options['secret_key'] != trim($_POST['secret_key']))) {
wpses_log('API Keys changed, reset state and deactivate');
$wpses_options['credentials_ok'] = 0;
$wpses_options['sender_ok'] = 0;
$wpses_options['active'] = 0;
}
if (!WP_SES_RESTRICTED) {
$wpses_options['access_key'] = trim($_POST['access_key']); //
$wpses_options['secret_key'] = trim($_POST['secret_key']); //
}
// TODO si credentials different, resetter credentials_ok
update_option('wpses_options', $wpses_options);
echo '<div id="message" class="updated fade"><p>' . __('Settings updated', 'wpses') . '</p></div>' . "\n";
}
wpses_getoptions();
//$wpses_options = get_option('wpses_options');
// validation cle amazon
if (!WP_SES_RESTRICTED) { // server side check.
// validation email envoi
if (!empty($_POST['addemail'])) {
wpses_verify_sender_step1($wpses_options['from_email']);
}
// remove verified email
if (!empty($_POST['removeemail'])) {
wpses_remove_sender($_POST['email']);
}
// envoi mail test
if (!empty($_POST['testemail'])) {
wpses_log('Test email request');
wpses_test_email($wpses_options['from_email']);
}
// envoi mail test prod
if (!empty($_POST['prodemail'])) {
wpses_log('Prod email request');
wpses_prod_email($_POST['prod_email_to'], $_POST['prod_email_subject'], $_POST['prod_email_content']);
}
}
include ('admin.tmpl.php');
}
// TODO
function wpses_uninstall() {
// delete_option('wpses_options');
// Do not delete, else we loose the version number
// TODO: add an uninstall link ? Not a big deal since we added very little overhead
}
function wpses_log($message) {
global $wpses_options;
$message = time() . "\t" . $message . "\r\n";
if ($wpses_options['log']) {
error_log($message, 3, WP_PLUGIN_DIR . '/wp-ses/log/wpses.log');
}
}
function wpses_admin_warnings() {
global $wpses_options;
if (!function_exists('curl_version')) {
function wpses_curl_warning() {
global $wpses_options;
echo "<div id='wpses-curl-warning' class='updated fade'><p><strong>" . __("WP SES - CURL extension not available. SES Won't work without Curl. Ask your host.", 'wpses') . "</strong></p></div>";
}
add_action('admin_notices', 'wpses_curl_warning');
return;
}
$active = $wpses_options['active'];
if ($active <= 0) {
function wpses_warning() {
global $wpses_options;
echo "<div id='wpses-warning' class='updated fade'><p><strong>" . __("WP SES - Simple Email Service is not fully activated. Please check it's config: ", 'wpses') .
'<a href="options-general.php?page=wp-ses/wp-ses.php">' . __("Settings → WP SES", 'wpses') . '</a>.' . "</strong></p></div>";
}
add_action('admin_notices', 'wpses_warning');
return;
}
}
function wpses_admin_menu() {
add_options_page('wpses', __('WP SES', 'wpses'), 'manage_options', __FILE__, 'wpses_options');
// Quota and Stats
if (!defined('WP_SES_HIDE_STATS') or ( false == WP_SES_HIDE_STATS)) {
add_submenu_page('index.php', 'SES Stats', 'SES Stats', 'manage_options', 'wp-ses/ses-stats.php');
}
}
function wpses_from($mail_from_email) {
global $wpses_options;
$from_email = $wpses_options['from_email'];
if (empty($from_email)) {
return $mail_from_email;
} else {
return $from_email;
}
}
function wpses_from_name($mail_from_name) {
global $wpses_options;
$from_name = $wpses_options['from_name'];
if (empty($from_name)) {
return $mail_from_name;
} else {
return $from_name;
}
}
function wpses_message_step1done() {
global $WPSESMSG;
echo "<div id='wpses-warning' class='updated fade'><p><strong>" . __("A confirmation request has been sent. You will receive at the stated email a confirmation request from amazon SES. You MUST click on the provided link in order to confirm your sender Email.<br />SES Answer - ", 'wpses') . $WPSESMSG . "</strong></p></div>";
}
/**
*
* @global type $wpses_options
* @global type $SES
* @return Fetches verifiedIdentities
*/
function wpses_getverified() {
global $wpses_options;
global $SES;
wpses_check_SES();
@ $result = $SES->listIdentities();
if (is_array($result)) {
wpses_log(count($result) . ' Verified Identities.');
return $result['Identities'];
} else {
wpses_log('No verified Identity.');
return NULL;
}
}
function wpses_check_SES() {
global $wpses_options;
global $SES;
if (!isset($SES)) {
$SES = new SimpleEmailService($wpses_options['access_key'], $wpses_options['secret_key'], $wpses_options['endpoint']);
}
}
/*
function wpses_error_handler($level, $message, $file, $line, $context) {
global $WPSESMSG;
$WPSESMSG = __('SES Error: ', 'wpses') . $message;
wpses_log("SES Error\t" . $message['Error']['Code'] . "\t" . $message['Error']['Message'] . "\t" . $message['Error']['RequestId']);
return (true); //And prevent the PHP error handler from continuing
}
*/
// start email verification (mail from amazon to sender, requesting validation)
function wpses_verify_sender_step1($mail) {
global $wpses_options;
global $SES, $WPSESMSG;
wpses_check_SES();
$WPSESMSG = '';
// dans la chaine : Sender - InvalidClientTokenId si auth pas correct
// Sender - OptInRequired
// The AWS Access Key Id needs a subscription for the service: si cl� aws ok, mais pas d'abo au service amazon lors de la verif d'un mail
// inscription depuis aws , verif phone.
//Use our custom handler
set_error_handler('wpses_error_handler');
try {
$rid = $SES->verifyEmailIdentity($mail);
$senders = get_option('wpses_senders');
if ($rid <> '') {
$senders[$mail] = array(
$rid['RequestId'],
false
);
$wpses_options['credentials_ok'] = 1;
wpses_log('credentials_ok, sender verified');
update_option('wpses_options', $wpses_options);
update_option('wpses_senders', $senders);
}
//echo("rid ");
//print_r($rid);
} catch (Exception $e) {
$WPSESMSG = __('Got exception: ', 'wpses') . $e->getMessage() . "\n";
wpses_log('SES Exception ' . $e->getMessage());
}
restore_error_handler();
$WPSESMSG .= ' id ' . var_export($rid, true);
wpses_message_step1done();
// add_action('admin_notices', 'wpses_message_step1done'); // no : too late for this !
}
function wpses_remove_sender($mail) {
global $wpses_options;
global $SES, $WPSESMSG;
wpses_check_SES();
$WPSESMSG = '';
$rid = $SES->deleteVerifiedEmailAddress($mail);
$WPSESMSG .= ' id ' . var_export($rid, true);
echo "<div id='wpses-warning' class='updated fade'><p><strong>" . $mail . '<br />' . __("This email address has been removed from verified senders.", 'wpses') . "</strong></p></div>";
}
/**
* Returns true is sender email is defined and allowed by ses
* @global type $wpses_options
*/
function wpses_sender_confirmed() {
global $wpses_options;
if ($wpses_options['from_email'] != '') {
$senders = (array) get_option('wpses_senders');
if ($senders[$wpses_options['from_email']][1]) {
// email matches
return true;
}
// email is not known, but domain could be listed
list($user, $domain) = explode('@', $wpses_options['from_email']);
if ($senders[$domain][1] === TRUE) {
//echo($domain." domain validated ");
return true;
}
}
return false;
}
function wpses_message_testdone() {
global $WPSESMSG;
echo "<div id='wpses-warning' class='updated fade'><p><strong>" . __("Test message has been sent to your sender Email address.<br />SES Answer - ", 'wpses') . $WPSESMSG . "</strong></p></div>";
}
function wpses_test_email($mail) {
global $wpses_options;
global $SES, $WPSESMSG;
wpses_check_SES();
$WPSESMSG = '';
$rid = wpses_mail($wpses_options['from_email'], __('WP SES - Test Message', 'wpses'), __("This is WP SES Test message. It has been sent via Amazon SES Service.\nAll looks fine !\n\n", 'wpses') . __('WP SES is a plugin by', 'wpses') . ' http://www.blog-expert.fr/');
$WPSESMSG .= ' id ' . var_export($rid, true);
wpses_message_testdone();
}
function wpses_prod_email($mail, $subject, $content) {
global $wpses_options;
global $SES, $WPSESMSG;
wpses_check_SES();
$WPSESMSG = '';
$rid = wpses_mail($mail, $subject, $content);
$WPSESMSG .= ' id ' . var_export($rid, true);
echo "<div id='wpses-warning' class='updated fade'><p><strong>" . __("Test message has been sent.<br />SES Answer - ", 'wpses') . $WPSESMSG . "</strong></p></div>";
}
function wpses_error_handler($errno, $errstr, $errfile, $errline) {
switch ($errno) {
case E_USER_WARNING:
if (is_admin()) {
echo "<div id='wpses-warning' class='updated fade'><p><strong>" . __("Error : ", 'wpses') . "[$errno] $errstr" . "</strong></p></div>";
}
wpses_log("Error\t$errstr");
break;
}
/* Don't execute PHP internal error handler */
return true;
}
// returns msg id
function wpses_mail($to, $subject, $message, $headers = '', $attachments = '') {
global $SES;
global $wpses_options;
global $wp_better_emails;
// headers can be sent as array, too. convert them to string to avoid further complications.
if (is_array($headers)) {
$headers = implode("\r\n", $headers);
}
if (is_array($to)) {
$to = implode(",", $to);
}
extract(apply_filters('wp_mail', compact('to', 'subject', 'message', 'headers')));
wpses_log('wpses_mail ' . $to . "\t" . $headers);
wpses_check_SES();
if (isset($wp_better_emails)) {
// From wpbe plugin, not efficient nor elegant - Will do better next time.
// Could just call the php filter on a adhoc object, to be less dependant on the implementation of wpbe code.
$txt = wp_specialchars_decode($message, ENT_QUOTES);
$txt = $wp_better_emails->set_email_template($txt, 'plaintext_template');
$txt = apply_filters('wpbe_plaintext_body', $wp_better_emails->template_vars_replacement($txt));
/** HTML ******************************************************* */
$html = $wp_better_emails->esc_textlinks($message);
$html = nl2br(make_clickable($html));
$html = $wp_better_emails->set_email_template($html);
$html = apply_filters('wpbe_html_body', $wp_better_emails->template_vars_replacement($html));
} else {
$message = preg_replace('/<(http:.*)>/', '$1', $message);
$message = preg_replace('/<(https:.*)>/', '$1', $message); // bad hack - handle httpS as well.
$html = $message;
$txt = strip_tags($html);
if (strlen($html) == strlen($txt)) {
$html = ''; // que msg text
}
// no html entity in txt.
$txt = html_entity_decode($txt, ENT_NOQUOTES, 'UTF-8');
}
// TODO: option pour que TXT si msg html, ou les deux comme ici par défaut.
$m = new SimpleEmailServiceMessage();
// To: may contain comma separated emails. If so, explode and add them all.
// what to do if more than 50 ? (SES limit)
if (preg_match('/,/im', $to)) {
$to = explode(',', $to);
foreach ($to as $toline) {
$m->addTo($toline);
}
} else {
$m->addTo($to);
}
$m->setReturnPath($wpses_options['return_path']);
$from = $wpses_options['from_name'] . ' <' . $wpses_options['from_email'] . '>';
if ('' != $wpses_options['reply_to']) {
if ('headers' == strtolower($wpses_options['reply_to'])) {
// extract replyto from headers
wpses_log('extract headers');
$rto = array();
//if (preg_match('/^Reply-To: ([a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4})\b/imsU', $headers, $rto)) {
//if (preg_match('/^Reply-To: (.*)\b/imsU', $headers, $rto)) {
if (preg_match('/^Reply-To: (.*)/im', $headers, $rto)) {
// does only support one email for now.
$m->addReplyTo($rto[1]);
wpses_log('add Reply-To ' . $rto[1]);
}
if (preg_match('/^From: (.*)/im', $headers, $rto)) {
// Uses "From:" header - was /isU which broke things, see https://wordpress.org/support/topic/gravity-forms-18205-latest-contact-form-7-403-latest-not-working
$from = $rto[1];
wpses_log('add from ' . $rto[1]);
}
// Handle multiple cc and bcc: from headers too ? Guess so... TODO
if ('' != $headers) {
$headers = str_replace("\r\n", "\n", $headers);
//wpses_log('headers are ' . $headers);
$lines = explode("\n", $headers);
foreach ($lines as $line) {
wpses_log('Line ' . $line);
if (preg_match('/^cc: (.*)/im', $line, $cc)) {
$m->addCC($cc[1]);
wpses_log('add cc ' . $cc[1]);
}
if (preg_match('/^Bcc: (.*)/im', $line, $Bcc)) {
$m->addBCC($Bcc[1]);
wpses_log('add Bcc ' . $Bcc[1]);
}
}
}
// Test : use full headers if provided
//$m->addCustomHeader($headers);
} else {
$m->addReplyTo($wpses_options['reply_to']);
}
}
$m->setFrom($from);
$m->setSubject($subject);
if ($html == '') { // que texte
$m->setMessageFromString($txt);
} else {
$m->setMessageFromString($txt, $html);
}
// Attachments
if ('' != $attachments) {
if (!is_array($attachments)) {
$attachments = explode("\n", $attachments);
}
// Now we got an array
foreach ($attachments as $afile) {
$m->addAttachmentFromFile(basename($afile), $afile);
}
}
set_error_handler('wpses_error_handler');
$res = $SES->sendEmail($m);
restore_error_handler();
// Call custom Hook
do_action('wpses_mailsent', $to, $subject, $message, $headers, $attachments);
if (is_array($res)) {
wpses_log('SES id=' . $res['MessageId']);
return $res['MessageId'];
} else {
return NULL;
}
}
function wpses_default_options($array, $value) {
global $wpses_options;
foreach ($array as $key) {
if (!isset($wpses_options[$key])) {
$wpses_options[$key] = '';
}
}
}
function wpses_getoptions() {
global $wpses_options;
$wpses_options = get_option('wpses_options');
if (!is_array($wpses_options)) {
$wpses_options = array();
}
wpses_default_options(array('reply_to', 'access_key', 'secret_key', 'from_email', 'from_name', 'return_path'), '');
wpses_default_options(array('sender_ok', 'credentials_ok'), 1);
wpses_default_options(array('force', 'log', 'active'), 0);
if (defined('WP_SES_ENDPOINT')) {
$wpses_options['endpoint'] = WP_SES_ENDPOINT;
}
if (!isset($wpses_options['endpoint'])) {
$wpses_options['endpoint'] = 'email.us-east-1.amazonaws.com';
}
if ('' == $wpses_options['endpoint']) {
$wpses_options['endpoint'] = 'email.us-east-1.amazonaws.com';
}
if (defined('WP_SES_ACCESS_KEY')) {
$wpses_options['access_key'] = WP_SES_ACCESS_KEY;
}
if (defined('WP_SES_SECRET_KEY')) {
$wpses_options['secret_key'] = WP_SES_SECRET_KEY;
}
if (defined('WP_SES_RETURNPATH')) {
if ('' != WP_SES_RETURNPATH) {
$wpses_options['return_path'] = WP_SES_RETURNPATH;
}
}
if (defined('WP_SES_FROM')) {
if ('' != WP_SES_FROM) {
$wpses_options['from_email'] = WP_SES_FROM;
}
}
if (defined('WP_SES_REPLYTO')) {
if ('' != WP_SES_REPLYTO) {
$wpses_options['reply_to'] = WP_SES_REPLYTO;
}
}
if (defined('WP_SES_AUTOACTIVATE')) {
if (WP_SES_AUTOACTIVATE) {
$wpses_options['active'] = 1;
}
}
}
global $wpses_options;
if (!isset($wpses_options)) {
wpses_getoptions();
}
// Test : always auto-activate
//$wpses_options['active'] = 1;
if ($wpses_options['active'] == 1) {
if (!function_exists('wp_mail')) {
function wp_mail($to, $subject, $message, $headers = '', $attachments = '') {
global $wpses_options;
$id = wpses_mail($to, $subject, $message, $headers, $attachments);
if ($id != '') {
return true;
} else {
return false;
}
}
} else {
wpses_log("ERROR\twp_mail override by another plugin !!");
function wpses_warningmail() {
echo "<div id='wpses-warning' class='updated fade'><p><strong>" . __('Another plugin did override wp-mail function. Please de-activate the other plugin if you want WP SES to work properly.', 'wpses') . "</strong></p></div>";
}
add_action('admin_notices', 'wpses_warningmail');
// Desactiver "active" si actif.
if ($wpses_options['active'] == 1) {
wpses_log("Then deactivating plugin");
try {
$func = new ReflectionFunction('wp_mail');
wpses_log('wp_mail already defined in ' . $func->getFileName());
} catch (Exception $e) {
}
$wpses_options['active'] = 0;
update_option('wpses_options', $wpses_options);
}
}
$SES = new SimpleEmailService($wpses_options['access_key'], $wpses_options['secret_key'], $wpses_options['endpoint']);
}
$WPSESMSG = '';