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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
<?php
namespace SolidWP\Mail;
use PHPMailer\PHPMailer\Exception; use PHPMailer\PHPMailer\PHPMailer; use SolidWP\Mail\Connectors\ConnectorSMTP; use WP_Error;
/** * Extension of PHPMailer that adds API mailing capabilities. * * This class extends the base PHPMailer functionality to support sending emails * through API endpoints in addition to traditional SMTP methods. It provides * methods for configuring API connections and formatting email data for API * transmission. * * @since 2.1.3 * @package SolidWP\Mail\Pro * @extends PHPMailer */ class SolidMailer extends PHPMailer {
/** * SMTP connector instance. * * @since 2.1.3 * @var ConnectorSMTP */ protected ConnectorSMTP $connector;
/** * Sets the SMTP connector instance. * * @param ConnectorSMTP $connector The connector instance to use for email transmission. * * @since 2.1.3 * @return void */ public function set_connector( ConnectorSMTP $connector ): void { $this->connector = $connector; }
/** * Sets the mailer type to API. * * @since 2.1.3 * @return void */ public function isAPI() { $this->Mailer = 'api'; }
/** * Sends an email using the API connector. * * @param string $header The email headers * @param string $body The email body * * @since 2.1.3 * @return bool | WP_Error The result from the API send operation * @throws \Exception */ public function apiSend( $header, $body ) { $email_data = $this->getEmailData( $header, $body ); if ( is_wp_error( $email_data ) ) { throw new Exception( $email_data->get_error_message(), self::STOP_CRITICAL ); }
$result = $this->connector->send_use_api( $email_data ); if ( is_wp_error( $result ) ) { throw new Exception( $result->get_error_message(), self::STOP_CRITICAL ); }
return $result; }
/** * Gets formatted email data including recipients, headers, and body content. * This method extracts and organizes all relevant email sending information. * * @param string $header The email headers. * @param string $body The email body content. * * @since 2.1.3 * * @return WP_Error | array{ * to: array<array{0: string, 1: string}>, * cc: array<array{0: string, 1: string}>, * bcc: array<array{0: string, 1: string}>, * from: string, * sender: string, * subject: string, * headers: string, * body: string, * custom_headers: array<string, string>, * reply_to: array<array{0: string, 1: string}>, * all_recipients: array<string> * } */ protected function getEmailData( string $header, string $body ): array { // Format header with proper line endings $formatted_header = static::stripTrailingWSP( $header ) . static::$LE . static::$LE; // Determine sender $sender = '' === $this->Sender ? $this->From : $this->Sender;
// Get all custom headers $custom_headers = []; foreach ( $this->CustomHeader as $header ) { $custom_headers[ $header[0] ] = $header[1]; }
// Collect all recipients $all_recipients = array_merge( array_column( $this->to, 0 ), array_column( $this->cc, 0 ), array_column( $this->bcc, 0 ) );
$email_data = [ // phpcs:ignore Squiz.PHP.CommentedOutCode.Found // Recipients with format: [[email, name], [email, name], ...] 'to' => $this->to, 'cc' => $this->cc, 'bcc' => $this->bcc,
// Sender information 'from' => $this->From, 'sender' => $sender,
// Content 'subject' => $this->Subject, 'headers' => $formatted_header, 'body' => $body, 'raw_body' => $this->encodeString( $this->Body, $this->Encoding ),
// Additional data 'custom_headers' => $custom_headers, 'reply_to' => $this->ReplyTo, 'all_recipients' => array_unique( $all_recipients ),
// Optional metadata if available 'message_type' => $this->ContentType ?? 'text/plain', 'charset' => $this->CharSet ?? 'utf-8', 'encoding' => $this->Encoding ?? '8bit',
// Attachments 'attachments' => $this->attachment, ];
// Validate required data if ( empty( $email_data['from'] ) ) { return new WP_Error( 'email_missing_from', 'From address is required', [ 'status' => self::STOP_CRITICAL ] ); }
if ( empty( $email_data['all_recipients'] ) ) { return new WP_Error( 'email_missing_recipients', 'At least one recipient is required', [ 'status' => self::STOP_CRITICAL ] ); }
return $email_data; } }
|