Commit 784d5d69 authored by dockx thibault's avatar dockx thibault
Browse files

Merge branch...

Merge branch '34-integrator-create-maillib-to-be-used-by-orchestrator-instead-of-the-mailcontroller' into 'dev'

Resolve "[Integrator] - Create MailLib to be used by orchestrator instead of the mailController."

See merge request !43
2 merge requests!52:sparkles: Releasing Fusiondirectory Integrator 1.2,!43Resolve "[Integrator] - Create MailLib to be used by orchestrator instead of the mailController."
Pipeline #29554 passed with stages
in 1 minute and 42 seconds
Showing with 126 additions and 0 deletions
+126 -0
......@@ -40,6 +40,7 @@ create_phpstan_rapport:
script:
- test -d ../dev-tools/ && rm -Rf ../dev-tools/
- git clone --depth 1 https://gitlab.fusiondirectory.org/fusiondirectory/dev-tools.git ../dev-tools
- apt install libphp-phpmailer
- cp ../dev-tools/phpstan/fusiondirectory-integrator/*.neon .
- /root/.composer/vendor/bin/phpstan analyse -c phpstan.neon
......
<?php
namespace FusionDirectory\Mail;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
class MailLib
{
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
......@@ -10,6 +10,12 @@ spl_autoload_register(function ($class) {
// Simple array to keep track of which classes have already been loaded.
static $classes = [];
if (strpos($class, 'PHPMailer') !== FALSE) {
require_once("/usr/share/php/libphp-phpmailer/src/Exception.php");
require_once("/usr/share/php/libphp-phpmailer/src/PHPMailer.php");
require_once("/usr/share/php/libphp-phpmailer/src/SMTP.php");
}
// Avoids re-loading classes that have already been loaded.
if (array_key_exists($class, $classes)) {
return;
......
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