Skip to content

Commit

Permalink
add log service
Browse files Browse the repository at this point in the history
  • Loading branch information
BMTmohammedtaha committed Dec 29, 2023
1 parent 1955e07 commit 8ef31eb
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 60 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"require": {
"effectra/config": "1.0",
"phpmailer/phpmailer": "^6.8",
"effectra/data-optimizer": "^1.1"
"effectra/data-optimizer": "^1.1",
"psr/log": "^3.0"
},
"minimum-stability": "stable",
"require-dev": {
Expand Down
102 changes: 51 additions & 51 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 31 additions & 8 deletions src/Services/PHPMailerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Effectra\Mail\Contracts\MailerInterface;
use Effectra\Mail\Contracts\MailInterface;
use Effectra\Mail\Exception\MailerException;
use Effectra\Mail\Utils\LoggerTrait;
use PHPMailer\PHPMailer\PHPMailer;

/**
Expand All @@ -17,6 +18,7 @@
*/
class PHPMailerService extends ConfigDriver implements MailerInterface
{
use LoggerTrait;
/**
* Send an email using PHPMailer.
*
Expand All @@ -28,6 +30,13 @@ public function send(MailInterface $mail)
{
$phpMailer = new PHPMailer();

if ($this->logger) {
$phpMailer->SMTPDebug = 2; // Set the debug level (0 to disable)
$phpMailer->Debugoutput = function ($str, $level) {
$this->startLog($level, $str);
};
}

$phpMailer->isSMTP();
$phpMailer->Host = $this->getHost();
$phpMailer->SMTPAuth = true;
Expand All @@ -37,21 +46,36 @@ public function send(MailInterface $mail)
// $phpMailer->SMTPSecure = 'tls';
$phpMailer->Port = $this->getPort();

$phpMailer->setFrom((string) $mail->getFrom());
$phpMailer->setFrom(
$mail->getFrom()->getEmail(),
$mail->getFrom()->getName()
);

foreach ($mail->getTo() as $address) {
$phpMailer->addAddress($address->getEmail(), $address->getName());
$phpMailer->addAddress(
$address->getEmail(),
$address->getName()
);
}

if ($mail->getReplyTo()) {
$phpMailer->addReplyTo((string) $mail->getReplyTo());
$phpMailer->addReplyTo(
$mail->getReplyTo()->getEmail(),
$mail->getReplyTo()->getName()
);
}
if ($mail->getCC()) {
$phpMailer->addCC((string) $mail->getCC());
$phpMailer->addCC(
$mail->getCC()->getEmail(),
$mail->getCC()->getName()
);
}

if ($mail->getBcc()) {
$phpMailer->addBCC((string) $mail->getBcc());
$phpMailer->addBCC(
$mail->getBcc()->getEmail(),
$mail->getBcc()->getName()
);
}

$phpMailer->Subject = $mail->getSubject();
Expand All @@ -73,9 +97,8 @@ public function send(MailInterface $mail)
}
}

if (!$phpMailer->send()) {
throw new MailerException('Mailer Error: ' . $phpMailer->ErrorInfo, 1);
}
$phpMailer->send();

$phpMailer->ClearAddresses();
$phpMailer->ClearAttachments();
}
Expand Down
79 changes: 79 additions & 0 deletions src/Utils/LoggerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Effectra\Mail\Utils;

use Psr\Log\LoggerInterface;

/**
* Trait LoggerTrait
*
* A trait providing logging capabilities using PSR-3 LoggerInterface.
*/
trait LoggerTrait
{
/** @var LoggerInterface|null The logger instance. */
private ?LoggerInterface $logger = null;

/**
* Get the logger instance.
*
* @return LoggerInterface|null The logger instance.
*/
public function getLogger(): ?LoggerInterface
{
return $this->logger;
}

/**
* Set the logger instance.
*
* @param LoggerInterface $logger The logger instance to set.
* @return self
*/
public function setLogger(LoggerInterface $logger): self
{
$this->logger = $logger;
return $this;
}

/**
* Start logging using the configured logger.
*
* @param string $str The log message.
* @param int $level The log level.
* @return void
*/
public function startLog(string $str, int $level): void
{
$internalLogLevel = $this->mapDebugLevelToLogLevel($level);
$this->logger->log($internalLogLevel, $str);
}

/**
* Map PHPMailer debug level to PSR-3 log level.
*
* @param int $level The PHPMailer debug level.
* @return string The corresponding PSR-3 log level.
*/
private function mapDebugLevelToLogLevel(int $level): string
{
switch ($level) {
case 0:
return 'debug'; // or 'info' based on your preference
case 1:
return 'debug';
case 2:
return 'info';
case 3:
return 'notice';
case 4:
return 'warning';
case 5:
return 'error';
default:
return 'error';
}
}
}

0 comments on commit 8ef31eb

Please sign in to comment.