Skip to content

Commit 4c888a3

Browse files
committed
🔧 create AcornMail provider and service
1 parent 18ce49c commit 4c888a3

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed

‎composer.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "inrage/acorn-mail",
3+
"type": "package",
4+
"description": "Simple WordPress SMTP using Acorn.",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Pascal GAULT",
9+
"email": "[email protected]",
10+
"homepage": "https://www.inrage.fr"
11+
}
12+
],
13+
"require": {
14+
"php": ">=8.3"
15+
},
16+
"require-dev": {
17+
"roots/acorn": "^4.0"
18+
},
19+
"autoload": {
20+
"psr-4": {
21+
"inRage\\AcornMail\\": "src/"
22+
}
23+
},
24+
"extra": {
25+
"acorn": {
26+
"providers": [
27+
"inRage\\AcornMail\\AcornMailServiceProvider"
28+
]
29+
}
30+
}
31+
}

‎config/wp-mail.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
return [
4+
'default' => env('MAIL_MAILER', 'native'),
5+
6+
'smtp' => [
7+
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
8+
'port' => env('MAIL_PORT', 587),
9+
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
10+
'username' => env('MAIL_USERNAME'),
11+
'password' => env('MAIL_PASSWORD'),
12+
],
13+
14+
'from' => [
15+
'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
16+
'name' => env('MAIL_FROM_NAME', 'Example'),
17+
],
18+
];

‎src/AcornMail.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace inRage\AcornMail;
4+
5+
use Illuminate\Support\Collection;
6+
use Illuminate\Support\Str;
7+
use PHPMailer\PHPMailer\PHPMailer;
8+
use Roots\Acorn\Application;
9+
10+
class AcornMail
11+
{
12+
protected Collection $config;
13+
14+
public function __construct(
15+
protected readonly Application $app
16+
) {
17+
$this->config = Collection::make($this->app->config->get('wp-mail.smtp'))
18+
->merge($this->app->config->get('wp-mail.from'))
19+
->merge($this->app->config->get('wp-mail.default')
20+
);
21+
22+
if (! $this->isConfigured()) {
23+
return;
24+
}
25+
26+
$this->overrideWordPressMailer();
27+
}
28+
29+
protected function fromName(): string
30+
{
31+
$name = $this->config->get('name');
32+
33+
return $name && ! Str::is($name, 'Example')
34+
? $name
35+
: get_bloginfo('name', 'display');
36+
}
37+
38+
protected function fromAddress(): string
39+
{
40+
$address = $this->config->get('address');
41+
42+
if ($address && ! Str::is($address, '[email protected]')) {
43+
return $address;
44+
}
45+
46+
$domain = parse_url(home_url(), PHP_URL_HOST);
47+
48+
return "noreply@{$domain}";
49+
}
50+
51+
public static function make(Application $app): self
52+
{
53+
return new static($app);
54+
}
55+
56+
private function isConfigured(): bool
57+
{
58+
return match ($this->config->get(0)) {
59+
'smtp' => $this->config->get('host') && $this->config->get('port'),
60+
'smtps' => $this->config->get('host')
61+
&& $this->config->get('port')
62+
&& $this->config->get('username')
63+
&& $this->config->get('password'),
64+
'native' => true,
65+
default => false,
66+
};
67+
}
68+
69+
private function overrideWordPressMailer(): void
70+
{
71+
if (! in_array($this->config->get(0), ['smtp', 'smtps'])) {
72+
return;
73+
}
74+
75+
add_filter('phpmailer_init', function (PHPMailer $mail) {
76+
$mail->isSMTP();
77+
78+
$mail->SMTPAuth = $this->config->get(0) === 'smtps';
79+
$mail->Host = $this->config->get('host');
80+
$mail->Port = $this->config->get('port');
81+
$mail->Timeout = $this->config->get('timeout', $mail->Timeout);
82+
83+
if ($this->config->get(0) === 'smtps') {
84+
$mail->SMTPSecure = $this->config->get('encryption', $mail->SMTPSecure);
85+
$mail->Username = $this->config->get('username');
86+
$mail->Password = $this->config->get('password');
87+
}
88+
89+
$mail->setFrom(
90+
$this->fromAddress(),
91+
$this->fromName()
92+
);
93+
});
94+
}
95+
}

‎src/AcornMailServiceProvider.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace inRage\AcornMail;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class AcornMailServiceProvider extends ServiceProvider
8+
{
9+
public function register(): void
10+
{
11+
$this->app->singleton('inRage\AcornMail', fn () => AcornMail::make($this->app));
12+
}
13+
14+
public function boot(): void
15+
{
16+
$this->publishes([
17+
__DIR__.'/../config/wp-mail.php' => $this->app->configPath('wp-mail.php'),
18+
], 'acorn-mail');
19+
20+
$this->app->make('inRage\AcornMail');
21+
}
22+
}

0 commit comments

Comments
 (0)