PluginProbe ʕ •ᴥ•ʔ
Really Simple Security – Simple and Performant Security (formerly Really Simple SSL) / 9.4.0
Really Simple Security – Simple and Performant Security (formerly Really Simple SSL) v9.4.0
9.5.11 9.5.10.1 9.5.10 trunk 9.4.0 9.4.1 9.4.2 9.4.3 9.5.0 9.5.0.1 9.5.0.2 9.5.1 9.5.2 9.5.2.2 9.5.2.3 9.5.3 9.5.3.1 9.5.3.2 9.5.4 9.5.5 9.5.6 9.5.7 9.5.8 9.5.9
really-simple-ssl / mailer / class-mail.php
really-simple-ssl / mailer Last commit date
templates 1 year ago class-mail-admin.php 1 year ago class-mail.php 1 year ago index.php 2 years ago
class-mail.php
224 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5
6 /**
7 * Class to send an e-mail
8 */
9
10 if ( ! class_exists( 'rsssl_mailer' ) ) {
11 class rsssl_mailer {
12
13 public $to;
14 public $title;
15 public $headers;
16 public $message;
17 public $branded = true;
18 public $subject;
19 public $button_text;
20 public $change_text;
21 public $sent_to_text;
22 public $what_now_text;
23 public $sent_by_text;
24 public $warning_blocks;
25 public $error = '';
26 public $template_filename;
27 public $block_template_filename;
28
29 public function __construct() {
30
31 $this->sent_by_text = __( "This email is part of the Really Simple Security Notification System", "really-simple-ssl" );
32 $this->subject = __( "Notification by Really Simple Security", "really-simple-ssl" );
33 $this->button_text = __( "Learn more", "really-simple-ssl" );
34 $this->to = rsssl_get_option( 'notifications_email_address', get_bloginfo( 'admin_email' ) );
35 $this->title = __( "Learn more about our features!", "really-simple-ssl" );
36 $this->sent_to_text = __( "This email was sent to", "really-simple-ssl" );
37 $this->what_now_text = __( "Learn more", "really-simple-ssl" );
38 $this->change_text = __( "Why did I receive this email?", "really-simple-ssl" );
39
40 $domain = '<a href="' . site_url() . '">' . site_url() . '</a>';
41 $this->message = sprintf( __( "You have enabled a feature on %s. We think it's important to let you know a little bit more about this feature so you can use it without worries.", "really-simple-ssl" ), $domain );
42
43 add_action( 'wp_mail_failed', array( $this, 'log_mailer_errors' ), 10, 1 );
44
45 }
46
47 /**
48 * Send a test email
49 * @return array
50 */
51 public function send_test_mail() {
52 if ( ! rsssl_user_can_manage() ) {
53 return [ 'success' => false, 'message' => 'Not allowed' ];
54 }
55
56 if ( ! is_email( $this->to ) ) {
57 return [
58 'success' => false,
59 'title' => __( "Test notification email error", 'really-simple-ssl' ),
60 'message' => __( 'Email address not valid', "really-simple-ssl" ),
61 ];
62 }
63 $this->title = __( "Really Simple Security - Notification Test", "really-simple-ssl" );
64 $this->message = __( "This email is confirmation that any security notices are likely to reach your inbox.", "really-simple-ssl" );
65 $this->warning_blocks = [
66 [
67 'title' => __( "About notifications", "really-simple-ssl" ),
68 'message' => __( "Email notifications are only sent for important updates, security notices or when certain features are enabled.", "really-simple-ssl" ),
69 'url' => rsssl_link('email-notifications/'),
70 ]
71 ];
72
73 return $this->send_mail( true );
74 }
75
76 public function send_verification_mail() {
77 if ( ! rsssl_user_can_manage() ) {
78 return [
79 'success' => false,
80 'message' => 'Not allowed',
81 'title' => __( "Email verification error", 'really-simple-ssl' ),
82 ];
83 }
84
85 $verification_code = str_pad( rand( 0, 999999 ), 6, '0', STR_PAD_LEFT );
86 $verification_expiration = strtotime( "+15 minutes" );
87
88 // Delete existing option
89 delete_option( 'rsssl_email_verification_code' );
90
91 update_option( 'rsssl_email_verification_code', $verification_code, false );
92 update_option( 'rsssl_email_verification_code_expiration', $verification_expiration, false );
93 update_option( 'rsssl_email_verification_status', 'started', false );
94
95 if ( ! is_email( $this->to ) ) {
96 return [
97 'success' => false,
98 'title' => __( "Email verification error", 'really-simple-ssl' ),
99 'message' => __( 'Email address not valid', "really-simple-ssl" )
100 ];
101 }
102
103 $user_id = get_current_user_id();
104
105 $verification_url = add_query_arg(
106 array(
107 'page' => 'really-simple-security',
108 'rsssl_nonce' => wp_create_nonce( 'rsssl_email_verification_' . $user_id ),
109 'rsssl_verification_code' => $verification_code,
110 'verified_email' => '1',
111 ),
112 rsssl_admin_url([], '#settings/general')
113 );
114
115 $this->subject = __( "Really Simple Security - Verify your email address", "really-simple-ssl" );
116 $this->title = __( "Please verify your email", "really-simple-ssl" );
117 $this->message = __('To use certain features in Really Simple Security we need to confirm emails are delivered without issues.', 'really-simple-ssl');
118 $this->button_text = __( "Verify email", "really-simple-ssl" );
119 $this->warning_blocks[] = [
120 'title' => '',
121 'message' => sprintf( __( "Click the button below to confirm your email address, or copy the following URL: %s", "really-simple-ssl" ), '{url}' ),
122 'url' => $verification_url,
123 ];
124
125 return $this->send_mail();
126 }
127
128 public function log_mailer_errors( $wp_error ) {
129 if ( is_wp_error( $wp_error ) ) {
130 $this->error = $wp_error->get_error_message();
131 }
132 }
133
134 /**
135 * Send an e-mail with the correct login URL
136 *
137 * @return array
138 */
139 public function send_mail(): array {
140 if ( empty( $this->message ) || empty( $this->subject ) ) {
141 $this->error = __( "Email could not be sent. No message or subject set.", "really-simple-ssl" );
142 }
143
144 if ( ! is_email( $this->to ) ) {
145 $this->error = __( "Email address not valid", "really-simple-ssl" );
146 }
147 $block_template = $this->branded ? rsssl_path . '/mailer/templates/block.html' : rsssl_path . '/mailer/templates/block-unbranded.html';
148 $email_template = $this->branded ? rsssl_path . '/mailer/templates/email.html' : rsssl_path . '/mailer/templates/email-unbranded.html';
149 $this->block_template_filename = apply_filters( 'rsssl_email_block_template', $block_template );
150 $this->template_filename = apply_filters( 'rsssl_email_template', $email_template );
151
152 $template = file_get_contents( $this->template_filename );
153 $block_html = '';
154 if ( is_array( $this->warning_blocks ) && count( $this->warning_blocks ) > 0 ) {
155 $block_template = file_get_contents( $this->block_template_filename );
156 foreach ( $this->warning_blocks as $warning_block ) {
157 $block_html .= str_replace(
158 [ '{title}', '{message}', '{url}' ],
159 [
160 sanitize_text_field( $warning_block['title'] ),
161 wp_kses_post( $warning_block['message'] ),
162 esc_url_raw( $warning_block['url'] )
163 ],
164 $block_template );
165 }
166 }
167 $username = rsssl_get_option( 'new_admin_user_login' );
168 $login_url = ! empty( rsssl_get_option( 'change_login_url' ) )
169 ? trailingslashit( site_url() ) . rsssl_get_option( 'change_login_url' )
170 : wp_login_url();
171 $body = str_replace(
172 [
173 '{title}',
174 '{message}',
175 '{warnings}',
176 '{email-address}',
177 '{learn-more}',
178 '{site_url}',
179 '{login_url}',
180 '{username}',
181 '{change_text}',
182 '{what_now}',
183 '{sent_to_text}',
184 '{sent_by_text}',
185 ],
186 [
187 sanitize_text_field( $this->title ),
188 wp_kses_post( $this->message ),
189 $block_html,
190 $this->to,
191 $this->button_text,
192 site_url(),
193 $login_url,
194 $username,
195 $this->change_text,
196 $this->what_now_text,
197 $this->sent_to_text,
198 $this->sent_by_text,
199 ], $template );
200 $success = wp_mail( $this->to, sanitize_text_field( $this->subject ), $body, array( 'Content-Type: text/html; charset=UTF-8' ) );
201 if ( $success ) {
202 return [
203 'success' => true,
204 'title' => __( "Email validation", 'really-simple-ssl' ),
205 'message' => __( 'Email sent! Please check your mail', "really-simple-ssl" )
206 ];
207 }
208
209 if ( empty( $this->error ) ) {
210 $this->error = __( 'Email could not be sent.', "really-simple-ssl" );
211 } else {
212 $this->error = __( 'An error occurred:', "really-simple-ssl" ) . '<br>' . $this->error;
213 }
214
215 return [
216 'success' => false,
217 'title' => __( "Email notification error", 'really-simple-ssl' ),
218 'message' => $this->error
219 ];
220 }
221
222 }
223 }
224