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
27 function __construct($throwExceptions = false)
29 // make sure we initiate our constructor here...
30 parent
::__construct($throwExceptions);
36 * Checks if the MyMailer service is configured for mail with all of the host parameters defined
39 public static function isConfigured()
41 switch ($GLOBALS['EMAIL_METHOD']) {
43 $requiredKeys = ['SMTP_HOST', 'SMTP_PORT', 'SMTP_SECURE'];
44 if ($GLOBALS['SMTP_Auth']) {
45 $requiredKeys[] = 'SMTP_USER';
46 $requiredKeys[] = 'SMTP_PASS';
54 foreach ($requiredKeys as $key) {
55 if (empty($GLOBALS[$key])) {
62 public static function emailServiceQueue(string $sender, string $recipient, string $subject, string $body): bool
64 if (empty($sender) ||
empty($recipient) ||
empty($subject) ||
empty($body)) {
68 sqlInsert("INSERT into `email_queue` (`sender`, `recipient`, `subject`, `body`, `datetime_queued`) VALUES (?, ?, ?, ?, NOW())", [$sender, $recipient, $subject, $body]);
72 public static function emailServiceRun(): void
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) {
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;
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
));
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']) {
119 $this->Mailer
= "mail";
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'];
132 $this->Mailer
= "sendmail";