Verified Commit 8c223668 authored by dockx thibault's avatar dockx thibault
Browse files

:sparkles: Feat(Integrator) - First import of maillib from orchestrator

parent 064a51e2
2 merge requests!52:sparkles: Releasing Fusiondirectory Integrator 1.2,!43Resolve "[Integrator] - Create MailLib to be used by orchestrator instead of the mailController."
Pipeline #29374 failed with stages
in 47 seconds
Showing with 117 additions and 0 deletions
+117 -0
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
class MailController
{
protected string $setFrom;
protected ?string $setBCC;
protected array $recipients;
protected string $body;
protected ?string $signature;
protected string $subject;
protected ?string $receipt;
protected ?array $attachments;
private PHPMailer $mail;
function __construct (
string $setFrom,
?string $setBCC,
array $recipients,
string $body,
?string $signature,
string $subject,
string $receipt = NULL, array $attachments = NULL
)
{
// The TRUE value passed it to enable the exception handling properly.
$this->mail = new PHPMailer(TRUE);
$this->setFrom = $setFrom;
$this->setBCC = $setBCC;
$this->recipients = $recipients;
$this->body = $body;
$this->signature = $signature;
$this->subject = $subject;
$this->receipt = $receipt;
$this->attachments = $attachments;
}
public function sendMail (): array
{
// Our returned array
$errors = [];
$this->mail->isSMTP();
$this->mail->Host = $_ENV["MAIL_HOST"];
/*
* In case there are FQDN errors responses by the SMTP server, try below.
* $this->mail->Helo = '['.$_SERVER['SERVER_ADDR'].']';
*/
$this->mail->SMTPAuth = TRUE;
$this->mail->Username = $_ENV["MAIL_USER"];
$this->mail->Password = $_ENV["MAIL_PASS"];
$this->mail->SMTPSecure = $_ENV["MAIL_SEC"];
$this->mail->Port = $_ENV["MAIL_PORT"];
$this->mail->AuthType = 'LOGIN';
if (!empty($this->attachments)) {
foreach ($this->attachments as $attachment) {
$this->mail->addStringAttachment($attachment['content'], $attachment['cn']);
}
}
$this->mail->setFrom($this->setFrom);
if (!empty($this->setBCC)) {
$this->mail->addBCC($this->setBCC);
}
if ($this->receipt === 'TRUE') {
$this->mail->addCustomHeader('Disposition-Notification-To', $this->setFrom);
}
$this->mail->Subject = $this->subject;
$this->mail->Body = $this->body;
if (!empty($this->signature)) {
$this->mail->Body .= "\n\n" . $this->signature;
}
// add it to keep SMTP connection open after each email sent
$this->mail->SMTPKeepAlive = TRUE;
if (!empty($this->recipients["count"])) {
unset($this->recipients["count"]);
}
/* We have an anti-spam logic applied above the mail controller. In case of mail template, only one email is within
the recipient address, in case of notifications (e.g), multiple address exists. Therefore, the counting of anti-spam
increment is applied prior of this controller added by the numbers of recipients. See notifications logic in a send
method.
*/
foreach ($this->recipients as $mail) {
$this->mail->addAddress($mail);
try {
$this->mail->send();
} catch (Exception $e) {
$errors[] = $this->mail->ErrorInfo;
}
$this->mail->clearAddresses();
if (empty($errors)) {
$errors[] = "SUCCESS";
}
}
$this->mail->smtpClose();
return $errors;
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment