/var/www/html_us/wp-content/plugins/wp-smtp/src/Mail/Hooks/PHPMailer.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
<?php

namespace SolidWP\Mail\Hooks;

use 
PHPMailer\PHPMailer\SMTP;
use 
SolidWP\Mail\AbstractController;
use 
SolidWP\Mail\App;
use 
SolidWP\Mail\Repository\ProvidersRepository;
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;

    
/**
     * Constructor for the class.
     *
     * @param ProvidersRepository $providers_repository The repository instance for managing providers.
     */
    
public function __constructProvidersRepository $providers_repository ) {
        
$this->providers_repository $providers_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'wp_smtp' ], 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_provider $this->providers_repository->get_active_provider();

        if ( ! 
is_object$active_provider ) ) {
            
// if there is no active provider, or the active provider use SMTP, then dont inject anything.
            
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';

            
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
            
$phpmailer = new SolidMailertrue );
            
$phpmailer->set_connector$active_provider );
        }

        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 SolidMailer $phpmailer The PHPMailer instance.
     *
     * @return void
     */
    
public function wp_smtp$phpmailer ) {
        
$default_provider $this->providers_repository->get_active_provider();
        
// make sure the provider data right.
        
if ( is_object$default_provider ) && $default_provider->validation() === true ) {
            
// now bind the SMTP info to wp phpmailer.
            
$phpmailer->Mailer   'smtp';
            
$phpmailer->From     $default_provider->get_from_email();
            
$phpmailer->FromName $default_provider->get_from_name();
            
$phpmailer->Sender   $phpmailer->From;
            
$phpmailer->AddReplyTo$phpmailer->From$phpmailer->FromName );
            
$phpmailer->Host       $default_provider->get_host();
            
$phpmailer->SMTPSecure $default_provider->get_secure();
            
$phpmailer->Port       $default_provider->get_port();
            
$phpmailer->SMTPAuth   $default_provider->is_authentication();

            if ( 
$phpmailer->SMTPAuth ) {
                
$phpmailer->Username $default_provider->get_username();
                
$phpmailer->Password $default_provider->get_password();
            }

            if ( 
$default_provider->isAPI() ) {
                
// set this as API sender.
                
$phpmailer->isAPI();
            }
        }
    }
}