This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternal-smtp.php
105 lines (84 loc) · 1.95 KB
/
external-smtp.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
<?php
namespace WPLib_Box;
use PHPMailer;
class External_Smtp {
/**
* @var string
*/
static private $_external_host;
/**
* @var bool|callable
*/
static private $_switch_criteria;
static function on_load() {
add_action( 'phpmailer_init', [ __CLASS__, '_phpmailer_init' ] );
}
/**
* @param string $host
*/
static function set_external_host( $host ) {
self::$_external_host = $host;
}
/**
* @param callable $switch_criteria
*/
static function set_switch_criteria( $switch_criteria ) {
self::$_switch_criteria = $switch_criteria;
}
/**
* @param PHPMailer $phpmailer
*/
static function _phpmailer_init( $phpmailer ) {
do {
$use_external = true;
$headers = $phpmailer->getCustomHeaders();
if ( isset( $headers[ 'X-External-Host' ] ) ) {
/**
* First look for a custom header and use it if found
*/
self::$_external_host = $headers[ 'X-External-Host' ];
/**
* If set we know we want to use external host.
*/
break;
}
if ( ! isset( self::$_external_host ) ) {
/**
* If no external host we can't send via external host
*/
break;
}
if ( ! isset( self::$_switch_criteria ) ) {
/**
* If switch criteria evaluates to false, we should not use external host
*/
break;
}
if ( ! self::$_switch_criteria ) {
/**
* Look to see if it wasn't set to some non-false value.
*/
break;
} elseif ( ! is_callable( self::$_switch_criteria ) ) {
/**
* Now look to specifically to see if ::$_switch_criteria() can be called
*/
break;
}
/**
* @param PHPMailer $phpmailer
*/
if ( ! call_user_func( self::$_switch_criteria, $phpmailer ) ) {
/**
* It can be called. Leave $use_external === true
*/
break;
}
$use_external = false;
} while ( false );
if ( $use_external && isset( self::$_external_host ) ) {
$phpmailer->Host = self::$_external_host;
}
}
}
External_Smtp::on_load();