Test postmark connection before storing credentials into the app

This commit is contained in:
Čarodej
2022-04-08 09:44:58 +02:00
parent 86090d5192
commit 9a15f2ecd8
10 changed files with 138 additions and 27 deletions

View File

@@ -19,7 +19,9 @@ class TestMailgunConnectionAction
// Set temporary mail connection
config([
'mail' => [
'driver' => 'mailgun',
'driver' => 'mailgun',
'from.address' => $credentials['sender'],
'from.name' => $credentials['sender'],
],
'services' => [
'mailgun' => [
@@ -31,7 +33,7 @@ class TestMailgunConnectionAction
]);
// Send test email
Mail::to('test@hi5ve.digital')->send(new TestMail('example@domain.com'));
Mail::to($credentials['sender'])->send(new TestMail($credentials['sender']));
} catch (TransportException | LogicException $error) {
abort(
response()->json([

View File

@@ -0,0 +1,34 @@
<?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 TestPostmarkConnectionAction
{
public function __invoke(array $credentials)
{
try {
// Set temporary mail connection
config([
'mail.driver' => 'postmark',
'mail.from.address' => $credentials['sender'],
'mail.from.name' => $credentials['sender'],
'services.postmark.token' => $credentials['token'],
]);
// Send test email
Mail::to($credentials['sender'])->send(new TestMail($credentials['sender']));
} catch (TransportException | LogicException $error) {
abort(
response()->json([
'type' => 'mailer-connection-error',
'title' => 'Mail Connection Error',
'message' => $error->getMessage(),
], 401)
);
}
}
}

View File

@@ -30,10 +30,11 @@ class TestSMTPConnectionAction
],
]]);
// Get sender
$sender = $credentials['email'] ?? $credentials['username'];
// Send test email
Mail::to('test@hi5ve.digital')->send(new TestMail($sender));
Mail::to($sender)->send(new TestMail($sender));
} catch (TransportException | LogicException $error) {
abort(
response()->json([

View File

@@ -5,11 +5,13 @@ use Artisan;
use Illuminate\Http\JsonResponse;
use Domain\Settings\Actions\TestSMTPConnectionAction;
use Domain\Settings\Actions\TestMailgunConnectionAction;
use Domain\Settings\Actions\TestPostmarkConnectionAction;
use Domain\Settings\Requests\StoreEmailCredentialsRequest;
class StoreEmailCredentialsController
{
public function __construct(
private TestPostmarkConnectionAction $testPostmarkConnection,
private TestMailgunConnectionAction $testMailgunConnection,
private TestSMTPConnectionAction $testSMTPConnection,
) {
@@ -38,16 +40,23 @@ class StoreEmailCredentialsController
'domain' => $request->input('mailgun.domain'),
'secret' => $request->input('mailgun.secret'),
'endpoint' => $request->input('mailgun.endpoint'),
'sender' => $request->input('mailgun.sender'),
]),
'postmark' => ($this->testPostmarkConnection)([
'token' => $request->input('postmark.token'),
'sender' => $request->input('postmark.sender'),
]),
};
$mail = [
'log' => [
'MAIL_DRIVER' => 'log',
'MAIL_DRIVER' => 'log',
],
'postmark' => [
'MAIL_DRIVER' => 'postmark',
'POSTMARK_TOKEN' => $request->input('postmark.token'),
'MAIL_DRIVER' => 'postmark',
'POSTMARK_TOKEN' => $request->input('postmark.token'),
'MAIL_FROM_ADDRESS' => $request->input('postmark.sender'),
'MAIL_FROM_NAME' => $request->input('postmark.sender'),
],
'smtp' => [
'MAIL_DRIVER' => 'smtp',
@@ -56,8 +65,8 @@ class StoreEmailCredentialsController
'MAIL_USERNAME' => $request->input('smtp.username'),
'MAIL_PASSWORD' => $request->input('smtp.password'),
'MAIL_ENCRYPTION' => $request->input('smtp.encryption') ?? '',
'MAIL_FROM_ADDRESS' => $request->input('smtp.email') ?? '"${MAIL_USERNAME}"',
'MAIL_FROM_NAME' => $request->input('smtp.email') ?? '"${MAIL_USERNAME}"',
'MAIL_FROM_ADDRESS' => $request->input('smtp.email') ?? $request->input('smtp.username'),
'MAIL_FROM_NAME' => $request->input('smtp.email') ?? $request->input('smtp.username'),
],
'ses' => [
'MAIL_DRIVER' => 'ses',
@@ -67,10 +76,12 @@ class StoreEmailCredentialsController
'AWS_SESSION_TOKEN' => $request->input('ses.session_token'),
],
'mailgun' => [
'MAIL_DRIVER' => 'mailgun',
'MAILGUN_DOMAIN' => $request->input('mailgun.domain'),
'MAILGUN_SECRET' => $request->input('mailgun.secret'),
'MAILGUN_ENDPOINT' => $request->input('mailgun.endpoint'),
'MAIL_DRIVER' => 'mailgun',
'MAILGUN_DOMAIN' => $request->input('mailgun.domain'),
'MAILGUN_SECRET' => $request->input('mailgun.secret'),
'MAILGUN_ENDPOINT' => $request->input('mailgun.endpoint'),
'MAIL_FROM_ADDRESS' => $request->input('mailgun.sender'),
'MAIL_FROM_NAME' => $request->input('mailgun.sender'),
],
];

View File

@@ -8,6 +8,7 @@ use Domain\Settings\DTO\S3CredentialsData;
use Domain\Settings\Actions\TestS3ConnectionAction;
use Domain\Settings\Actions\TestSMTPConnectionAction;
use Domain\Settings\Actions\TestMailgunConnectionAction;
use Domain\Settings\Actions\TestPostmarkConnectionAction;
use Domain\SetupWizard\Requests\StoreEnvironmentSetupRequest;
class StoreEnvironmentSettingsController extends Controller
@@ -16,6 +17,7 @@ class StoreEnvironmentSettingsController extends Controller
private TestS3ConnectionAction $testS3Connection,
private TestSMTPConnectionAction $testSMTPConnection,
private TestMailgunConnectionAction $testMailgunConnection,
private TestPostmarkConnectionAction $testPostmarkConnection,
) {
}
@@ -45,6 +47,11 @@ class StoreEnvironmentSettingsController extends Controller
'domain' => $request->input('mailgun.domain'),
'secret' => $request->input('mailgun.secret'),
'endpoint' => $request->input('mailgun.endpoint'),
'sender' => $request->input('mailgun.sender'),
]),
'postmark' => ($this->testPostmarkConnection)([
'token' => $request->input('postmark.token'),
'sender' => $request->input('postmark.sender'),
]),
};
@@ -85,11 +92,13 @@ class StoreEnvironmentSettingsController extends Controller
],
'mail' => [
'log' => [
'MAIL_DRIVER' => 'log',
'MAIL_DRIVER' => 'log',
],
'postmark' => [
'MAIL_DRIVER' => 'postmark',
'POSTMARK_TOKEN' => $request->input('postmark.token'),
'MAIL_DRIVER' => 'postmark',
'POSTMARK_TOKEN' => $request->input('postmark.token'),
'MAIL_FROM_ADDRESS' => $request->input('postmark.sender'),
'MAIL_FROM_NAME' => $request->input('postmark.sender'),
],
'smtp' => [
'MAIL_DRIVER' => 'smtp',
@@ -98,8 +107,8 @@ class StoreEnvironmentSettingsController extends Controller
'MAIL_USERNAME' => $request->input('smtp.username'),
'MAIL_PASSWORD' => $request->input('smtp.password'),
'MAIL_ENCRYPTION' => $request->input('smtp.encryption') ?? '',
'MAIL_FROM_ADDRESS' => $request->input('smtp.email') ?? '"${MAIL_USERNAME}"',
'MAIL_FROM_NAME' => $request->input('smtp.email') ?? '"${MAIL_USERNAME}"',
'MAIL_FROM_ADDRESS' => $request->input('smtp.email') ?? $request->input('smtp.username'),
'MAIL_FROM_NAME' => $request->input('smtp.email') ?? $request->input('smtp.username'),
],
'ses' => [
'MAIL_DRIVER' => 'ses',
@@ -109,10 +118,12 @@ class StoreEnvironmentSettingsController extends Controller
'AWS_SESSION_TOKEN' => $request->input('ses.session_token'),
],
'mailgun' => [
'MAIL_DRIVER' => 'mailgun',
'MAILGUN_DOMAIN' => $request->input('mailgun.domain'),
'MAILGUN_SECRET' => $request->input('mailgun.secret'),
'MAILGUN_ENDPOINT' => $request->input('mailgun.endpoint'),
'MAIL_DRIVER' => 'mailgun',
'MAILGUN_DOMAIN' => $request->input('mailgun.domain'),
'MAILGUN_SECRET' => $request->input('mailgun.secret'),
'MAILGUN_ENDPOINT' => $request->input('mailgun.endpoint'),
'MAIL_FROM_ADDRESS' => $request->input('mailgun.sender'),
'MAIL_FROM_NAME' => $request->input('mailgun.sender'),
],
],
'environment' => [