PluginProbe ʕ •ᴥ•ʔ
WP Mail SMTP by WPForms – The Most Popular SMTP and Email Log Plugin / 3.5.2
WP Mail SMTP by WPForms – The Most Popular SMTP and Email Log Plugin v3.5.2
0.9.6 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.2 1.7.0 1.7.1 1.8.0 1.8.1 1.9.0 2.0.0 2.0.1 2.1.1 2.2.1 2.3.1 2.4.0 2.5.0 2.5.1 2.6.0 2.7.0 2.8.0 2.9.0 3.0.1 3.0.2 3.0.3 3.1.0 3.10.0 3.11.0 3.11.1 3.2.0 3.2.1 3.3.0 3.4.0 3.5.0 3.5.1 3.5.2 3.6.1 3.7.0 3.8.0 3.8.2 3.9.0 4.0.1 4.1.0 4.1.1 4.2.0 4.3.0 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.8.0 trunk 0.10.0 0.10.1 0.11.1 0.11.2 0.3.1 0.3.2 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.5.2 0.6 0.7 0.8 0.8.2 0.8.3 0.8.4 0.8.5 0.8.6 0.8.7 0.9.0 0.9.1 0.9.2 0.9.3 0.9.4 0.9.5
wp-mail-smtp / src / Processor.php
wp-mail-smtp / src Last commit date
Admin 3 years ago Compatibility 3 years ago Helpers 3 years ago Providers 3 years ago Reports 3 years ago Tasks 3 years ago UsageTracking 3 years ago Conflicts.php 3 years ago Connect.php 3 years ago Core.php 3 years ago Debug.php 3 years ago Geo.php 3 years ago MailCatcher.php 3 years ago MailCatcherInterface.php 3 years ago MailCatcherV6.php 3 years ago Migration.php 3 years ago MigrationAbstract.php 3 years ago Options.php 3 years ago Processor.php 3 years ago SiteHealth.php 3 years ago Upgrade.php 3 years ago Uploads.php 3 years ago WP.php 3 years ago
Processor.php
338 lines
1 <?php
2
3 namespace WPMailSMTP;
4
5 /**
6 * Class Processor modifies the behaviour of wp_mail() function.
7 *
8 * @since 1.0.0
9 */
10 class Processor {
11
12 /**
13 * This attribute will hold the "original" WP from email address passed to the wp_mail_from filter,
14 * that is not equal to the default email address.
15 *
16 * It should hold an email address set via the wp_mail_from filter, before we might overwrite it.
17 *
18 * @since 2.1.0
19 *
20 * @var string
21 */
22 protected $wp_mail_from;
23
24 /**
25 * Assign all hooks to proper places.
26 *
27 * @since 1.0.0
28 */
29 public function hooks() {
30
31 add_action( 'phpmailer_init', array( $this, 'phpmailer_init' ) );
32
33 // High priority number tries to ensure our plugin code executes last and respects previous hooks, if not forced.
34 add_filter( 'wp_mail_from', array( $this, 'filter_mail_from_email' ), PHP_INT_MAX );
35 add_filter( 'wp_mail_from_name', array( $this, 'filter_mail_from_name' ), PHP_INT_MAX );
36 }
37
38 /**
39 * Redefine certain PHPMailer options with our custom ones.
40 *
41 * @since 1.0.0
42 *
43 * @param \PHPMailer $phpmailer It's passed by reference, so no need to return anything.
44 */
45 public function phpmailer_init( $phpmailer ) { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded
46
47 $options = Options::init();
48 $mailer = $options->get( 'mail', 'mailer' );
49
50 // Check that mailer is not blank, and if mailer=smtp, host is not blank.
51 if (
52 ! $mailer ||
53 ( 'smtp' === $mailer && ! $options->get( 'smtp', 'host' ) )
54 ) {
55 return;
56 }
57
58 // If the mailer is pepipost, make sure we have a username and password.
59 if (
60 'pepipost' === $mailer &&
61 ( ! $options->get( 'pepipost', 'user' ) && ! $options->get( 'pepipost', 'pass' ) )
62 ) {
63 return;
64 }
65
66 // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
67
68 // Set the mailer type as per config above, this overrides the already called isMail method.
69 // It's basically always 'smtp'.
70 $phpmailer->Mailer = $mailer;
71
72 // Set the Sender (return-path) if required.
73 if ( $options->get( 'mail', 'return_path' ) ) {
74 $phpmailer->Sender = $phpmailer->From;
75 }
76
77 // Set the SMTPSecure value, if set to none, leave this blank. Possible values: 'ssl', 'tls', ''.
78 if ( 'none' === $options->get( $mailer, 'encryption' ) ) {
79 $phpmailer->SMTPSecure = '';
80 } else {
81 $phpmailer->SMTPSecure = $options->get( $mailer, 'encryption' );
82 }
83
84 // Check if user has disabled SMTPAutoTLS.
85 if ( $options->get( $mailer, 'encryption' ) !== 'tls' && ! $options->get( $mailer, 'autotls' ) ) {
86 $phpmailer->SMTPAutoTLS = false;
87 }
88
89 // Check if original WP from email can be set as the reply_to attribute.
90 if ( $this->allow_setting_original_from_email_to_reply_to( $phpmailer->getReplyToAddresses(), $mailer ) ) {
91 $phpmailer->addReplyTo( $this->wp_mail_from );
92 }
93
94 // If we're sending via SMTP, set the host.
95 if ( 'smtp' === $mailer ) {
96 // Set the other options.
97 $phpmailer->Host = $options->get( $mailer, 'host' );
98 $phpmailer->Port = $options->get( $mailer, 'port' );
99
100 // If we're using smtp auth, set the username & password.
101 if ( $options->get( $mailer, 'auth' ) ) {
102 $phpmailer->SMTPAuth = true;
103 $phpmailer->Username = $options->get( $mailer, 'user' );
104 $phpmailer->Password = $options->get( $mailer, 'pass' );
105 }
106 } elseif ( 'pepipost' === $mailer ) {
107 // Set the Pepipost settings for BC.
108 $phpmailer->Mailer = 'smtp';
109 $phpmailer->Host = 'smtp.pepipost.com';
110 $phpmailer->Port = $options->get( $mailer, 'port' );
111 $phpmailer->SMTPSecure = $options->get( $mailer, 'encryption' ) === 'none' ? '' : $options->get( $mailer, 'encryption' );
112 $phpmailer->SMTPAuth = true;
113 $phpmailer->Username = $options->get( $mailer, 'user' );
114 $phpmailer->Password = $options->get( $mailer, 'pass' );
115 }
116 // phpcs:enable
117
118 // Maybe set default reply-to header.
119 $this->set_default_reply_to( $phpmailer );
120
121 // You can add your own options here.
122 // See the phpmailer documentation for more info: https://github.com/PHPMailer/PHPMailer/tree/5.2-stable.
123 /* @noinspection PhpUnusedLocalVariableInspection It's passed by reference. */
124 $phpmailer = apply_filters( 'wp_mail_smtp_custom_options', $phpmailer );
125 }
126
127 /**
128 * Check if it's allowed to set the original WP from email to the reply_to field.
129 *
130 * @since 2.1.0
131 *
132 * @param array $reply_to Array of currently set reply to emails.
133 * @param string $mailer The slug of current mailer.
134 *
135 * @return bool
136 */
137 protected function allow_setting_original_from_email_to_reply_to( $reply_to, $mailer ) {
138
139 $options = Options::init();
140 $forced = $options->get( 'mail', 'from_email_force' );
141 $from_email = $options->get( 'mail', 'from_email' );
142
143 if ( ! empty( $reply_to ) || empty( $this->wp_mail_from ) ) {
144 return false;
145 }
146
147 if ( in_array( $mailer, [ 'zoho' ], true ) ) {
148 $sender = $options->get( $mailer, 'user_details' );
149 $from_email = ! empty( $sender['email'] ) ? $sender['email'] : '';
150 $forced = true;
151 }
152
153 if (
154 $from_email === $this->wp_mail_from ||
155 ! $forced
156 ) {
157 return false;
158 }
159
160 return true;
161 }
162
163 /**
164 * This method will be called every time 'smtp' and 'mail' mailers will be used to send emails.
165 *
166 * @since 1.3.0
167 * @since 1.5.0 Added a do_action() to be able to hook into.
168 *
169 * @param bool $is_sent If the email was sent.
170 * @param array $to To email address.
171 * @param array $cc CC email addresses.
172 * @param array $bcc BCC email addresses.
173 * @param string $subject The email subject.
174 * @param string $body The email body.
175 * @param string $from The from email address.
176 */
177 public static function send_callback( $is_sent, $to, $cc, $bcc, $subject, $body, $from ) {
178
179 if ( ! $is_sent ) {
180 // Add mailer to the beginning and save to display later.
181 Debug::set(
182 'Mailer: ' . esc_html( wp_mail_smtp()->get_providers()->get_options( Options::init()->get( 'mail', 'mailer' ) )->get_title() ) . "\r\n" .
183 'PHPMailer was able to connect to SMTP server but failed while trying to send an email.'
184 );
185 } else {
186 Debug::clear();
187 }
188
189 do_action( 'wp_mail_smtp_mailcatcher_smtp_send_after', $is_sent, $to, $cc, $bcc, $subject, $body, $from );
190 }
191
192 /**
193 * Modify the email address that is used for sending emails.
194 *
195 * @since 1.0.0
196 * @since 1.3.0 Forcing email rewrite if option is selected.
197 * @since 1.7.0 Default email may be empty, so pay attention to that as well.
198 *
199 * @param string $wp_email The email address passed by the filter.
200 *
201 * @return string
202 */
203 public function filter_mail_from_email( $wp_email ) {
204
205 $options = Options::init();
206 $forced = $options->get( 'mail', 'from_email_force' );
207 $from_email = $options->get( 'mail', 'from_email' );
208 $def_email = WP::get_default_email();
209
210 // Save the "original" set WP email from address for later use.
211 if ( $wp_email !== $def_email ) {
212 $this->wp_mail_from = filter_var( $wp_email, FILTER_VALIDATE_EMAIL );
213 }
214
215 // Return FROM EMAIL if forced in settings.
216 if ( $forced && ! empty( $from_email ) ) {
217 return $from_email;
218 }
219
220 // If the FROM EMAIL is not the default, return it unchanged.
221 if ( ! empty( $def_email ) && $wp_email !== $def_email ) {
222 return $wp_email;
223 }
224
225 return ! empty( $from_email ) ? $from_email : $wp_email;
226 }
227
228 /**
229 * Modify the sender name that is used for sending emails.
230 *
231 * @since 1.0.0
232 * @since 1.3.0 Forcing name rewrite if option is selected.
233 *
234 * @param string $name The from name passed through the filter.
235 *
236 * @return string
237 */
238 public function filter_mail_from_name( $name ) {
239
240 $options = Options::init();
241 $force = $options->get( 'mail', 'from_name_force' );
242
243 // If the FROM NAME is not the default and not forced, return it unchanged.
244 if ( ! $force && $name !== $this->get_default_name() ) {
245 return $name;
246 }
247
248 $name = $options->get( 'mail', 'from_name' );
249
250 return $name;
251 }
252
253 /**
254 * Get the default email address based on domain name.
255 *
256 * @since 1.0.0
257 * @since 1.7.0 May return an empty string.
258 *
259 * @return string Empty string when we aren't able to get the site domain (CLI, misconfigured server etc).
260 */
261 public function get_default_email() {
262
263 $server_name = Geo::get_site_domain();
264
265 if ( empty( $server_name ) ) {
266 return '';
267 }
268
269 // Get rid of www.
270 $sitename = strtolower( $server_name );
271 if ( substr( $sitename, 0, 4 ) === 'www.' ) {
272 $sitename = substr( $sitename, 4 );
273 }
274
275 return 'wordpress@' . $sitename;
276 }
277
278 /**
279 * Get the default email FROM NAME generated by WordPress.
280 *
281 * @since 1.3.0
282 *
283 * @return string
284 */
285 public function get_default_name() {
286 return 'WordPress';
287 }
288
289 /**
290 * Get or create the phpmailer.
291 *
292 * @since 1.9.0
293 *
294 * @return MailCatcherInterface
295 */
296 public function get_phpmailer() {
297
298 global $phpmailer;
299
300 // Make sure the PHPMailer class has been instantiated.
301 if ( ! is_object( $phpmailer ) || ! is_a( $phpmailer, 'PHPMailer' ) ) {
302 $phpmailer = wp_mail_smtp()->generate_mail_catcher( true ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
303 }
304
305 return $phpmailer;
306 }
307
308 /**
309 * Set the default reply_to header, if:
310 * - no other reply_to headers are already set and,
311 * - the default reply_to address filter `wp_mail_smtp_processor_default_reply_to_addresses` is configured.
312 *
313 * @since 2.1.1
314 *
315 * @param MailCatcherInterface $phpmailer The PHPMailer object.
316 */
317 private function set_default_reply_to( $phpmailer ) {
318
319 if ( ! empty( $phpmailer->getReplyToAddresses() ) ) {
320 return;
321 }
322
323 $default_reply_to_emails = apply_filters( 'wp_mail_smtp_processor_set_default_reply_to', '' );
324
325 if ( empty( $default_reply_to_emails ) ) {
326 return;
327 }
328
329 foreach ( explode( ',', $default_reply_to_emails ) as $email ) {
330 $email = trim( $email );
331
332 if ( filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
333 $phpmailer->addReplyTo( $email );
334 }
335 }
336 }
337 }
338