mirror of
https://github.com/crater-invoice/crater.git
synced 2025-10-27 11:41:09 -04:00
Refactor mail-sender
This commit is contained in:
@ -41,14 +41,6 @@ class MailSenderController extends Controller
|
||||
{
|
||||
$this->authorize('create', MailSender::class);
|
||||
|
||||
$mailConfiguration = MailSender::where('company_id', $request->header('company'))
|
||||
->where('is_default', true)
|
||||
->first();
|
||||
|
||||
if ($mailConfiguration && $request['is_default'] == true) {
|
||||
$mailConfiguration->update(['is_default' => false]);
|
||||
}
|
||||
|
||||
$mailSender = MailSender::createFromRequest($request);
|
||||
|
||||
return new MailSenderResource($mailSender);
|
||||
@ -78,15 +70,6 @@ class MailSenderController extends Controller
|
||||
{
|
||||
$this->authorize('update', $mailSender);
|
||||
|
||||
$mailConfiguration = MailSender::where('company_id', $request->header('company'))
|
||||
->where('is_default', true)
|
||||
->where('id', '<>', $mailSender->id)
|
||||
->first();
|
||||
|
||||
if ($mailConfiguration && $request['is_default'] == true) {
|
||||
$mailConfiguration->update(['is_default' => false]);
|
||||
}
|
||||
|
||||
$mailSender->updateFromRequest($request);
|
||||
|
||||
return new MailSenderResource($mailSender);
|
||||
@ -102,6 +85,10 @@ class MailSenderController extends Controller
|
||||
{
|
||||
$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([
|
||||
|
||||
@ -9,6 +9,7 @@ use Crater\Models\CompanySetting;
|
||||
use Crater\Models\Customer;
|
||||
use Crater\Models\EmailLog;
|
||||
use Crater\Models\Estimate;
|
||||
use Crater\Models\MailSender;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class EstimatePdfController extends Controller
|
||||
@ -27,14 +28,16 @@ class EstimatePdfController extends Controller
|
||||
);
|
||||
|
||||
if ($notifyEstimateViewed == 'YES') {
|
||||
$data['estimate'] = Estimate::findOrFail($estimate->id)->toArray();
|
||||
$data['user'] = Customer::find($estimate->customer_id)->toArray();
|
||||
$notificationEmail = CompanySetting::getSetting(
|
||||
'notification_email',
|
||||
$estimate->company_id
|
||||
);
|
||||
$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);
|
||||
|
||||
\Mail::to($notificationEmail)->send(new EstimateViewedMail($data));
|
||||
$data['from_address'] = $mailSender->from_address;
|
||||
$data['from_name'] = $mailSender->from_name;
|
||||
$data['user'] = Customer::find($estimate->customer_id)->toArray();
|
||||
$data['estimate'] = Estimate::findOrFail($estimate->id)->toArray();
|
||||
|
||||
send_mail(new EstimateViewedMail($data), $mailSender, $notificationEmail);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -9,6 +9,7 @@ use Crater\Models\CompanySetting;
|
||||
use Crater\Models\Customer;
|
||||
use Crater\Models\EmailLog;
|
||||
use Crater\Models\Invoice;
|
||||
use Crater\Models\MailSender;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class InvoicePdfController extends Controller
|
||||
@ -28,14 +29,16 @@ class InvoicePdfController extends Controller
|
||||
);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -4,6 +4,7 @@ namespace Crater\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Crater\Models\FileDisk;
|
||||
use Crater\Models\MailSender;
|
||||
|
||||
class ConfigMiddleware
|
||||
{
|
||||
@ -28,6 +29,12 @@ class ConfigMiddleware
|
||||
}
|
||||
}
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,7 +30,7 @@ class EstimateViewedMail extends Mailable
|
||||
*/
|
||||
public function build()
|
||||
{
|
||||
return $this->from(config('mail.from.address'), config('mail.from.name'))
|
||||
return $this->from($this->data['from_address'], $this->data['from_name'])
|
||||
->markdown('emails.viewed.estimate', ['data', $this->data]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,7 +30,7 @@ class InvoiceViewedMail extends Mailable
|
||||
*/
|
||||
public function build()
|
||||
{
|
||||
return $this->from(config('mail.from.address'), config('mail.from.name'))
|
||||
return $this->from($this->data['from_address'], $this->data['from_name'])
|
||||
->markdown('emails.viewed.invoice', ['data', $this->data]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -59,6 +59,10 @@ class MailSender extends Model
|
||||
{
|
||||
$senderMail = self::create($request->getMailSenderPayload());
|
||||
|
||||
if ($request->is_default) {
|
||||
$senderMail->removeOtherDefaultMailSenders($request);
|
||||
}
|
||||
|
||||
return $senderMail;
|
||||
}
|
||||
|
||||
@ -68,6 +72,10 @@ class MailSender extends Model
|
||||
|
||||
$this->update($data);
|
||||
|
||||
if ($request->is_default) {
|
||||
$this->removeOtherDefaultMailSenders($request);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -81,6 +89,9 @@ class MailSender extends Model
|
||||
'address' => $mailSender->from_address,
|
||||
'name' => $mailSender->from_name
|
||||
];
|
||||
$settings['sendmail'] = config('mail.sendmail');
|
||||
$settings['markdown'] = config('mail.markdown');
|
||||
$settings['log_channel'] = config('mail.log_channel');
|
||||
|
||||
Config::set('mail', $settings);
|
||||
|
||||
@ -91,4 +102,10 @@ class MailSender extends Model
|
||||
return true;
|
||||
}
|
||||
|
||||
public function removeOtherDefaultMailSenders($request) {
|
||||
MailSender::where('company_id', $request->header('company'))
|
||||
->where('is_default', true)
|
||||
->where('id', '<>', $this->id)
|
||||
->update(['is_default' => false]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@ use Crater\Models\Currency;
|
||||
use Crater\Models\CustomField;
|
||||
use Crater\Models\Setting;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Mail\Mailable;
|
||||
|
||||
/**
|
||||
* Get company setting
|
||||
@ -70,6 +71,42 @@ function set_active($path, $active = 'active')
|
||||
return call_user_func_array('Request::is', (array)$path) ? $active : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Send Mail
|
||||
*
|
||||
* @param Mailable $mailable
|
||||
* @param object $mailSender
|
||||
* @return string $to
|
||||
*/
|
||||
function send_mail(Mailable $mailable, object $mailSender = null, string $to)
|
||||
{
|
||||
if ($mailSender->bcc && $mailSender->cc) {
|
||||
\Mail::to($to)
|
||||
->bcc(explode(',', $mailSender->bcc))
|
||||
->cc(explode(',', $mailSender->cc))
|
||||
->send($mailable);
|
||||
}
|
||||
|
||||
if ($mailSender->bcc && $mailSender->cc == null) {
|
||||
\Mail::to($to)
|
||||
->bcc(explode(',', $mailSender->bcc))
|
||||
->send($mailable);
|
||||
}
|
||||
|
||||
if ($mailSender->bcc == null && $mailSender->cc) {
|
||||
\Mail::to($to)
|
||||
->cc(explode(',', $mailSender->cc))
|
||||
->send($mailable);
|
||||
}
|
||||
|
||||
if ($mailSender->bcc == null && $mailSender->cc == null) {
|
||||
\Mail::to($to)
|
||||
->send($mailable);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @return mixed
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
|
||||
namespace Crater\Traits;
|
||||
|
||||
use Crater\Mail\EstimateViewedMail;
|
||||
use Crater\Mail\InvoiceViewedMail;
|
||||
use Crater\Mail\SendEstimateMail;
|
||||
use Crater\Mail\SendInvoiceMail;
|
||||
use Crater\Mail\SendPaymentMail;
|
||||
@ -18,45 +20,21 @@ trait MailTrait
|
||||
|
||||
switch ($model) {
|
||||
case 'invoice':
|
||||
$mail = new SendInvoiceMail($data);
|
||||
send_mail(new SendInvoiceMail($data), $mailSender, $data['to']);
|
||||
|
||||
break;
|
||||
|
||||
case 'estimate':
|
||||
$mail = new SendEstimateMail($data);
|
||||
send_mail(new SendEstimateMail($data), $mailSender, $data['to']);
|
||||
|
||||
break;
|
||||
|
||||
case 'payment':
|
||||
$mail = new SendPaymentMail($data);
|
||||
send_mail(new SendPaymentMail($data), $mailSender, $data['to']);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if ($mailSender->bcc && $mailSender->cc) {
|
||||
\Mail::to($data['to'])
|
||||
->bcc(explode(',', $mailSender->bcc))
|
||||
->cc(explode(',', $mailSender->cc))
|
||||
->send($mail);
|
||||
}
|
||||
|
||||
if ($mailSender->bcc && $mailSender->cc == null) {
|
||||
\Mail::to($data['to'])
|
||||
->bcc(explode(',', $mailSender->bcc))
|
||||
->send($mail);
|
||||
}
|
||||
|
||||
if ($mailSender->bcc == null && $mailSender->cc) {
|
||||
\Mail::to($data['to'])
|
||||
->cc(explode(',', $mailSender->cc))
|
||||
->send($mail);
|
||||
}
|
||||
|
||||
if ($mailSender->bcc == null && $mailSender->cc == null) {
|
||||
\Mail::to($data['to'])
|
||||
->send($mail);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,15 +16,6 @@
|
||||
{{ $t('general.edit') }}
|
||||
</BaseDropdownItem>
|
||||
|
||||
<!-- delete mail-sender -->
|
||||
<BaseDropdownItem v-if="!row.is_default" @click="removeMailSender(row.id)">
|
||||
<BaseIcon
|
||||
name="TrashIcon"
|
||||
class="w-5 h-5 mr-3 text-gray-400 group-hover:text-gray-500"
|
||||
/>
|
||||
{{ $t('general.delete') }}
|
||||
</BaseDropdownItem>
|
||||
|
||||
<!-- send test mail-sender -->
|
||||
<BaseDropdownItem @click="openMailSenderTestModal(row.id)">
|
||||
<BaseIcon
|
||||
@ -33,6 +24,15 @@
|
||||
/>
|
||||
{{ $t('general.send_test_mail') }}
|
||||
</BaseDropdownItem>
|
||||
|
||||
<!-- delete mail-sender -->
|
||||
<BaseDropdownItem v-if="!row.is_default" @click="removeMailSender(row.id)">
|
||||
<BaseIcon
|
||||
name="TrashIcon"
|
||||
class="w-5 h-5 mr-3 text-gray-400 group-hover:text-gray-500"
|
||||
/>
|
||||
{{ $t('general.delete') }}
|
||||
</BaseDropdownItem>
|
||||
</BaseDropdown>
|
||||
</template>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user