<?php
 use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; vB::includeLibraryFile('vendor/autoload.php'); abstract class vB_Mail_Transport_PhpMailer extends vB_Mail_Transport { protected ?PHPMailer $mailer; protected array $smtpSecureMap = [ 'none' => '', 'tls' => PHPMailer::ENCRYPTION_STARTTLS, 'ssl' => PHPMailer::ENCRYPTION_SMTPS, ]; protected array $debug = []; public function send(array|string $to, string $subject, string $message, bool $isHtml, array $headers, array|string $replyto = '') : bool { $mailer = $this->getMailer(); $this->setMailData($to, $subject, $message, $isHtml, $headers, $replyto); $status = $mailer->send(); if (!$status) { throw new Exception($mailer->ErrorInfo); } return true; } public function setDebug(bool $isDebug) : void { parent::setDebug($isDebug); if (isset($this->mailer)) { $this->mailer->SMTPDebug = ($isDebug ? SMTP::DEBUG_SERVER : SMTP::DEBUG_OFF); } } protected function getMailer() { if (!isset($this->mailer)) { $this->mailer = $this->createPhpMailer(); } return $this->mailer; } public function getDebugInfo() : array { return $this->debug; } private function logDebug(string $string, int $level) : void { $this->debug[] = $string; } protected function setMailData(array|string $to, string $subject, string $message, bool $isHtml, array $headers, array|string $replyto) : void { $mailer = $this->mailer; $mailer->clearAllRecipients(); $mailer->clearCustomHeaders(); $mailer->SMTPDebug = ($this->isDebug ? SMTP::DEBUG_SERVER : SMTP::DEBUG_OFF); $mailer->Debugoutput = $this->logDebug(...); $this->debug = []; if (!$this->isDataUtf8Encoded) { $message = $this->toUtf8($message); $subject = $this->toUtf8($subject); } foreach ($this->defaultHeaders AS $header) { $mailer->addCustomHeader($header); } foreach ($headers AS $header) { $mailer->addCustomHeader($header); } $mailer->addAddress(...$this->getEmailParms($to)); if ($replyto) { $replyto = $this->getEmailParms($replyto); $mailer->addReplyTo(...$replyto); } $vboptions = vB::getDatastore()->getValue('options'); if (!empty($replyto[1])) { $sendername = $replyto[1] . ' - ' . $vboptions['bbtitle']; } else { $sendername = $this->getSenderName(); } $mailer->setFrom($this->toUtf8($vboptions['webmasteremail']), $this->toUtf8($sendername)); $mailer->Subject = $subject; $mailer->CharSet = PHPMailer::CHARSET_UTF8; $mailer->isHTML($isHtml); if ($isHtml) { $mailer->msgHTML($message); } else { $mailer->Body = $message; } } protected function createPhpMailer() : PHPMailer { $vboptions = vB::getDatastore()->getValue('options'); $mailer = new PHPMailer(); $mailer->CharSet = PHPMailer::CHARSET_UTF8; if ($vboptions['bounceemail']) { $mailer->Sender = $vboptions['bounceemail']; } return $mailer; } protected function getEmailParms(array|string $email) : array { if (!is_array($email)) { $email = [$this->toUtf8($email), '']; } else { $email[1] = $this->toUtf8($email[1]); } return $email; } public function startBatch() { $this->getMailer()->SMTPKeepAlive = true; } public function endBatch() { if (isset($this->mailer)) { $this->mailer->smtpClose(); } } } 