fix: Update patient_tracker.php (#6595)
[openemr.git] / library / classes / postmaster.php
blobc9a708c63c6590a8d874ad7a2d8e4e4c448d12af
1 <?php
3 /**
4 * MyMailer class
6 * @package OpenEMR
7 * @link https://www.open-emr.org
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2010 Open Support LLC
10 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
14 use OpenEMR\Common\Crypto\CryptoGen;
15 use PHPMailer\PHPMailer\PHPMailer;
17 class MyMailer extends PHPMailer
19 var $Mailer;
20 var $SMTPAuth;
21 var $Host;
22 var $Username;
23 var $Password;
24 var $Port;
25 var $CharSet;
27 function __construct($throwExceptions = false)
29 // make sure we initiate our constructor here...
30 parent::__construct($throwExceptions);
32 $this->emailMethod();
35 /**
36 * Checks if the MyMailer service is configured for mail with all of the host parameters defined
37 * @return bool
39 public static function isConfigured()
41 switch ($GLOBALS['EMAIL_METHOD']) {
42 case "SMTP":
43 $requiredKeys = ['SMTP_HOST', 'SMTP_PORT', 'SMTP_SECURE'];
44 if ($GLOBALS['SMTP_Auth']) {
45 $requiredKeys[] = 'SMTP_USER';
46 $requiredKeys[] = 'SMTP_PASS';
48 break;
49 default:
50 $requiredKeys = [];
51 break;
54 foreach ($requiredKeys as $key) {
55 if (empty($GLOBALS[$key])) {
56 return false;
59 return true;
62 public static function emailServiceQueue(string $sender, string $recipient, string $subject, string $body): bool
64 if (empty($sender) || empty($recipient) || empty($subject) || empty($body)) {
65 return false;
68 sqlInsert("INSERT into `email_queue` (`sender`, `recipient`, `subject`, `body`, `datetime_queued`) VALUES (?, ?, ?, ?, NOW())", [$sender, $recipient, $subject, $body]);
69 return true;
72 public static function emailServiceRun(): void
74 // collect the queue
75 $res = sqlStatement("SELECT `id`, `sender`, `recipient`, `subject`, `body` FROM `email_queue` WHERE `sent` = 0");
77 // send emails in the queue (to avoid race conditions, sent flag is rechecked before sending the email and then quickly set before proceeding to send the email)
78 // (first ensure the email method is properly configured)
79 $emailMethodConfigured = self::isConfigured();
80 while ($ret = sqlFetchArray($res)) {
81 $sql = sqlQuery("SELECT `sent` FROM `email_queue` WHERE `id` = ?", [$ret['id']]);
82 if ($sql['sent'] == 1) {
83 // Sent, so skip
84 } else {
85 // Not sent, so set the sent flag, and then send the email
86 sqlStatement("UPDATE `email_queue` SET `sent` = 1, `datetime_sent` = NOW() WHERE `id` = ?", [$ret['id']]);
88 if ($emailMethodConfigured) {
89 $mail = new MyMailer();
90 $email_subject = $ret['subject'];
91 $email_sender = $ret['sender'];
92 $email_address = $ret['recipient'];
93 $message = $ret['body'];
94 $mail->AddReplyTo($email_sender, $email_sender);
95 $mail->SetFrom($email_sender, $email_sender);
96 $mail->AddAddress($email_address);
97 $mail->Subject = $email_subject;
98 $mail->MsgHTML("<html><body><div class='wrapper'>" . text($message) . "</div></body></html>");
99 $mail->AltBody = $message;
100 $mail->IsHTML(true);
101 if (!$mail->Send()) {
102 sqlStatement("UPDATE `email_queue` SET `error` = 1, `error_message`= ?, , `datetime_error` = NOW() WHERE `id` = ?", [$mail->ErrorInfo, $ret['id']]);
103 error_log("Failed to send email notification through Mymailer emailServiceRun with error " . errorLogEscape($mail->ErrorInfo));
105 } else {
106 sqlStatement("UPDATE `email_queue` SET `error` = 1, `error_message`= 'email method is not configured correctly', `datetime_error` = NOW() WHERE `id` = ?", [$ret['id']]);
107 error_log("Failed to send email notification through Mymailer since email method is not configured correctly");
113 function emailMethod()
115 global $HTML_CHARSET;
116 $this->CharSet = $HTML_CHARSET;
117 switch ($GLOBALS['EMAIL_METHOD']) {
118 case "PHPMAIL":
119 $this->Mailer = "mail";
120 break;
121 case "SMTP":
122 $this->Mailer = "smtp";
123 $this->SMTPAuth = $GLOBALS['SMTP_Auth'];
124 $this->Host = $GLOBALS['SMTP_HOST'];
125 $this->Username = $GLOBALS['SMTP_USER'];
126 $cryptoGen = new CryptoGen();
127 $this->Password = $cryptoGen->decryptStandard($GLOBALS['SMTP_PASS']);
128 $this->Port = $GLOBALS['SMTP_PORT'];
129 $this->SMTPSecure = $GLOBALS['SMTP_SECURE'];
130 break;
131 case "SENDMAIL":
132 $this->Mailer = "sendmail";
133 break;