forked from Automattic/vip-go-mu-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvip-mail.php
131 lines (106 loc) · 3.32 KB
/
vip-mail.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
126
127
128
129
130
131
<?php
/*
Plugin Name: VIP Mail
Description: Routes mail via Automattic mail servers
Author: Automattic
Version: 1.0
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
class VIP_Noop_Mailer {
function __construct( $phpmailer ) {
$this->subject = $phpmailer->Subject ?? '[No Subject]';
$this->recipients = implode( ', ', array_keys( $phpmailer->getAllRecipientAddresses() ) );
}
function send() {
trigger_error( sprintf( '%s: skipped sending email with subject `%s` to %s', __METHOD__, $this->subject, $this->recipients ), E_USER_NOTICE );
}
}
class VIP_SMTP {
function init() {
add_action( 'phpmailer_init', array( $this, 'phpmailer_init' ) );
add_action( 'bp_phpmailer_init', array( $this, 'phpmailer_init' ) );
add_filter( 'wp_mail_from', array( $this, 'filter_wp_mail_from' ), 1 );
}
function phpmailer_init( &$phpmailer ) {
if ( defined( 'VIP_BLOCK_WP_MAIL' ) && true === VIP_BLOCK_WP_MAIL ) {
$phpmailer = new VIP_Noop_Mailer( $phpmailer );
return;
}
global $all_smtp_servers;
if ( ! is_array( $all_smtp_servers ) || empty( $all_smtp_servers ) ) {
return;
}
if ( count( $all_smtp_servers ) > 1 ) {
shuffle( $all_smtp_servers );
}
$phpmailer->isSMTP();
$phpmailer->Host = current( $all_smtp_servers );
$tracking_header = $this->get_tracking_header( WPCOM_VIP_MAIL_TRACKING_KEY );
if ( false !== $tracking_header ) {
$phpmailer->AddCustomHeader( $tracking_header );
}
}
public function filter_wp_mail_from( $from ) {
return '[email protected]';
}
protected function get_tracking_header( $key ) {
// Don't need an environment check, since this should never trigger locally
if ( false === $key ) {
error_log( sprintf( '%s: Empty tracking header key; check that `WPCOM_VIP_MAIL_TRACKING_KEY` is correctly defined.', __METHOD__ ) );
return false;
}
$caller = $this->get_mail_caller();
$server_name = php_uname( 'n' );
$secret_data = [ $caller, FILES_CLIENT_SITE_ID, $server_name ];
$raw_data = implode( '|', $secret_data );
$iv = openssl_random_pseudo_bytes( openssl_cipher_iv_length( 'AES-256-CBC' ) );
$encrypted_caller_and_data = sprintf(
'%s.%s',
base64_encode( $iv ),
openssl_encrypt( $raw_data, 'AES-256-CBC', base64_decode( $key ), 0, $iv )
);
$project_id = 1; // Specific to VIP Go
$site_id = get_current_network_id();
$blog_id = get_current_blog_id();
$post_id = get_the_ID();
$user_id = get_current_user_id();
return sprintf(
'X-Automattic-Tracking: %d:%d:%s:%d:%d:%d',
$project_id,
$site_id,
$encrypted_caller_and_data,
$blog_id,
$post_id,
$user_id
);
}
/**
* Track down which function/method triggered the email.
*/
protected function get_mail_caller() {
$caller = 'unknown';
$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS );
foreach ( $trace as $index => $call ) {
$skip_functions = [
'do_action',
'apply_filters',
'do_action_ref_array',
'wp_mail',
];
if ( in_array( $call['function'], $skip_functions, true ) ) {
continue;
}
if ( isset( $call['class'] ) ) {
if ( 'VIP_SMTP' === $call['class'] ) {
continue;
}
$caller = sprintf( '%s%s%s', $call['class'], $call['type'] ?? '->', $call['function'] );
break;
}
$caller = $call['function'];
break;
}
return $caller;
}
}
( new VIP_SMTP() )->init();