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
|
<?php
namespace SolidWP\Mail\Hooks;
use PHPMailer\PHPMailer\SMTP; use SolidWP\Mail\AbstractController; use SolidWP\Mail\App; use SolidWP\Mail\Connectors\ConnectionPool; use SolidWP\Mail\Connectors\ConnectorSMTP; use SolidWP\Mail\Repository\ProvidersRepository; use SolidWP\Mail\Repository\SettingsRepository; use SolidWP\Mail\SolidMailer;
/** * Class MailerController * * This class is responsible for handling email functionality within the Solid_SMTP plugin. * * @package Solid_SMTP\Controller */ class PHPMailer extends AbstractController {
/** * The repository for managing SMTP mailers. * * @var ProvidersRepository */ protected ProvidersRepository $providers_repository;
/** * The repository for getting settings. * * @var SettingsRepository */ protected SettingsRepository $settings_repository;
/** * Constructor for the class. * * @param ProvidersRepository $providers_repository The repository instance for managing providers. * @param SettingsRepository $settings_repository The settings repository instance. */ public function __construct( ProvidersRepository $providers_repository, SettingsRepository $settings_repository ) { $this->providers_repository = $providers_repository; $this->settings_repository = $settings_repository; }
/** * Register hooks. * * Implementing the InterfaceController interface, this method registers hooks related to email functionality. * * @return void */ public function register_hooks() { add_filter( 'pre_wp_mail', [ $this, 'init_solidmail_mailer' ] ); add_action( 'phpmailer_init', [ $this, 'set_up_connection_pool' ], 9999 ); add_action( 'wp_mail_failed', [ $this, 'maybe_capture_sending_error' ] ); }
/** * Initializes the SolidMail mailer integration. Return null so the default behavior continue to run. * * @since 2.1.3 * * @return null */ public function init_solidmail_mailer() { $active_providers = $this->providers_repository->get_active_providers(); $default_provider = $this->providers_repository->get_default_provider();
if ( ! $default_provider instanceof ConnectorSMTP && count( $active_providers ) === 0 ) { // if there is no active providers and default provider, do nothing. return null; }
// Declare the phpmailer instance before the wp_mail does it. global $phpmailer; if ( ! $phpmailer instanceof SolidMailer ) { require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php'; require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php'; require_once ABSPATH . WPINC . '/PHPMailer/Exception.php'; require_once WPSMTP_PATH . 'inc/wp-phpmailer-compatibility.php';
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited $phpmailer = new SolidMailer( true ); }
return null; }
/** * Captures the sending error if one occurs. * * This function sets the PHPMailer send error variable in the application with the provided WP_Error object. * * @param \WP_Error $error The error object to capture. */ public function maybe_capture_sending_error( \WP_Error $error ) { App::setVar( 'phpmailer_send_error', $error ); }
/** * Configure PHPMailer for SMTP. * * This method is invoked when PHPMailer is initialized to configure it for SMTP usage. * * @param \PHPMailer\PHPMailer\PHPMailer $phpmailer The PHPMailer instance. * * @return void */ public function set_up_connection_pool( \PHPMailer\PHPMailer\PHPMailer $phpmailer ): void { if ( ! $phpmailer instanceof SolidMailer ) { return; }
$connection_pool = new ConnectionPool();
$active_connections = $this->providers_repository->get_active_providers();
// 1. Add connections with a full email match foreach ( $active_connections as $connection ) { if ( $connection->get_from_email() === $phpmailer->From ) { $connection_pool->append( $connection ); } }
// 2. Use a default connection if we could not find at least one // or prioritize the default connection if we want to add unmatched active connections $default_connection = $this->providers_repository->get_default_provider(); if ( $connection_pool->count() === 0 || $this->settings_repository->use_unmatched_connections() ) { $connection_pool->append( $default_connection ); }
// 3. Add unmatched active connections if enabled if ( $this->settings_repository->use_unmatched_connections() ) { foreach ( $active_connections as $connection ) { $connection_pool->append( $connection ); } }
$phpmailer->init_pool( $connection_pool ); }
/** * If Solid Mail is used for the current request * * @return bool */ public static function is_solid_mail_configured(): bool { global $phpmailer; return $phpmailer instanceof SolidMailer && $phpmailer->get_connection() instanceof ConnectorSMTP; } }
|