|
| 1 | +<?php |
| 2 | +namespace EarthAsylumConsulting\Extensions; |
| 3 | + |
| 4 | +if (! class_exists(__NAMESPACE__.'\Simple_SMTP_extension', false) ) |
| 5 | +{ |
| 6 | + /** |
| 7 | + * Extension: simple_smtp - Configure phpmailer using SMTP server |
| 8 | + * |
| 9 | + * @category WordPress Plugin |
| 10 | + * @package {eac}Doojigger\Extensions |
| 11 | + * @author Kevin Burkholder <KBurkholder@EarthAsylum.com> |
| 12 | + * @copyright Copyright (c) 2023 EarthAsylum Consulting <www.EarthAsylum.com> |
| 13 | + * @version 1.x |
| 14 | + * @link https://eacDoojigger.earthasylum.com/ |
| 15 | + * @see https://eacDoojigger.earthasylum.com/phpdoc/ |
| 16 | + */ |
| 17 | + |
| 18 | + class Simple_SMTP_extension extends \EarthAsylumConsulting\abstract_extension |
| 19 | + { |
| 20 | + /** |
| 21 | + * @var string extension version |
| 22 | + */ |
| 23 | + const VERSION = '23.0608.1'; |
| 24 | + |
| 25 | + |
| 26 | + /** |
| 27 | + * constructor method |
| 28 | + * |
| 29 | + * @param object $plugin main plugin object |
| 30 | + * @return void |
| 31 | + */ |
| 32 | + public function __construct($plugin) |
| 33 | + { |
| 34 | + parent::__construct($plugin, self::DEFAULT_DISABLED | self::ALLOW_ALL); |
| 35 | + |
| 36 | + if ($this->is_admin()) |
| 37 | + { |
| 38 | + $this->registerExtension( [$this->className,'Simple SMTP'] ); |
| 39 | + // Register plugin options when needed |
| 40 | + $this->add_action( "options_settings_page", array($this, 'admin_options_settings') ); |
| 41 | + // Add contextual help |
| 42 | + $this->add_action( 'options_settings_help', array($this, 'admin_options_help') ); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + /** |
| 48 | + * register options on options_settings_page |
| 49 | + * |
| 50 | + * @access public |
| 51 | + * @return void |
| 52 | + */ |
| 53 | + public function admin_options_settings() |
| 54 | + { |
| 55 | + require 'includes/simple_smtp.options.php'; |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + /** |
| 60 | + * Add help tab on admin page |
| 61 | + * |
| 62 | + * @return void |
| 63 | + */ |
| 64 | + public function admin_options_help() |
| 65 | + { |
| 66 | + if (!$this->plugin->isSettingsPage('Simple SMTP')) return; |
| 67 | + |
| 68 | + require 'includes/simple_smtp.help.php'; |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * initialize method - called from main plugin |
| 74 | + * |
| 75 | + * @return void |
| 76 | + */ |
| 77 | + public function initialize() |
| 78 | + { |
| 79 | + if ( ! parent::initialize() ) return; // disabled |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + /** |
| 84 | + * Add filters and actions - called from main plugin |
| 85 | + * |
| 86 | + * @return void |
| 87 | + */ |
| 88 | + public function addActionsAndFilters() |
| 89 | + { |
| 90 | + parent::addActionsAndFilters(); |
| 91 | + |
| 92 | + \add_action( 'phpmailer_init', array( $this, 'phpmailer_init' ) ); |
| 93 | + \add_filter( 'wp_mail', array( $this, 'wp_mail_headers' ), 999); |
| 94 | + |
| 95 | + if ($this->is_option('smtp_override')) |
| 96 | + { |
| 97 | + if ($from = $this->get_option('smtp_fromemail')) |
| 98 | + { |
| 99 | + \add_filter( 'wp_mail_from', function($email) use ($from) |
| 100 | + { |
| 101 | + return $from; |
| 102 | + }, 999); |
| 103 | + } |
| 104 | + if ($from = $this->get_option('smtp_fromname')) |
| 105 | + { |
| 106 | + \add_filter( 'wp_mail_from_name', function($name) use ($from) |
| 107 | + { |
| 108 | + return $from; |
| 109 | + }, 999); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + \add_filter( 'simpleSMTP_from_name', function($from=null) |
| 114 | + { |
| 115 | + return $this->get_option('smtp_fromname') ?: $from; |
| 116 | + } |
| 117 | + ); |
| 118 | + \add_filter( 'simpleSMTP_from_email', function($from=null) |
| 119 | + { |
| 120 | + return $this->get_option('smtp_fromemail') ?: $from; |
| 121 | + } |
| 122 | + ); |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + /** |
| 128 | + * set phpmailer options for SMTP server use (called from wp_mail) |
| 129 | + * |
| 130 | + * @param object $phpmailer - phpmailer object |
| 131 | + * @return void |
| 132 | + */ |
| 133 | + public function phpmailer_init(object $phpmailer) |
| 134 | + { |
| 135 | + if ($debugLevel = $this->get_option('smtp_debug')) |
| 136 | + { |
| 137 | + $phpmailer->SMTPDebug = $debugLevel; |
| 138 | + \add_action( 'wp_mail_succeeded', array( $this, 'wp_mail_complete' )); |
| 139 | + \add_action( 'wp_mail_failed', array( $this, 'wp_mail_complete' )); |
| 140 | + ob_start(); |
| 141 | + } |
| 142 | + |
| 143 | + if ($this->is_option('smtp_server')) |
| 144 | + { |
| 145 | + $phpmailer->isSMTP(); |
| 146 | + $phpmailer->Host = $this->get_option('smtp_server'); |
| 147 | + $phpmailer->Port = $this->get_option('smtp_port'); |
| 148 | + } |
| 149 | + |
| 150 | + if ( ($username = $this->get_option_decrypt('smtp_username')) && ($password = $this->get_option_decrypt('smtp_password')) ) |
| 151 | + { |
| 152 | + $phpmailer->SMTPAuth = true; |
| 153 | + $phpmailer->Username = $username; |
| 154 | + $phpmailer->Password = $password; |
| 155 | + } |
| 156 | + else |
| 157 | + { |
| 158 | + $phpmailer->SMTPAuth = false; |
| 159 | + } |
| 160 | + |
| 161 | + $encryption = $this->get_option('smtp_encryption'); |
| 162 | + if ($encryption && $encryption != 'none') |
| 163 | + { |
| 164 | + $phpmailer->SMTPSecure = $encryption; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | + /** |
| 170 | + * debugging - after when wp_mail sends email |
| 171 | + * |
| 172 | + * @param array|wp_error $mail_data An array containing the mail recipient, subject, message, headers, and attachments |
| 173 | + * @return void |
| 174 | + */ |
| 175 | + public function wp_mail_complete($mail_data) |
| 176 | + { |
| 177 | + $debug = ob_get_clean(); |
| 178 | + $this->plugin->logDebug([$mail_data,$debug],__METHOD__); |
| 179 | + } |
| 180 | + |
| 181 | + |
| 182 | + /** |
| 183 | + * wp_mail custom headers |
| 184 | + * |
| 185 | + * @param string|array $args - wp_mail args |
| 186 | + * @param string $headers - smtp_headers |
| 187 | + * @return void |
| 188 | + */ |
| 189 | + public function wp_mail_headers($args) |
| 190 | + { |
| 191 | + if (! ($headers = $this->get_option('smtp_headers')) ) |
| 192 | + { |
| 193 | + return $args; |
| 194 | + } |
| 195 | + |
| 196 | + $headers = explode( "\n", str_replace( "\r\n", "\n", $headers ) ); |
| 197 | + |
| 198 | + if (! empty($args['headers']) ) |
| 199 | + { |
| 200 | + if (! is_array($args['headers']) ) { |
| 201 | + $args['headers'] = explode( "\n", str_replace( "\r\n", "\n", $args['headers'] ) ); |
| 202 | + } |
| 203 | + $args['headers'] = array_merge($headers,$args['headers']); |
| 204 | + } |
| 205 | + else |
| 206 | + { |
| 207 | + $args['headers'] = $headers; |
| 208 | + } |
| 209 | + return $args; |
| 210 | + } |
| 211 | + |
| 212 | + |
| 213 | + /** |
| 214 | + * filter for options_form_post_ _smtp_testemail |
| 215 | + * |
| 216 | + * @param string $email - the value POSTed |
| 217 | + * @param string $fieldName - the name of the field/option |
| 218 | + * @param array $metaData - the option metadata |
| 219 | + * @param string $priorValue - the prior option value |
| 220 | + * @return string $email |
| 221 | + */ |
| 222 | + public function send_test_email($email, $fieldName=null, $metaData=null, $priorValue=null) |
| 223 | + { |
| 224 | + if (! ($testEmail = filter_var( $email, FILTER_VALIDATE_EMAIL )) ) |
| 225 | + { |
| 226 | + return $email; |
| 227 | + } |
| 228 | + |
| 229 | + $content = require 'includes/simple_smtp.email.php'; |
| 230 | + |
| 231 | + $result = wp_mail( |
| 232 | + $testEmail, |
| 233 | + \get_option('blogname')." SMTP Email Test", |
| 234 | + $content, |
| 235 | + [ |
| 236 | + "from: ".$this->get_option('smtp_fromname').' <'.$this->get_option('smtp_fromemail').'>', |
| 237 | + "Content-type: text/html" |
| 238 | + ] |
| 239 | + ); |
| 240 | + } |
| 241 | + } |
| 242 | +} |
| 243 | +/** |
| 244 | + * return a new instance of this class |
| 245 | + */ |
| 246 | +if (isset($this)) return new Simple_SMTP_extension($this); |
| 247 | +?> |
0 commit comments