Skip to content

Commit

Permalink
Add support for email success and failure logs
Browse files Browse the repository at this point in the history
  • Loading branch information
titouanmathis committed Oct 20, 2023
1 parent f336f0d commit 9eaf7c6
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 4 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"require": {
"php": "^7.3|^8.0",
"symfony/yaml": "^5.1",
"studiometa/webpack-config": "^4.0"
"studiometa/webpack-config": "^4.0",
"monolog/monolog": "^2.9|^3.0",
"psr/log": "^1.1"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.4",
Expand Down
156 changes: 154 additions & 2 deletions composer.lock

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

64 changes: 63 additions & 1 deletion src/Managers/EmailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,47 @@

namespace Studiometa\WPToolkit\Managers;

use WP_Error;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Psr\Log\LoggerInterface;
use PHPMailer\PHPMailer\PHPMailer;
use function Studiometa\WPToolkit\env;

/** Class **/
class EmailManager implements ManagerInterface {
/**
* Logger instance.
*
* @var LoggerInterface|null;
*/
private $logger;

/**
* Class constructor.
*
* @param LoggerInterface|null $logger A logger instance to log mail actions.
*/
public function __construct( ?LoggerInterface $logger = null ) {
if ( ! is_null( $logger ) ) {
$this->logger = $logger;
} elseif ( env( 'MAIL_LOG' ) ) {
$this->logger = new Logger( 'email' );
$this->logger->pushHandler( new StreamHandler( env( 'MAIL_LOG' ) ) );

Check warning on line 36 in src/Managers/EmailManager.php

View check run for this annotation

Codecov / codecov/patch

src/Managers/EmailManager.php#L31-L36

Added lines #L31 - L36 were not covered by tests
}
}

/**
* {@inheritdoc}
*/
public function run() {
if ( 'smtp' === env( 'MAIL_MAILER' ) ) {
add_action( 'phpmailer_init', array( $this, 'configure_smtp' ), 10, 1 );
add_action( 'phpmailer_init', array( $this, 'configure_smtp' ) );

Check warning on line 45 in src/Managers/EmailManager.php

View check run for this annotation

Codecov / codecov/patch

src/Managers/EmailManager.php#L43-L45

Added lines #L43 - L45 were not covered by tests
}

if ( $this->logger ) {
add_action( 'wp_mail_succeeded', array( $this, 'log_success' ) );
add_action( 'wp_mail_failed', array( $this, 'log_failure' ) );

Check warning on line 50 in src/Managers/EmailManager.php

View check run for this annotation

Codecov / codecov/patch

src/Managers/EmailManager.php#L48-L50

Added lines #L48 - L50 were not covered by tests
}
}

Expand All @@ -38,4 +68,36 @@ public function configure_smtp( PHPMailer $mailer ): void {
$mailer->IsSMTP();

Check warning on line 68 in src/Managers/EmailManager.php

View check run for this annotation

Codecov / codecov/patch

src/Managers/EmailManager.php#L62-L68

Added lines #L62 - L68 were not covered by tests
// phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
}

/**
* Log successfully sent mails.
*
* @param array{to: string[], subject: string, message: string, headers: string[], attachments: string[]} $mail_data An array containing the email recipient(s), subject, message, headers, and attachments.
*
* @return void
*/
public function log_success( array $mail_data ): void {
if ( $this->logger ) {
$this->logger->info( 'Mail sent', $mail_data );

Check warning on line 81 in src/Managers/EmailManager.php

View check run for this annotation

Codecov / codecov/patch

src/Managers/EmailManager.php#L79-L81

Added lines #L79 - L81 were not covered by tests
}
}

/**
* Log failure happening when sending mails.
*
* @param WP_Error $error The error sent.
*
* @return void
*/
public function log_failure( WP_Error $error ): void {
if ( $this->logger ) {

Check warning on line 93 in src/Managers/EmailManager.php

View check run for this annotation

Codecov / codecov/patch

src/Managers/EmailManager.php#L92-L93

Added lines #L92 - L93 were not covered by tests
/**
* Mail data.
*
* @var array
*/
$mail_data = $error->get_error_data();
$this->logger->error( $error->get_error_message(), $mail_data );

Check warning on line 100 in src/Managers/EmailManager.php

View check run for this annotation

Codecov / codecov/patch

src/Managers/EmailManager.php#L99-L100

Added lines #L99 - L100 were not covered by tests
}
}
}

0 comments on commit 9eaf7c6

Please sign in to comment.