Skip to content

Commit

Permalink
Rename pluging to wordpress-mailer-dsn
Browse files Browse the repository at this point in the history
  • Loading branch information
voronkovich committed Feb 20, 2023
1 parent 8cd7ecc commit 94b7dcd
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 43 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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).
Expand All @@ -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://[email protected]:[email protected]?SMTPDebug=3'
MAILER_DSN='smtps://[email protected]:[email protected]?SMTPDebug=3'
```

## License
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
24 changes: 12 additions & 12 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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':
Expand All @@ -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(
Expand All @@ -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'];
Expand All @@ -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);

Expand Down
10 changes: 5 additions & 5 deletions mailurl.php → mailerdsn.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

/**
* Plugin Name: Mail URL
* Plugin URI: https://github.com/voronkovich/wordpress-mail-url
* Description: Configure wp_mail() via MAIL_URL environment variable
* Plugin Name: Mailer DSN
* Plugin URI: https://github.com/voronkovich/wordpress-mailer-dsn
* Description: Configure wp_mail() via MAILER_DSN environment variable
* Version: 0.0.2
* License: MIT
* License URI: https://github.com/voronkovich/wordpress-mail-url/blob/main/LICENSE
* License URI: https://github.com/voronkovich/wordpress-mailer-dsn/blob/main/LICENSE
* Author: Oleg Voronkovich <[email protected]>
* Author URI: https://github.com/voronkovich
*/
Expand All @@ -16,5 +16,5 @@
add_action('phpmailer_init', function ($phpmailer) {
require_once __DIR__ . '/functions.php';

\mailurl_phpmailer_init($phpmailer);
\mailerdsn_phpmailer_init($phpmailer);
});
26 changes: 13 additions & 13 deletions tests/MailUrlTest.php → tests/MailerDsnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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://[email protected]:[email protected]?SMTPDebug=3&Timeout=60');
\putenv('MAILER_DSN=smtps://[email protected]:[email protected]?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');
Expand Down

0 comments on commit 94b7dcd

Please sign in to comment.