Test mailgun connection before storing credentials into the app

This commit is contained in:
Čarodej
2022-04-08 08:00:13 +02:00
parent 6ed2efcc4e
commit 86090d5192
14 changed files with 222 additions and 134 deletions

View File

@@ -0,0 +1,47 @@
<?php
namespace Domain\Settings\Actions;
use Mail;
use Domain\Settings\Mail\TestMail;
use Symfony\Component\Mailer\Exception\LogicException;
use Symfony\Component\Mailer\Exception\TransportException;
class TestSMTPConnectionAction
{
/**
* Throw an Exception if connection isn't successful.
*
* @return never
*/
public function __invoke(array $credentials)
{
try {
// Set temporary mail connection
config(['mail' => [
'driver' => 'smtp',
'host' => $credentials['host'],
'port' => $credentials['port'],
'username' => $credentials['username'],
'password' => $credentials['password'],
'encryption' => $credentials['encryption'] ?? '',
'from' => [
'address' => $credentials['email'] ?? $credentials['username'],
'name' => $credentials['email'] ?? $credentials['username'],
],
]]);
$sender = $credentials['email'] ?? $credentials['username'];
// Send test email
Mail::to('test@hi5ve.digital')->send(new TestMail($sender));
} catch (TransportException | LogicException $error) {
abort(
response()->json([
'type' => 'mailer-connection-error',
'title' => 'Mail Connection Error',
'message' => $error->getMessage(),
], 401)
);
}
}
}