From e83de75a9298576b1cd6ee59b45e169f43b2d353 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Fri, 7 Jun 2024 15:30:57 +0000 Subject: [PATCH] feat: SMTP 2/2 - Implementation using PHPMailer Before test, make sure the system variable 'Ticket Email Address' is set. 1) Add a new SMTP setting entry, visit: http://localhost:8083/Admin/SMTPSettings Make sure to use valid SMTP creds/info 2) Submit a new ticket, visit: http://localhost:8083/Admin/SubmitTicket Make sure the 'Email' is your e-mail address. After submiting the ticket, check your inbox for the ticket e-mail. --- code/web/sys/Email/Mailer.php | 10 ++++++- code/web/sys/Email/SMTPSetting.php | 43 ++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/code/web/sys/Email/Mailer.php b/code/web/sys/Email/Mailer.php index 97d9dc8c2b..e4fbd2e53b 100644 --- a/code/web/sys/Email/Mailer.php +++ b/code/web/sys/Email/Mailer.php @@ -20,11 +20,15 @@ public function send($to, $subject, $body, $replyTo = null, $htmlBody = null, $a require_once ROOT_DIR . '/sys/Email/SendGridSetting.php'; require_once ROOT_DIR . '/sys/Email/AmazonSesSetting.php'; + require_once ROOT_DIR . '/sys/Email/SMTPSetting.php'; require_once ROOT_DIR . '/sys/CurlWrapper.php'; //TODO: Do validation of the address $amazonSesSettings = new AmazonSesSetting(); + $smtpServerSettings = new SMTPSetting(); - if ($amazonSesSettings->find(true)) { + if($smtpServerSettings->find(true)) { + $result = $this->sendViaSMTP($smtpServerSettings, $to, $replyTo, $subject, $body, $htmlBody, $attachments); + }elseif ($amazonSesSettings->find(true)) { $result = $this->sendViaAmazonSes($amazonSesSettings, $to, $replyTo, $subject, $body, $htmlBody, $attachments); } else { $sendGridSettings = new SendGridSetting(); @@ -137,4 +141,8 @@ private function sendViaAmazonSes(AmazonSesSetting $amazonSesSettings, string $t } } } + + private function sendViaSMTP(SMTPSetting $smtpSettings, string $to, ?string $replyTo, string $subject, ?string $body, ?string $htmlBody, ?array $attachments): bool { + return $smtpSettings->sendEmail($to, $replyTo, $subject, $body, $htmlBody, $attachments); + } } \ No newline at end of file diff --git a/code/web/sys/Email/SMTPSetting.php b/code/web/sys/Email/SMTPSetting.php index 930f9aed2f..f9b53a81c7 100644 --- a/code/web/sys/Email/SMTPSetting.php +++ b/code/web/sys/Email/SMTPSetting.php @@ -1,5 +1,8 @@ isSMTP(); + // $mail->SMTPDebug = SMTP::DEBUG_SERVER; + $mail->Host = $this->host; + $mail->SMTPAuth = true; + $mail->Username = $this->user_name; + $mail->Password = $this->password; + + if($this->ssl_mode != 'disabled'){ + $mail->SMTPSecure = $this->ssl_mode; + } + + $mail->From = $this->from_address; + $mail->FromName = 'Aspen Discovery'; + $mail->addAddress($to); + + for($i = 0; $i < sizeof($attachments['name']); $i++){ + $mail->addAttachment($attachments['tmp_name'][$i], $attachments['name'][0]); + } + + $mail->Subject = $subject; + $mail->Body = $htmlBody ?: $body; + + if(!$mail->send()) { + echo 'Message could not be sent.'; + echo 'Mailer Error: ' . $mail->ErrorInfo; + return false; + } else { + echo 'Message has been sent'; + return true; + } + } + function getActiveAdminSection(): string { return 'system_admin'; }