Skip to content

Commit

Permalink
feat: SMTP 2/2 - Implementation using PHPMailer
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ammopt committed Jun 12, 2024
1 parent 638f2f8 commit e83de75
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
10 changes: 9 additions & 1 deletion code/web/sys/Email/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
43 changes: 43 additions & 0 deletions code/web/sys/Email/SMTPSetting.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

class SMTPSetting extends DataObject {
public $__table = 'smtp_settings';
Expand Down Expand Up @@ -81,6 +84,46 @@ public static function getObjectStructure($context = ''): array {
];
}

function sendEmail($to, $replyTo, $subject, $body, $htmlBody, $attachments){

require_once ('PHPMailer-6.9.1/src/PHPMailer.php');
require_once ('PHPMailer-6.9.1/src/SMTP.php');
require_once ('PHPMailer-6.9.1/src/Exception.php');

$mail = new PHPMailer();

$mail->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';
}
Expand Down

0 comments on commit e83de75

Please sign in to comment.