mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-26 02:20:39 +00:00
Test mailgun connection before storing credentials into the app
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
<?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 TestMailgunConnectionAction
|
||||
{
|
||||
/**
|
||||
* Throw an Exception if connection isn't successful.
|
||||
*
|
||||
* @return never
|
||||
*/
|
||||
public function __invoke(array $credentials)
|
||||
{
|
||||
try {
|
||||
// Set temporary mail connection
|
||||
config([
|
||||
'mail' => [
|
||||
'driver' => 'mailgun',
|
||||
],
|
||||
'services' => [
|
||||
'mailgun' => [
|
||||
'domain' => $credentials['domain'],
|
||||
'secret' => $credentials['secret'],
|
||||
'endpoint' => $credentials['endpoint'],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
// Send test email
|
||||
Mail::to('test@hi5ve.digital')->send(new TestMail('example@domain.com'));
|
||||
} catch (TransportException | LogicException $error) {
|
||||
abort(
|
||||
response()->json([
|
||||
'type' => 'mailer-connection-error',
|
||||
'title' => 'Mail Connection Error',
|
||||
'message' => $error->getMessage(),
|
||||
], 401)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,31 +2,43 @@
|
||||
namespace Domain\Settings\Actions;
|
||||
|
||||
use Storage;
|
||||
use Aws\S3\Exception\S3Exception;
|
||||
use League\Flysystem\UnableToWriteFile;
|
||||
use Domain\Settings\DTO\S3CredentialsData;
|
||||
|
||||
class TestS3ConnectionAction
|
||||
{
|
||||
public function __invoke(S3CredentialsData $credentials): void
|
||||
{
|
||||
// Set temporary s3 connection
|
||||
config([
|
||||
'filesystems.disks.s3' => [
|
||||
'driver' => 's3',
|
||||
'key' => $credentials->key,
|
||||
'secret' => $credentials->secret,
|
||||
'region' => $credentials->region,
|
||||
'bucket' => $credentials->bucket,
|
||||
'endpoint' => $credentials->endpoint,
|
||||
],
|
||||
]);
|
||||
try {
|
||||
// Set temporary s3 connection
|
||||
config([
|
||||
'filesystems.disks.s3' => [
|
||||
'driver' => 's3',
|
||||
'key' => $credentials->key,
|
||||
'secret' => $credentials->secret,
|
||||
'region' => $credentials->region,
|
||||
'bucket' => $credentials->bucket,
|
||||
'endpoint' => $credentials->endpoint,
|
||||
],
|
||||
]);
|
||||
|
||||
// Try to get files
|
||||
Storage::disk('s3')->allFiles();
|
||||
// Try to get files
|
||||
Storage::disk('s3')->allFiles();
|
||||
|
||||
// Try to create folder
|
||||
Storage::disk('s3')->makeDirectory('s3-test');
|
||||
// Try to create folder
|
||||
Storage::disk('s3')->makeDirectory('s3-test');
|
||||
|
||||
// Delete test folder
|
||||
Storage::disk('s3')->deleteDirectory('s3-test');
|
||||
// Delete test folder
|
||||
Storage::disk('s3')->deleteDirectory('s3-test');
|
||||
} catch (S3Exception | UnableToWriteFile $error) {
|
||||
abort(
|
||||
response()->json([
|
||||
'type' => 's3-connection-error',
|
||||
'title' => 'S3 Connection Error',
|
||||
'message' => $error->getMessage(),
|
||||
], 401)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,57 +1,45 @@
|
||||
<?php
|
||||
namespace Domain\Settings\Controllers;
|
||||
|
||||
use Mail;
|
||||
use Artisan;
|
||||
use Illuminate\Http\Response;
|
||||
use Domain\Settings\Mail\TestMail;
|
||||
use Symfony\Component\Mailer\Exception\LogicException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Domain\Settings\Actions\TestSMTPConnectionAction;
|
||||
use Domain\Settings\Actions\TestMailgunConnectionAction;
|
||||
use Domain\Settings\Requests\StoreEmailCredentialsRequest;
|
||||
use Symfony\Component\Mailer\Exception\TransportException;
|
||||
|
||||
class StoreEmailCredentialsController
|
||||
{
|
||||
public function __construct(
|
||||
private TestMailgunConnectionAction $testMailgunConnection,
|
||||
private TestSMTPConnectionAction $testSMTPConnection,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set new email credentials to .env file
|
||||
*/
|
||||
public function __invoke(StoreEmailCredentialsRequest $request): Response
|
||||
public function __invoke(StoreEmailCredentialsRequest $request): JsonResponse
|
||||
{
|
||||
// Abort in demo mode
|
||||
abort_if(is_demo(), 204, 'Done.');
|
||||
|
||||
if (! app()->runningUnitTests()) {
|
||||
// Test smtp server
|
||||
if ($request->input('mailDriver') === 'smtp') {
|
||||
try {
|
||||
// Get credentials
|
||||
$credentials = [
|
||||
'smtp' => [
|
||||
'driver' => 'smtp',
|
||||
'host' => $request->input('smtp.host'),
|
||||
'port' => $request->input('smtp.port'),
|
||||
'username' => $request->input('smtp.username'),
|
||||
'password' => $request->input('smtp.password'),
|
||||
'encryption' => $request->input('smtp.encryption') ?? '',
|
||||
'from.address' => $request->input('smtp.email') ?? $request->input('smtp.username'),
|
||||
'from.name' => $request->input('smtp.email') ?? $request->input('smtp.username'),
|
||||
],
|
||||
];
|
||||
|
||||
// Set temporary mail connection
|
||||
config(['mail' => $credentials['smtp']]);
|
||||
|
||||
$sender = $request->input('smtp.email') ?? $request->input('smtp.username');
|
||||
|
||||
// Send test email
|
||||
Mail::to('test@hi5ve.digital')->send(new TestMail($sender));
|
||||
} catch (TransportException|LogicException $error) {
|
||||
return response([
|
||||
'type' => 'mailer-connection-error',
|
||||
'title' => 'Mail Connection Error',
|
||||
'message' => $error->getMessage(),
|
||||
], 401);
|
||||
}
|
||||
}
|
||||
// Test email connection
|
||||
match ($request->input('mailDriver')) {
|
||||
'smtp' => ($this->testSMTPConnection)([
|
||||
'host' => $request->input('smtp.host'),
|
||||
'port' => $request->input('smtp.port'),
|
||||
'username' => $request->input('smtp.username'),
|
||||
'password' => $request->input('smtp.password'),
|
||||
'encryption' => $request->input('smtp.encryption') ?? '',
|
||||
'email' => $request->input('smtp.email'),
|
||||
]),
|
||||
'mailgun' => ($this->testMailgunConnection)([
|
||||
'domain' => $request->input('mailgun.domain'),
|
||||
'secret' => $request->input('mailgun.secret'),
|
||||
'endpoint' => $request->input('mailgun.endpoint'),
|
||||
]),
|
||||
};
|
||||
|
||||
$mail = [
|
||||
'log' => [
|
||||
@@ -61,7 +49,7 @@ class StoreEmailCredentialsController
|
||||
'MAIL_DRIVER' => 'postmark',
|
||||
'POSTMARK_TOKEN' => $request->input('postmark.token'),
|
||||
],
|
||||
'smtp' => [
|
||||
'smtp' => [
|
||||
'MAIL_DRIVER' => 'smtp',
|
||||
'MAIL_HOST' => $request->input('smtp.host'),
|
||||
'MAIL_PORT' => $request->input('smtp.port'),
|
||||
@@ -96,6 +84,6 @@ class StoreEmailCredentialsController
|
||||
Artisan::call('config:cache');
|
||||
}
|
||||
|
||||
return response('Done', 204);
|
||||
return response()->json('Done', 204);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user