Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Email Manager #22

Merged
merged 7 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ jobs:
fail-fast: false
matrix:
php-versions:
- '7.3'
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
steps:
- uses: actions/checkout@v2

Expand Down Expand Up @@ -52,10 +51,9 @@ jobs:
fail-fast: false
matrix:
php-versions:
- '7.3'
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
steps:
- uses: actions/checkout@v2

Expand Down Expand Up @@ -90,10 +88,9 @@ jobs:
fail-fast: false
matrix:
php-versions:
- '7.3'
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'

services:
mysql:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Added

- Add an `EmailManager` to configure `PHPMailer` via environment variables ([#22](https://github.com/studiometa/wp-toolkit/pull/22))
- Add a `Plugin::disable` method to the `Plugin` helper class ([#26](https://github.com/studiometa/wp-toolkit/pull/26))
- Add a `request` helper function ([#26](https://github.com/studiometa/wp-toolkit/pull/26))
- Add a `Request` helper class ([#26](https://github.com/studiometa/wp-toolkit/pull/26))
Expand Down
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
"type": "library",
"require": {
"php": "^8.1",
"symfony/yaml": "^6.4|^7.0",
"monolog/monolog": "^2.9|^3.0",
"psr/log": "^1.1",
"studiometa/webpack-config": "^5.0",
"wecodemore/wordpress-early-hook": "^1.2",
"symfony/http-foundation": "^6.4|^7.0"
"symfony/http-foundation": "^6.4|^7.0",
"symfony/yaml": "^6.4|^7.0",
"wecodemore/wordpress-early-hook": "^1.2"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.4",
Expand Down Expand Up @@ -46,6 +48,7 @@
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"phpstan/extension-installer": true
}
},
"sort-packages": true
}
}
154 changes: 153 additions & 1 deletion composer.lock

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

115 changes: 115 additions & 0 deletions src/Managers/EmailManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* Configure emails.
*
* @package Studiometa\WPToolkit
*/

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;

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

View check run for this annotation

Codecov / codecov/patch

src/Managers/EmailManager.php#L35

Added line #L35 was not covered by tests
} elseif (env('MAIL_LOG')) {
$this->logger = new Logger('email');
$this->logger->pushHandler(new StreamHandler(env('MAIL_LOG')));

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

View check run for this annotation

Codecov / codecov/patch

src/Managers/EmailManager.php#L37-L38

Added lines #L37 - L38 were not covered by tests
}
}

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

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 53 in src/Managers/EmailManager.php

View check run for this annotation

Codecov / codecov/patch

src/Managers/EmailManager.php#L52-L53

Added lines #L52 - L53 were not covered by tests
}
}

/**
* Configure SMTP server to send mails.
*
* @param PHPMailer $mailer The PHPMailer instance.
* @return void
*/
public function configure_smtp(PHPMailer $mailer): void
{
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$mailer->Host = env('MAIL_HOST');
$mailer->Port = (int) env('MAIL_PORT');
$mailer->Username = env('MAIL_USERNAME');
$mailer->Password = env('MAIL_PASSWORD');
$mailer->SMTPAuth = 'true' === env('MAIL_AUTH');
$mailer->SMTPSecure = env('MAIL_ENCRYPTION');
$mailer->IsSMTP();
// 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

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

View check run for this annotation

Codecov / codecov/patch

src/Managers/EmailManager.php#L89

Added line #L89 was not covered by tests
{
if ($this->logger) {
$this->logger->info('Mail sent', $mail_data);

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

View check run for this annotation

Codecov / codecov/patch

src/Managers/EmailManager.php#L91-L92

Added lines #L91 - L92 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

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

View check run for this annotation

Codecov / codecov/patch

src/Managers/EmailManager.php#L103

Added line #L103 was not covered by tests
{
if ($this->logger) {

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

View check run for this annotation

Codecov / codecov/patch

src/Managers/EmailManager.php#L105

Added line #L105 was 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 112 in src/Managers/EmailManager.php

View check run for this annotation

Codecov / codecov/patch

src/Managers/EmailManager.php#L111-L112

Added lines #L111 - L112 were not covered by tests
}
}
}
Loading
Loading