Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mail sender #1154

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Crater\Http\Controllers\V1\Admin\MailSender;

use Crater\Http\Controllers\Controller;
use Crater\Http\Resources\MailSenderResource;
use Crater\Models\MailSender;
use Illuminate\Http\Request;

class GetAllMailSendersController extends Controller
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function __invoke(Request $request)
{
$mailSenders = MailSender::whereCompany()->get();

return MailSenderResource::collection($mailSenders);
}
}
111 changes: 111 additions & 0 deletions app/Http/Controllers/V1/Admin/MailSender/MailSenderController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Crater\Http\Controllers\V1\Admin\MailSender;

use Crater\Http\Controllers\Controller;
use Crater\Http\Requests\MailSenderRequest;
use Crater\Http\Resources\MailSenderResource;
use Crater\Models\MailSender;
use Illuminate\Http\Request;

class MailSenderController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$this->authorize('viewAny', MailSender::class);

$limit = $request->has('limit') ? $request->limit : 10;

$mailSenders = MailSender::whereCompany()
->applyFilters($request->all())
->paginateData($limit);

return (MailSenderResource::collection($mailSenders))
->additional(['meta' => [
'mail_sender_total_count' => MailSender::whereCompany()->count(),
]]);
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(MailSenderRequest $request)
{
$this->authorize('create', MailSender::class);

$mailConfiguration = MailSender::where('company_id', $request->header('company'))
->where('is_default', true)
->count();

if ($mailConfiguration > 0 && $request['is_default'] == true) {
return respondJson('default_record_exists', 'Default mail sender already exist');
}

$mailSender = MailSender::createFromRequest($request);

return new MailSenderResource($mailSender);
}

/**
* Display the specified resource.
*
* @param \Crater\Models\SenderMail $senderMail
* @return \Illuminate\Http\Response
*/
public function show(MailSender $mailSender)
{
$this->authorize('view', $mailSender);

return new MailSenderResource($mailSender);
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \Crater\Models\SenderMail $senderMail
* @return \Illuminate\Http\Response
*/
public function update(MailSenderRequest $request, MailSender $mailSender)
{
$this->authorize('update', $mailSender);

$mailConfiguration = MailSender::where('company_id', $request->header('company'))
->where('is_default', true)
->where('id', '<>', $mailSender->id)
->count();

if ($mailConfiguration > 0 && $request['is_default'] == true) {
return respondJson('default_record_exists', 'Default mail sender already exist');
}

$mailSender->updateFromRequest($request);

return new MailSenderResource($mailSender);
}

/**
* Remove the specified resource from storage.
*
* @param \Crater\Models\SenderMail $senderMail
* @return \Illuminate\Http\Response
*/
public function destroy(MailSender $mailSender)
{
$this->authorize('delete', $mailSender);

$mailSender->delete();

return response()->json([
'success' => true,
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,29 @@
namespace Crater\Http\Controllers\V1\Admin\Settings;

use Crater\Http\Controllers\Controller;
use Crater\Http\Requests\MailEnvironmentRequest;
use Crater\Http\Requests\TestMailDriverRequest;
use Crater\Mail\TestMail;
use Crater\Models\Setting;
use Crater\Space\EnvironmentManager;
use Illuminate\Http\JsonResponse;
use Crater\Models\MailSender;
use Illuminate\Http\Request;
use Mail;

class MailConfigurationController extends Controller
{
/**
* @var EnvironmentManager
*/
protected $environmentManager;

/**
* @param EnvironmentManager $environmentManager
*/
public function __construct(EnvironmentManager $environmentManager)
{
$this->environmentManager = $environmentManager;
}

/**
*
* @param MailEnvironmentRequest $request
* @return JsonResponse
*/
public function saveMailEnvironment(MailEnvironmentRequest $request)
public function TestMailDriver(TestMailDriverRequest $request)
{
$this->authorize('manage email config');

$setting = Setting::getSetting('profile_complete');
$results = $this->environmentManager->saveMailVariables($request);

if ($setting !== 'COMPLETED') {
Setting::setSetting('profile_complete', 4);
}

return response()->json($results);
}

public function getMailEnvironment()
{
$this->authorize('manage email config');

$MailData = [
'mail_driver' => config('mail.driver'),
'mail_host' => config('mail.host'),
'mail_port' => config('mail.port'),
'mail_username' => config('mail.username'),
'mail_password' => config('mail.password'),
'mail_encryption' => config('mail.encryption'),
'from_name' => config('mail.from.name'),
'from_mail' => config('mail.from.address'),
'mail_mailgun_endpoint' => config('services.mailgun.endpoint'),
'mail_mailgun_domain' => config('services.mailgun.domain'),
'mail_mailgun_secret' => config('services.mailgun.secret'),
'mail_ses_key' => config('services.ses.key'),
'mail_ses_secret' => config('services.ses.secret'),
];
MailSender::setMailConfiguration($request->mail_sender_id);

Mail::to($request->to)->send(new TestMail($request->subject, $request->message));

return response()->json($MailData);
return response()->json([
'success' => true,
]);
}

/**
*
* @return JsonResponse
*/
public function getMailDrivers()
public function getMailDrivers(Request $request)
{
$this->authorize('manage email config');

$drivers = [
'smtp',
'mail',
Expand All @@ -87,21 +36,4 @@ public function getMailDrivers()

return response()->json($drivers);
}

public function testEmailConfig(Request $request)
{
$this->authorize('manage email config');

$this->validate($request, [
'to' => 'required|email',
'subject' => 'required',
'message' => 'required',
]);

Mail::to($request->to)->send(new TestMail($request->subject, $request->message));

return response()->json([
'success' => true,
]);
}
}
85 changes: 85 additions & 0 deletions app/Http/Requests/MailSenderRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Crater\Http\Requests;

use Illuminate\Validation\Rule;
use Illuminate\Foundation\Http\FormRequest;

class MailSenderRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$rules = [
'name' => [
'required',
Rule::unique('mail_senders')
->where('company_id', $this->header('company'))
],
'driver' => [
'required',
],
'is_default' => [
'nullable'
],
'bcc' => [
'nullable'
],
'cc' => [
'nullable'
],
'from_address' => [
'nullable'
],
'from_name' => [
'nullable'
],
'settings' => [
'nullable'
],
'settings.*' => [
'nullable'
]
];

if ($this->isMethod('PUT')) {
$rules['name'] = [
'nullable',
Rule::unique('mail_senders')
->ignore($this->route('mail_sender')->id)
->where('company_id', $this->header('company'))
];
}

return $rules;
}

public function getMailSenderPayload()
{
$data = $this->validated();

if ($data['settings']['encryption'] == 'none') {
$data['settings']['encryption'] = '';
}

return collect($data)
->merge([
'company_id' => $this->header('company'),
])
->toArray();
}
}
2 changes: 1 addition & 1 deletion app/Http/Requests/SendEstimatesRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function rules()
'body' => [
'required',
],
'from' => [
'mail_sender_id' => [
'required',
],
'to' => [
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/SendInvoiceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function rules()
'subject' => [
'required',
],
'from' => [
'mail_sender_id' => [
'required',
],
'to' => [
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/SendPaymentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function rules()
'body' => [
'required',
],
'from' => [
'mail_sender_id' => [
'required',
],
'to' => [
Expand Down
Loading