.htaccess
1 year ago
Email.php
1 year ago
Notification.php
1 year ago
index.html
1 year ago
web.config
1 year ago
Email.php
64 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JetBackup\Notification; |
| 4 | |
| 5 | use JetBackup\Exception\NotificationException; |
| 6 | use JetBackup\Factory; |
| 7 | use JetBackup\Wordpress\Helper; |
| 8 | use JetBackup\Wordpress\Wordpress; |
| 9 | |
| 10 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 11 | |
| 12 | class Email { |
| 13 | |
| 14 | private function __construct() {} |
| 15 | |
| 16 | /** |
| 17 | * @throws NotificationException |
| 18 | */ |
| 19 | public static function send($recipient, $subject, $message, $attachments=[], $from=null, $headers=[]) { |
| 20 | |
| 21 | |
| 22 | if (!Factory::getSettingsNotifications()->isEmailsEnabled()) return true; // Do nothing if emails are disabled |
| 23 | if (!Helper::validateEmail($recipient)) throw new NotificationException("Email recipient ($recipient) invalid!"); |
| 24 | |
| 25 | |
| 26 | |
| 27 | if(!$from) { |
| 28 | $site_url = parse_url(Wordpress::getBlogInfo('url')); |
| 29 | // Use a fake domain if running on localhost (real domains may be rejected in tests) |
| 30 | $from = 'wordpress@' . ($site_url['host'] === 'localhost' ? 'localhost.local' : $site_url['host']); |
| 31 | } |
| 32 | |
| 33 | if(!$recipient) { |
| 34 | $recipient = Wordpress::getBlogInfo('admin_email'); |
| 35 | } |
| 36 | |
| 37 | if (!Helper::validateEmail($from)) throw new NotificationException("Email from ($from) invalid!"); |
| 38 | |
| 39 | $subject = Wordpress::sanitizeTextField($subject); |
| 40 | |
| 41 | $headers = array_merge($headers, [ |
| 42 | 'MIME-Version: 1.0', |
| 43 | 'Content-Type: text/html; charset=UTF-8', |
| 44 | ]); |
| 45 | |
| 46 | foreach ($headers as $key => $value) { |
| 47 | $headers[$key] = Wordpress::sanitizeTextField($value); |
| 48 | } |
| 49 | |
| 50 | // Ensure $attachments is an array |
| 51 | $attachments = is_array($attachments) ? $attachments : [$attachments]; |
| 52 | |
| 53 | $attachments = array_filter($attachments, function($file) { |
| 54 | $valid = file_exists($file) && is_readable($file); |
| 55 | if (!$valid) throw new NotificationException("Attachment $file is not valid!"); |
| 56 | }); |
| 57 | |
| 58 | try { |
| 59 | return Wordpress::sendMail($recipient, $subject, $message, $headers, $attachments); |
| 60 | } catch (\Exception $e) { |
| 61 | throw new NotificationException($e->getMessage(), $e->getCode()); |
| 62 | } |
| 63 | } |
| 64 | } |