2024-02-16 03:38:22 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2024-02-16 03:38:22 +01:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2024-02-16 20:39:33 +01:00
|
|
|
use App\Http\Requests\Smtp\CheckSmtpRequest;
|
2024-02-16 03:38:22 +01:00
|
|
|
use App\Mail\TestMailServer;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
|
|
|
class SmtpController extends BaseController
|
|
|
|
{
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2024-02-16 20:39:33 +01:00
|
|
|
public function check(CheckSmtpRequest $request)
|
2024-02-16 03:38:22 +01:00
|
|
|
{
|
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
$company = $user->company();
|
|
|
|
|
2024-02-29 04:02:32 +01:00
|
|
|
$smtp_host = $request->input('smtp_host', $company->smtp_host);
|
|
|
|
$smtp_port = $request->input('smtp_port', $company->smtp_port);
|
|
|
|
$smtp_username = $request->input('smtp_username', $company->smtp_username);
|
|
|
|
$smtp_password = $request->input('smtp_password', $company->smtp_password);
|
|
|
|
$smtp_encryption = $request->input('smtp_encryption', $company->smtp_encryption ?? 'tls');
|
|
|
|
$smtp_local_domain = $request->input('smtp_local_domain', strlen($company->smtp_local_domain) > 2 ? $company->smtp_local_domain : null);
|
|
|
|
$smtp_verify_peer = $request->input('verify_peer', $company->smtp_verify_peer ?? true);
|
|
|
|
|
2024-02-16 03:38:22 +01:00
|
|
|
config([
|
|
|
|
'mail.mailers.smtp' => [
|
|
|
|
'transport' => 'smtp',
|
2024-02-29 04:02:32 +01:00
|
|
|
'host' => $smtp_host,
|
|
|
|
'port' => $smtp_port,
|
|
|
|
'username' => $smtp_username,
|
|
|
|
'password' => $smtp_password,
|
|
|
|
'encryption' => $smtp_encryption,
|
|
|
|
'local_domain' => $smtp_local_domain,
|
|
|
|
'verify_peer' => $smtp_verify_peer,
|
2024-02-16 03:38:22 +01:00
|
|
|
'timeout' => 5,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
(new \Illuminate\Mail\MailServiceProvider(app()))->register();
|
|
|
|
|
|
|
|
try {
|
2024-03-17 23:03:34 +01:00
|
|
|
|
|
|
|
$sending_email = (isset($company->settings->custom_sending_email) && stripos($company->settings->custom_sending_email, "@")) ? $company->settings->custom_sending_email : $user->email;
|
|
|
|
$sending_user = (isset($company->settings->email_from_name) && strlen($company->settings->email_from_name) > 2) ? $company->settings->email_from_name : $user->name();
|
|
|
|
|
|
|
|
$mailable = new TestMailServer('Email Server Works!', $sending_email);
|
|
|
|
$mailable->from($sending_email,$sending_user);
|
|
|
|
|
|
|
|
Mail::mailer('smtp')
|
|
|
|
->to($user->email, $user->present()->name())
|
|
|
|
->send($mailable);
|
|
|
|
|
2024-02-16 03:38:22 +01:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
app('mail.manager')->forgetMailers();
|
|
|
|
return response()->json(['message' => $e->getMessage()], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
app('mail.manager')->forgetMailers();
|
|
|
|
|
|
|
|
return response()->json(['message' => 'Ok'], 200);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|