| @@ -3,10 +3,10 @@ | ||
| 3 | 3 | /** |
| 4 | 4 | * Plugin Name: Mail Queue |
| 5 | 5 | * Plugin URI: https://www.webdesign-muenchen.de/wordpress-plugin-mail-queue/ |
| 6 | 6 | * Description: Take Control and improve Security of wp_mail(). Queue outgoing emails, log their sendings and get alerted, if your website wants to send more emails than usual. |
| 7 | - * Version: 1.3 | |
| 8 | - * Requires at least: 5.9 | |
| 7 | + * Version: 1.1 | |
| 8 | + * Requires at least: 5.2 | |
| 9 | 9 | * Requires PHP: 7.2 |
| 10 | 10 | * Author: WDM |
| 11 | 11 | * Author URI: https://www.webdesign-muenchen.de |
| 12 | 12 | * License: GPLv3 or later |
| @@ -26,24 +26,15 @@ | ||
| 26 | 26 | 'email' => get_option('admin_email'), |
| 27 | 27 | 'email_amount' => '10', |
| 28 | 28 | 'queue_amount' => '1', |
| 29 | 29 | 'queue_interval' => '5', |
| 30 | - 'queue_interval_unit' => 'minutes', | |
| 31 | 30 | 'clear_queue' => '14', |
| 32 | 31 | 'tableName' => 'mail_queue', |
| 33 | - 'triggercount' => 0, | |
| 34 | 32 | ); |
| 35 | 33 | $args = get_option('wdm_wpma_settings'); |
| 36 | 34 | $options = wp_parse_args($args,$defaults); |
| 37 | - | |
| 38 | - if ($options['queue_interval_unit'] == 'seconds') { | |
| 39 | - $options['queue_interval'] = intval($options['queue_interval']); | |
| 40 | - if ($options['queue_interval'] < 10) { $options['queue_interval'] = 10; } // Minimum Interval 10 Seconds | |
| 41 | - } else { | |
| 42 | - $options['queue_interval'] = intval($options['queue_interval']) * 60; | |
| 43 | - } | |
| 44 | - | |
| 45 | - $options['clear_queue'] = intval($options['clear_queue']) * 24; | |
| 35 | + $options['queue_interval'] = intval($options['queue_interval']) * 60; | |
| 36 | + $options['clear_queue'] = intval($options['clear_queue']) * 24; | |
| 46 | 37 | return $options; |
| 47 | 38 | } |
| 48 | 39 | |
| 49 | 40 | |
| @@ -55,85 +46,59 @@ | ||
| 55 | 46 | **************************************************************** */ |
| 56 | 47 | $wdm_wpma_mailid = 0; |
| 57 | 48 | $wdm_wpma_options = wdm_wpma_get_settings(); // Get Settings |
| 58 | 49 | |
| 59 | -if ($wdm_wpma_options['enabled'] == '1' && wp_doing_cron() == false) { | |
| 60 | - add_filter( 'pre_wp_mail' , 'wdm_wpma_prewpmail', 10, 2); | |
| 61 | -} | |
| 50 | +if ($wdm_wpma_options['enabled'] == '1' && !defined( 'DOING_CRON' )) { | |
| 62 | 51 | |
| 63 | -// pre WP Mail Filter | |
| 64 | -function wdm_wpma_prewpmail($null, $atts) { | |
| 52 | + if ( !function_exists( 'wp_mail' ) ) { | |
| 53 | + function wp_mail ( $to, $subject, $message, $headers = '', $attachments = [] ) { | |
| 65 | 54 | |
| 66 | - global $wpdb, $wdm_wpma_options; | |
| 55 | + global $wpdb, $wdm_wpma_options; | |
| 67 | 56 | |
| 68 | - // Mail Variables | |
| 69 | - $to = $atts['to']; | |
| 70 | - $subject = $atts['subject']; | |
| 71 | - $message = $atts['message']; | |
| 72 | - $headers = $atts['headers']; | |
| 73 | - $attachments = $atts['attachments']; | |
| 74 | - $status = 'queue'; | |
| 57 | + // Write email in Queue | |
| 58 | + $tableName = $wpdb->prefix.$wdm_wpma_options['tableName']; | |
| 59 | + $data = array( | |
| 60 | + 'timestamp'=> current_time('mysql',false), | |
| 61 | + 'recipient'=> maybe_serialize($to), | |
| 62 | + 'subject'=> $subject, | |
| 63 | + 'message'=> $message, | |
| 64 | + 'status' => 'queue', | |
| 65 | + 'attachments' => '' | |
| 66 | + ); | |
| 67 | + if (isset($headers) && $headers) { $data['headers'] = maybe_serialize($headers); } | |
| 75 | 68 | |
| 76 | - // Instant Sending or Prio Mail? | |
| 77 | - if (isset($headers) && is_array($headers) && count($headers) > 0) { | |
| 78 | - foreach($headers as $index => $val) { | |
| 79 | - if (preg_match("#^X-Mail-Queue-Prio: +Instant *$#i",$val)) { | |
| 80 | - array_splice($headers,$index,1); | |
| 81 | - $status = 'instant'; | |
| 82 | - break; | |
| 83 | - } else if (preg_match("#^X-Mail-Queue-Prio: +High *$#i",$val)) { | |
| 84 | - array_splice($headers,$index,1); | |
| 85 | - $status = 'high'; | |
| 86 | - break; | |
| 69 | + // store attachments in /attachments/ Folder, to address them later | |
| 70 | + if (isset($attachments) && $attachments && $attachments != '') { | |
| 71 | + | |
| 72 | + $subfolder = time().'-'.rand(0,999999); | |
| 73 | + $foldercreated = wp_mkdir_p(plugin_dir_path(__FILE__).'attachments/'.$subfolder); | |
| 74 | + if (!$foldercreated) { | |
| 75 | + error_log('Could not create Subfolder for Email attachment'); | |
| 76 | + $data['info'] = 'Error: Could not store attachments'; | |
| 77 | + } else { | |
| 78 | + if (!is_array($attachments)) { $attachments = array($attachments); } | |
| 79 | + $newattachments = array(); | |
| 80 | + global $wp_filesystem; | |
| 81 | + if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){ | |
| 82 | + include_once(ABSPATH . 'wp-admin/includes/file.php'); | |
| 83 | + WP_Filesystem(); | |
| 84 | + } | |
| 85 | + foreach($attachments as $item) { | |
| 86 | + $newfile = plugin_dir_path(__FILE__).'attachments/'.$subfolder.'/'.basename($item); | |
| 87 | + $wp_filesystem->copy($item,$newfile); | |
| 88 | + array_push($newattachments,$newfile); | |
| 89 | + } | |
| 90 | + $data['attachments'] = maybe_serialize($newattachments); | |
| 91 | + } | |
| 87 | 92 | } |
| 88 | - } | |
| 89 | - } | |
| 93 | + $wpdb->insert($tableName,$data); | |
| 90 | 94 | |
| 91 | - // Write email in Queue | |
| 92 | - $tableName = $wpdb->prefix.$wdm_wpma_options['tableName']; | |
| 93 | - $data = array( | |
| 94 | - 'timestamp'=> current_time('mysql',false), | |
| 95 | - 'recipient'=> maybe_serialize($to), | |
| 96 | - 'subject'=> $subject, | |
| 97 | - 'message'=> $message, | |
| 98 | - 'status' => $status, | |
| 99 | - 'attachments' => '' | |
| 100 | - ); | |
| 101 | - if (isset($headers) && $headers) { $data['headers'] = maybe_serialize($headers); } | |
| 95 | + // Fake Submit by returning 'True' | |
| 96 | + return true; | |
| 102 | 97 | |
| 103 | - // store attachments in /attachments/ Folder, to address them later | |
| 104 | - if (isset($attachments) && $attachments && $attachments != '') { | |
| 105 | - | |
| 106 | - $subfolder = time().'-'.rand(0,999999); | |
| 107 | - $foldercreated = wp_mkdir_p(plugin_dir_path(__FILE__).'attachments/'.$subfolder); | |
| 108 | - if (!$foldercreated) { | |
| 109 | - error_log('Could not create Subfolder for Email attachment'); | |
| 110 | - $data['info'] = 'Error: Could not store attachments'; | |
| 111 | - } else { | |
| 112 | - if (!is_array($attachments)) { $attachments = array($attachments); } | |
| 113 | - $newattachments = array(); | |
| 114 | - global $wp_filesystem; | |
| 115 | - if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){ | |
| 116 | - include_once(ABSPATH . 'wp-admin/includes/file.php'); | |
| 117 | - WP_Filesystem(); | |
| 118 | - } | |
| 119 | - foreach($attachments as $item) { | |
| 120 | - $newfile = plugin_dir_path(__FILE__).'attachments/'.$subfolder.'/'.basename($item); | |
| 121 | - $wp_filesystem->copy($item,$newfile); | |
| 122 | - array_push($newattachments,$newfile); | |
| 123 | - } | |
| 124 | - $data['attachments'] = maybe_serialize($newattachments); | |
| 125 | 98 | } |
| 126 | 99 | } |
| 127 | - $wpdb->insert($tableName,$data); | |
| 128 | 100 | |
| 129 | - if ($status == 'instant') { | |
| 130 | - return null; | |
| 131 | - } else { | |
| 132 | - // Fake Submit by returning 'True' | |
| 133 | - return true; | |
| 134 | - } | |
| 135 | - | |
| 136 | 101 | } |
| 137 | 102 | |
| 138 | 103 | |
| 139 | 104 | |
| @@ -151,21 +116,21 @@ | ||
| 151 | 116 | |
| 152 | 117 | |
| 153 | 118 | |
| 154 | 119 | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 155 | 124 | /* *************************************************************** |
| 156 | 125 | CRON |
| 157 | 126 | **************************************************************** */ |
| 158 | 127 | function wdm_wpma_search_mail_from_queue() { |
| 159 | - | |
| 128 | + | |
| 160 | 129 | global $wpdb,$wdm_wpma_options,$wdm_wpma_mailid; |
| 161 | 130 | if ($wdm_wpma_options['enabled'] != '1') { return; } |
| 162 | 131 | $tableName = $wpdb->prefix.$wdm_wpma_options['tableName']; |
| 163 | 132 | |
| 164 | - // Triggercount to avoid multiple runs | |
| 165 | - $wdm_wpma_options['triggercount']++; | |
| 166 | - if ($wdm_wpma_options['triggercount'] > 1) { return; } | |
| 167 | - | |
| 168 | 133 | // Mails in Queue? |
| 169 | 134 | $mailjobs = $wpdb->get_results("SELECT * FROM `$tableName` WHERE `status` = 'queue' ORDER BY `id`",'ARRAY_A'); |
| 170 | 135 | $mailsInQueue = is_array($mailjobs) ? count($mailjobs) : 0; |
| 171 | 136 | |
| @@ -180,9 +145,9 @@ | ||
| 180 | 145 | $alertMessage = 'Hi,'; |
| 181 | 146 | $alertMessage .= "\n\n"; |
| 182 | 147 | $alertMessage .= 'this is an important message from your WordPress website '.esc_url(get_option('siteurl')).'.'; |
| 183 | 148 | $alertMessage .= "\n"; |
| 184 | - $alertMessage .= "\n".'The Mail Queue Plugin has detected that your website tries to send more emails than expected (currently '.$mailsInQueue.').'; | |
| 149 | + $alertMessage .= "\n".'The Mail Queue Plugin has detected that your website tries to send more emails than expected.'; | |
| 185 | 150 | $alertMessage .= "\n".'Please take a close look at the email queue, because it contains more messages than the specified limit.'; |
| 186 | 151 | $alertMessage .= "\n"; |
| 187 | 152 | $alertMessage .= "\n".'In case this is the usual amount of emails, you can adjust the threshold for alerts in the settings of your Mail Queue Plugin.'; |
| 188 | 153 | $alertMessage .= "\n\n"; |
| @@ -212,17 +177,13 @@ | ||
| 212 | 177 | // Send Mails in Queue |
| 213 | 178 | if ($mailsInQueue > 0) { |
| 214 | 179 | $results = array_slice($mailjobs,0,intval($wdm_wpma_options['queue_amount'])); |
| 215 | 180 | if ($results && count($results) > 0) { |
| 216 | - foreach($results as $index => $item) { | |
| 181 | + foreach($results as $item) { | |
| 217 | 182 | if ($item['recipient'] && $item['recipient'] != '') { $to = maybe_unserialize($item['recipient']); } else { $to = $wdm_wpma_options['email']; $item['subject'] = 'ERROR // '.$item['subject']; } |
| 218 | 183 | if ($item['headers'] && $item['headers'] != '') { $headers = maybe_unserialize($item['headers']); } else { $headers = ''; } |
| 219 | 184 | if ($item['attachments'] && $item['attachments'] != '') { $attachments = maybe_unserialize($item['attachments']); } else { $attachments = ''; } |
| 220 | - $wdm_wpma_mailid = $item['id']; | |
| 221 | - | |
| 222 | - //$item['subject'] .= ' // Timestamp '.time().' // BlogID '.$wpdb->blogid.' // Cron '.wp_doing_cron().' // Resultscount '.count($results).' // Index '.$index; | |
| 223 | - | |
| 224 | - remove_filter('pre_wp_mail','wdm_wpma_prewpmail',10); | |
| 185 | + $wdm_wpma_mailid = $item['id']; | |
| 225 | 186 | $sendstatus = wp_mail($to,$item['subject'],$item['message'],$headers,$attachments); // Finally sends the email for real |
| 226 | 187 | if ($sendstatus) { |
| 227 | 188 | $wpdb->update($tableName,array('timestamp'=>current_time('mysql',false),'status'=>'sent'),array('id'=>$item['id']),'%s','%d'); |
| 228 | 189 | } |
| @@ -280,13 +241,8 @@ | ||
| 280 | 241 | $tableName = $wpdb->prefix.'mail_queue'; |
| 281 | 242 | $wpdb->query( "DROP TABLE IF EXISTS $tableName" ); |
| 282 | 243 | } |
| 283 | 244 | |
| 284 | -/* Delete Cron when Plugin deactivated */ | |
| 285 | -function wdm_wpma_deactivate() { | |
| 286 | - wp_clear_scheduled_hook( 'wp_mail_queue_hook' ); | |
| 287 | -} | |
| 288 | - | |
| 289 | 245 | /* Create MySQL Table on Activation: https://codex.wordpress.org/Creating_Tables_with_Plugins */ |
| 290 | 246 | function wdm_wpma_installDatabaseTables() { |
| 291 | 247 | global $wpdb; |
| 292 | 248 | |
| @@ -310,11 +266,8 @@ | ||
| 310 | 266 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 311 | 267 | dbDelta( $sql ); |
| 312 | 268 | |
| 313 | 269 | register_uninstall_hook( __FILE__, 'wdm_wpma_uninstall' ); |
| 314 | - | |
| 315 | - register_deactivation_hook( __FILE__, 'wdm_wpma_deactivate' ); | |
| 316 | - | |
| 317 | 270 | } |
| 318 | 271 | register_activation_hook( __FILE__, 'wdm_wpma_installDatabaseTables' ); |
| 319 | 272 | |
| 320 | 273 | |