diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6f7826dca53f79fcdbe036012eb703ee4ae5f605..88ff820707587dd1a86e868f83c7e243b3d1c07c 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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 diff --git a/src/FusionDirectory/Mail/MailLib.php b/src/FusionDirectory/Mail/MailLib.php new file mode 100644 index 0000000000000000000000000000000000000000..e25450b280b03c3a804010e174a7d0c21916fdbe --- /dev/null +++ b/src/FusionDirectory/Mail/MailLib.php @@ -0,0 +1,119 @@ +<?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 diff --git a/src/autoloader.php b/src/autoloader.php index 97f858f35ddd4e065611d76f8e98a72c1ce3b0b0..c291451f11fb08448081ab61b7f510de028f9c21 100644 --- a/src/autoloader.php +++ b/src/autoloader.php @@ -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;