Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

[CI-1130]: Mail sender #3

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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);
}
}
98 changes: 98 additions & 0 deletions app/Http/Controllers/V1/Admin/MailSender/MailSenderController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?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);

$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);

$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);

if ($mailSender->is_default) {
return respondJson('You can\'t remove default mail sender.', 'You can\'t remove default mail sender.');
}

$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,
]);
}
}
15 changes: 9 additions & 6 deletions app/Http/Controllers/V1/Customer/EstimatePdfController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Crater\Models\Customer;
use Crater\Models\EmailLog;
use Crater\Models\Estimate;
use Crater\Models\MailSender;
use Illuminate\Http\Request;

class EstimatePdfController extends Controller
Expand All @@ -27,14 +28,16 @@ public function getPdf(EmailLog $emailLog, Request $request)
);

if ($notifyEstimateViewed == 'YES') {
$data['estimate'] = Estimate::findOrFail($estimate->id)->toArray();
$notificationEmail = CompanySetting::getSetting('notification_email', $estimate->company_id);
$mailSender = MailSender::where('company_id', $estimate->company_id)->where('is_default', true)->first();
MailSender::setMailConfiguration($mailSender->id);

$data['from_address'] = $mailSender->from_address;
$data['from_name'] = $mailSender->from_name;
$data['user'] = Customer::find($estimate->customer_id)->toArray();
$notificationEmail = CompanySetting::getSetting(
'notification_email',
$estimate->company_id
);
$data['estimate'] = Estimate::findOrFail($estimate->id)->toArray();

\Mail::to($notificationEmail)->send(new EstimateViewedMail($data));
send_mail(new EstimateViewedMail($data), $mailSender, $notificationEmail);
}
}

Expand Down
13 changes: 8 additions & 5 deletions app/Http/Controllers/V1/Customer/InvoicePdfController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Crater\Models\Customer;
use Crater\Models\EmailLog;
use Crater\Models\Invoice;
use Crater\Models\MailSender;
use Illuminate\Http\Request;

class InvoicePdfController extends Controller
Expand All @@ -28,14 +29,16 @@ public function getPdf(EmailLog $emailLog, Request $request)
);

if ($notifyInvoiceViewed == 'YES') {
$notificationEmail = CompanySetting::getSetting('notification_email', $invoice->company_id);
$mailSender = MailSender::where('company_id', $invoice->company_id)->where('is_default', true)->first();
MailSender::setMailConfiguration($mailSender->id);

$data['from_address'] = $mailSender->from_address;
$data['from_name'] = $mailSender->from_name;
$data['invoice'] = Invoice::findOrFail($invoice->id)->toArray();
$data['user'] = Customer::find($invoice->customer_id)->toArray();
$notificationEmail = CompanySetting::getSetting(
'notification_email',
$invoice->company_id
);

\Mail::to($notificationEmail)->send(new InvoiceViewedMail($data));
send_mail(new InvoiceViewedMail($data), $mailSender, $notificationEmail);
}
}

Expand Down
7 changes: 7 additions & 0 deletions app/Http/Middleware/ConfigMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Crater\Models\FileDisk;
use Crater\Models\MailSender;

class ConfigMiddleware
{
Expand All @@ -28,6 +29,12 @@ public function handle($request, Closure $next)
}
}

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

if ($default_mail_sender) {
$default_mail_sender->setMailConfiguration($default_mail_sender->id);
}

return $next($request);
}
}
Loading