PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.3
Yatra – Travel Booking & Tour Operator Software v3.0.3
3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 2.0.11 All 82 releases
yatra / app / Services / EmailService.php

EmailService.php in Yatra – Travel Booking & Tour Operator Software 3.0.3, at app/Services/EmailService.php

222 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Email Service
4 *
5 * Handles email sending with SMTP support
6 *
7 * @package Yatra\Services
8 * @since 3.0.0
9 */
10
11 declare(strict_types=1);
12
13 namespace Yatra\Services;
14
15 use PHPMailer\PHPMailer\PHPMailer;
16 use PHPMailer\PHPMailer\Exception;
17
18 class EmailService
19 {
20 /**
21 * Send email using WordPress wp_mail or SMTP
22 *
23 * @param string|array $to Recipient email address(es)
24 * @param string $subject Email subject
25 * @param string $message Email message
26 * @param array $headers Optional headers
27 * @param array $attachments Optional attachments
28 * @return bool True on success, false on failure
29 */
30 public static function send($to, string $subject, string $message, array $headers = [], array $attachments = []): bool
31 {
32 // Check if SMTP is enabled
33 $smtp_enabled = SettingsService::isEnabled('smtp_enabled');
34
35 if ($smtp_enabled) {
36 return self::sendViaSMTP($to, $subject, $message, $headers, $attachments);
37 }
38
39 // Use default WordPress wp_mail
40 return self::sendViaWpMail($to, $subject, $message, $headers, $attachments);
41 }
42
43 /**
44 * Send email via WordPress wp_mail with custom from address
45 */
46 private static function sendViaWpMail($to, string $subject, string $message, array $headers, array $attachments): bool
47 {
48 // Add custom from address if configured
49 add_filter('wp_mail_from', [self::class, 'customFromEmail']);
50 add_filter('wp_mail_from_name', [self::class, 'customFromName']);
51
52 $result = wp_mail($to, $subject, $message, $headers, $attachments);
53
54 // Remove filters after sending
55 remove_filter('wp_mail_from', [self::class, 'customFromEmail']);
56 remove_filter('wp_mail_from_name', [self::class, 'customFromName']);
57
58 return $result;
59 }
60
61 /**
62 * Send email via SMTP
63 */
64 private static function sendViaSMTP($to, string $subject, string $message, array $headers, array $attachments): bool
65 {
66 // Get SMTP settings
67 $smtp_host = SettingsService::getString('smtp_host', 'smtp.gmail.com');
68 $smtp_port = SettingsService::getInt('smtp_port', 587);
69 $smtp_username = SettingsService::getString('smtp_username', '');
70 $smtp_password = SettingsService::getString('smtp_password', '');
71 $smtp_encryption = SettingsService::getString('smtp_encryption', 'tls');
72 $from_email = SettingsService::getString('from_email', get_option('admin_email'));
73 $from_name = SettingsService::getString('from_name', get_bloginfo('name'));
74
75 // Validate required settings
76 if (empty($smtp_host) || empty($smtp_username) || empty($smtp_password)) {
77 return false;
78 }
79
80 try {
81 $mail = new PHPMailer(true);
82
83 // Server settings
84 $mail->isSMTP();
85 $mail->Host = $smtp_host;
86 $mail->SMTPAuth = true;
87 $mail->Username = $smtp_username;
88 $mail->Password = $smtp_password;
89 self::configureSmtpEncryption($mail, $smtp_encryption);
90 $mail->Port = $smtp_port;
91
92 // Recipients
93 $mail->setFrom($from_email, $from_name);
94
95 if (is_array($to)) {
96 foreach ($to as $recipient) {
97 $mail->addAddress($recipient);
98 }
99 } else {
100 $mail->addAddress($to);
101 }
102
103 // Process headers
104 foreach ($headers as $header) {
105 if (stripos($header, 'content-type:') === 0) {
106 if (stripos($header, 'text/html') !== false) {
107 $mail->isHTML(true);
108 }
109 } elseif (stripos($header, 'cc:') === 0) {
110 $cc = trim(substr($header, 3));
111 $mail->addCC($cc);
112 } elseif (stripos($header, 'bcc:') === 0) {
113 $bcc = trim(substr($header, 4));
114 $mail->addBCC($bcc);
115 } elseif (stripos($header, 'reply-to:') === 0) {
116 $reply_to = trim(substr($header, 9));
117 $mail->addReplyTo($reply_to);
118 }
119 }
120
121 // Content
122 $mail->Subject = $subject;
123 $mail->Body = $message;
124
125 // Attachments
126 foreach ($attachments as $attachment) {
127 $mail->addAttachment($attachment);
128 }
129
130 $mail->send();
131 return true;
132
133 } catch (Exception $e) {
134 return false;
135 }
136 }
137
138 private static function configureSmtpEncryption(PHPMailer $mail, string $smtp_encryption): void
139 {
140 if ($smtp_encryption === 'ssl') {
141 $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
142 $mail->SMTPAutoTLS = true;
143
144 return;
145 }
146 if ($smtp_encryption === 'tls') {
147 $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
148 $mail->SMTPAutoTLS = true;
149
150 return;
151 }
152 $mail->SMTPSecure = '';
153 $mail->SMTPAutoTLS = false;
154 }
155
156 /**
157 * Custom from email filter
158 */
159 public static function customFromEmail($email)
160 {
161 $from_email = SettingsService::getString('from_email', '');
162 return !empty($from_email) ? $from_email : $email;
163 }
164
165 /**
166 * Custom from name filter
167 */
168 public static function customFromName($name)
169 {
170 $from_name = SettingsService::getString('from_name', '');
171 return !empty($from_name) ? $from_name : $name;
172 }
173
174 /**
175 * Test SMTP connection
176 *
177 * @return array Result with success status and message
178 */
179 public static function testSMTPConnection(): array
180 {
181 $smtp_host = SettingsService::getString('smtp_host', '');
182 $smtp_port = SettingsService::getInt('smtp_port', 587);
183 $smtp_username = SettingsService::getString('smtp_username', '');
184 $smtp_password = SettingsService::getString('smtp_password', '');
185 $smtp_encryption = SettingsService::getString('smtp_encryption', 'tls');
186
187 if (empty($smtp_host) || empty($smtp_username) || empty($smtp_password)) {
188 return [
189 'success' => false,
190 'message' => __('Please configure all SMTP settings first.', 'yatra')
191 ];
192 }
193
194 try {
195 $mail = new PHPMailer(true);
196 $mail->isSMTP();
197 $mail->Host = $smtp_host;
198 $mail->SMTPAuth = true;
199 $mail->Username = $smtp_username;
200 $mail->Password = $smtp_password;
201 self::configureSmtpEncryption($mail, $smtp_encryption);
202 $mail->Port = $smtp_port;
203 $mail->Timeout = 10;
204
205 // Test connection
206 $mail->smtpConnect();
207 $mail->smtpClose();
208
209 return [
210 'success' => true,
211 'message' => __('SMTP connection successful!', 'yatra')
212 ];
213
214 } catch (Exception $e) {
215 return [
216 'success' => false,
217 'message' => sprintf(__('SMTP connection failed: %s', 'yatra'), $e->getMessage())
218 ];
219 }
220 }
221 }
222