PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.2.7
Yatra – Travel Booking & Tour Operator Software v3.0.2.7
3.0.15 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 All 83 releases
yatra / app / Services / EmailService.php

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

224 lines 7.4 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 error_log('Yatra SMTP: Missing required SMTP configuration');
78 return false;
79 }
80
81 try {
82 $mail = new PHPMailer(true);
83
84 // Server settings
85 $mail->isSMTP();
86 $mail->Host = $smtp_host;
87 $mail->SMTPAuth = true;
88 $mail->Username = $smtp_username;
89 $mail->Password = $smtp_password;
90 self::configureSmtpEncryption($mail, $smtp_encryption);
91 $mail->Port = $smtp_port;
92
93 // Recipients
94 $mail->setFrom($from_email, $from_name);
95
96 if (is_array($to)) {
97 foreach ($to as $recipient) {
98 $mail->addAddress($recipient);
99 }
100 } else {
101 $mail->addAddress($to);
102 }
103
104 // Process headers
105 foreach ($headers as $header) {
106 if (stripos($header, 'content-type:') === 0) {
107 if (stripos($header, 'text/html') !== false) {
108 $mail->isHTML(true);
109 }
110 } elseif (stripos($header, 'cc:') === 0) {
111 $cc = trim(substr($header, 3));
112 $mail->addCC($cc);
113 } elseif (stripos($header, 'bcc:') === 0) {
114 $bcc = trim(substr($header, 4));
115 $mail->addBCC($bcc);
116 } elseif (stripos($header, 'reply-to:') === 0) {
117 $reply_to = trim(substr($header, 9));
118 $mail->addReplyTo($reply_to);
119 }
120 }
121
122 // Content
123 $mail->Subject = $subject;
124 $mail->Body = $message;
125
126 // Attachments
127 foreach ($attachments as $attachment) {
128 $mail->addAttachment($attachment);
129 }
130
131 $mail->send();
132 return true;
133
134 } catch (Exception $e) {
135 error_log('Yatra SMTP Error: ' . $mail->ErrorInfo);
136 return false;
137 }
138 }
139
140 private static function configureSmtpEncryption(PHPMailer $mail, string $smtp_encryption): void
141 {
142 if ($smtp_encryption === 'ssl') {
143 $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
144 $mail->SMTPAutoTLS = true;
145
146 return;
147 }
148 if ($smtp_encryption === 'tls') {
149 $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
150 $mail->SMTPAutoTLS = true;
151
152 return;
153 }
154 $mail->SMTPSecure = '';
155 $mail->SMTPAutoTLS = false;
156 }
157
158 /**
159 * Custom from email filter
160 */
161 public static function customFromEmail($email)
162 {
163 $from_email = SettingsService::getString('from_email', '');
164 return !empty($from_email) ? $from_email : $email;
165 }
166
167 /**
168 * Custom from name filter
169 */
170 public static function customFromName($name)
171 {
172 $from_name = SettingsService::getString('from_name', '');
173 return !empty($from_name) ? $from_name : $name;
174 }
175
176 /**
177 * Test SMTP connection
178 *
179 * @return array Result with success status and message
180 */
181 public static function testSMTPConnection(): array
182 {
183 $smtp_host = SettingsService::getString('smtp_host', '');
184 $smtp_port = SettingsService::getInt('smtp_port', 587);
185 $smtp_username = SettingsService::getString('smtp_username', '');
186 $smtp_password = SettingsService::getString('smtp_password', '');
187 $smtp_encryption = SettingsService::getString('smtp_encryption', 'tls');
188
189 if (empty($smtp_host) || empty($smtp_username) || empty($smtp_password)) {
190 return [
191 'success' => false,
192 'message' => __('Please configure all SMTP settings first.', 'yatra')
193 ];
194 }
195
196 try {
197 $mail = new PHPMailer(true);
198 $mail->isSMTP();
199 $mail->Host = $smtp_host;
200 $mail->SMTPAuth = true;
201 $mail->Username = $smtp_username;
202 $mail->Password = $smtp_password;
203 self::configureSmtpEncryption($mail, $smtp_encryption);
204 $mail->Port = $smtp_port;
205 $mail->Timeout = 10;
206
207 // Test connection
208 $mail->smtpConnect();
209 $mail->smtpClose();
210
211 return [
212 'success' => true,
213 'message' => __('SMTP connection successful!', 'yatra')
214 ];
215
216 } catch (Exception $e) {
217 return [
218 'success' => false,
219 'message' => sprintf(__('SMTP connection failed: %s', 'yatra'), $e->getMessage())
220 ];
221 }
222 }
223 }
224