diff --git a/README.md b/README.md index 5a6621c..082e830 100644 --- a/README.md +++ b/README.md @@ -1,22 +1,22 @@ -# WordPress Mail URL +# WordPress Mailer DSN -[![CI](https://github.com/voronkovich/wordpress-mail-url/actions/workflows/ci.yml/badge.svg)](https://github.com/voronkovich/wordpress-mail-url/actions/workflows/ci.yml) +[![CI](https://github.com/voronkovich/wordpress-mailer-dsn/actions/workflows/ci.yml/badge.svg)](https://github.com/voronkovich/wordpress-mailer-dsn/actions/workflows/ci.yml) -[WordPress](https://wordpress.org/) plugin to configure [wp_mail()](https://developer.wordpress.org/reference/functions/wp_mail/) via `MAIL_URL` environment variable. +[WordPress](https://wordpress.org/) plugin to configure [wp_mail()](https://developer.wordpress.org/reference/functions/wp_mail/) via `MAILER_DSN` environment variable. ## Installation Use the [Composer](https://getcomposer.org/): ```sh -composer require voronkovich/wordpress-mail-url +composer require voronkovich/wordpress-mailer-dsn ``` Don't forget to activate the plugin, if you don't use the `mu-plugins` directory. -Define (in your `.env` file for example) the `MAIL_URL` variable like this: +Define (in your `.env` file for example) the `MAILER_DSN` variable like this: ```sh -MAIL_URL='mail://localhost' +MAILER_DSN='mail://localhost' ``` ## Configuraton @@ -32,7 +32,7 @@ Supported protocols: Additional configuration could be applied via query string: ```sh -MAIL_URL='mail://localhost?XMailer=SuperMailer&FromName=CoolSite' +MAILER_DSN='mail://localhost?XMailer=SuperMailer&FromName=CoolSite' ``` [PHPMailer](https://github.com/PHPMailer/PHPMailer) configured by public properties, so you can use any of them. All allowed options could be found at [PHPMailer Docs](https://phpmailer.github.io/PHPMailer/classes/PHPMailer-PHPMailer-PHPMailer.html#toc-properties). @@ -41,17 +41,17 @@ MAIL_URL='mail://localhost?XMailer=SuperMailer&FromName=CoolSite' ### Sendmail ```sh -MAIL_URL='sendmail://localhost?Sendmail=/usr/sbin/sendmail%20-oi%20-t' +MAILER_DSN='sendmail://localhost?Sendmail=/usr/sbin/sendmail%20-oi%20-t' ``` ### SMTP ```sh -MAIL_URL='smtp://user@password@localhost?SMTPDebug=3&Timeout=1000' +MAILER_DSN='smtp://user@password@localhost?SMTPDebug=3&Timeout=1000' ``` ### Gmail ```sh -MAIL_URL='smtps://user@gmail.com:password@smtp.gmail.com?SMTPDebug=3' +MAILER_DSN='smtps://user@gmail.com:password@smtp.gmail.com?SMTPDebug=3' ``` ## License diff --git a/composer.json b/composer.json index 6e05823..af91e7a 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { - "name": "voronkovich/wordpress-mail-url", - "description": "WordPress plugin to configure wp_mail() via MAIL_URL environment variable.", - "homepage": "https://github.com/voronkovich/wordpress-mail-url", + "name": "voronkovich/wordpress-mailer-dsn", + "description": "WordPress plugin to configure wp_mail() via MAILER_DSN environment variable.", + "homepage": "https://github.com/voronkovich/wordpress-mailer-dsn", "keywords": ["wordpress", "plugin"], "type": "wordpress-muplugin", "license": "MIT", diff --git a/functions.php b/functions.php index 3cf7b96..1ddf2b0 100644 --- a/functions.php +++ b/functions.php @@ -2,21 +2,21 @@ defined('ABSPATH') || exit; -function mailurl_phpmailer_init($phpmailer) +function mailerdsn_phpmailer_init($phpmailer) { - $url = \mailurl_get_url(); + $url = \mailerdsn_get_dsn(); if ($url) { - \mailurl_phpmailer_configure($phpmailer, $url); + \mailerdsn_phpmailer_configure($phpmailer, $url); } } -function mailurl_get_url() +function mailerdsn_get_dsn() { - return \defined('MAIL_URL') ? MAIL_URL : \getenv('MAIL_URL'); + return \defined('MAILER_DSN') ? MAILER_DSN : \getenv('MAILER_DSN'); } -function mailurl_parse_url($url) +function mailerdsn_parse_url($url) { $config = \parse_url($url); @@ -33,9 +33,9 @@ function mailurl_parse_url($url) return $config; } -function mailurl_phpmailer_configure($phpmailer, $url) +function mailerdsn_phpmailer_configure($phpmailer, $url) { - $config = \mailurl_parse_url($url); + $config = \mailerdsn_parse_url($url); switch ($config['scheme']) { case 'mail': @@ -49,7 +49,7 @@ function mailurl_phpmailer_configure($phpmailer, $url) break; case 'smtp': case 'smtps': - \mailurl_phpmailer_configure_smtp($phpmailer, $config); + \mailerdsn_phpmailer_configure_smtp($phpmailer, $config); break; default: throw new \RuntimeException( @@ -61,13 +61,13 @@ function mailurl_phpmailer_configure($phpmailer, $url) } if (isset($config['query'])) { - \mailurl_phpmailer_configure_options($phpmailer, $config['query']); + \mailerdsn_phpmailer_configure_options($phpmailer, $config['query']); } return $phpmailer; } -function mailurl_phpmailer_configure_smtp($phpmailer, $config) +function mailerdsn_phpmailer_configure_smtp($phpmailer, $config) { $phpmailer->isSMTP(); $isSMTPS = 'smtps' === $config['scheme']; @@ -88,7 +88,7 @@ function mailurl_phpmailer_configure_smtp($phpmailer, $config) } } -function mailurl_phpmailer_configure_options($phpmailer, $options) +function mailerdsn_phpmailer_configure_options($phpmailer, $options) { $allowedOptions = \get_object_vars($phpmailer); diff --git a/mailurl.php b/mailerdsn.php similarity index 50% rename from mailurl.php rename to mailerdsn.php index 22baa7d..14f7e54 100644 --- a/mailurl.php +++ b/mailerdsn.php @@ -1,12 +1,12 @@ * Author URI: https://github.com/voronkovich */ @@ -16,5 +16,5 @@ add_action('phpmailer_init', function ($phpmailer) { require_once __DIR__ . '/functions.php'; - \mailurl_phpmailer_init($phpmailer); + \mailerdsn_phpmailer_init($phpmailer); }); diff --git a/tests/MailUrlTest.php b/tests/MailerDsnTest.php similarity index 65% rename from tests/MailUrlTest.php rename to tests/MailerDsnTest.php index 6515b69..68abd2c 100644 --- a/tests/MailUrlTest.php +++ b/tests/MailerDsnTest.php @@ -14,51 +14,51 @@ /** * @coversNothing */ -class MailUrlTest extends TestCase +class MailerDsnTest extends TestCase { - public function testThrowsExceptionIfUrlIsMailformed() + public function testThrowsExceptionIfDsnIsMailformed() { - \putenv('MAIL_URL=localhost'); + \putenv('MAILER_DSN=localhost'); $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('Mailformed mail URL: "localhost".'); $phpmailer = new PHPMailer(true); - \mailurl_phpmailer_init($phpmailer); + \mailerdsn_phpmailer_init($phpmailer); } - public function testThrowsExceptionIfUrHasInvalidScheme() + public function testThrowsExceptionIfDsnHasInvalidScheme() { - \putenv('MAIL_URL=ftp://localhost'); + \putenv('MAILER_DSN=ftp://localhost'); $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('Invalid mail URL scheme: "ftp"'); $phpmailer = new PHPMailer(true); - \mailurl_phpmailer_init($phpmailer); + \mailerdsn_phpmailer_init($phpmailer); } - public function testThrowsExceptionIfUrHasInvalidOption() + public function testThrowsExceptionIfDsnHasInvalidOption() { - \putenv('MAIL_URL=mail://localhost?Helo=Hi&Unknown=Invalid'); + \putenv('MAILER_DSN=mail://localhost?Helo=Hi&Unknown=Invalid'); $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('Unknown mail URL option: "Unknown"'); $phpmailer = new PHPMailer(true); - \mailurl_phpmailer_init($phpmailer); + \mailerdsn_phpmailer_init($phpmailer); } - public function testConfiguresPHPMailerWithProvidedByUrlSettings() + public function testConfiguresPHPMailerWithProvidedByDsnSettings() { - \putenv('MAIL_URL=smtps://user@gmail.com:password@smtp.gmail.com?SMTPDebug=3&Timeout=60'); + \putenv('MAILER_DSN=smtps://user@gmail.com:password@smtp.gmail.com?SMTPDebug=3&Timeout=60'); $phpmailer = new PHPMailer(true); - \mailurl_phpmailer_init($phpmailer); + \mailerdsn_phpmailer_init($phpmailer); $this->assertEquals($phpmailer->Mailer, 'smtp'); $this->assertEquals($phpmailer->Host, 'smtp.gmail.com');