diff --git a/app/Console/Commands/SendTestEmails.php b/app/Console/Commands/SendTestEmails.php index 439e79563b45..be96efc1a371 100644 --- a/app/Console/Commands/SendTestEmails.php +++ b/app/Console/Commands/SendTestEmails.php @@ -155,10 +155,5 @@ class SendTestEmails extends Command ->setSubject($message['subject']) ->setBody($message['body']); - // Mail::to(config('ninja.testvars.test_email'), 'Mr Test') - // ->cc($cc_emails) - // ->bcc($bcc_emails) - //->replyTo(also_available_if_needed) - //->send(new TemplateEmail($email_builder, $user, $client)); } } diff --git a/app/Http/Controllers/EmailController.php b/app/Http/Controllers/EmailController.php index eac013a5b7c4..3bc064ea40a4 100644 --- a/app/Http/Controllers/EmailController.php +++ b/app/Http/Controllers/EmailController.php @@ -117,52 +117,55 @@ class EmailController extends BaseController $subject = $request->input('subject'); $body = $request->input('body'); $entity_string = strtolower(class_basename($entity_obj)); - $template = $request->input('template'); - $template = str_replace("email_template_", "", $template); + $template = str_replace("email_template_", "", $request->input('template')); + $data = [ + 'subject' => $subject, + 'body' => $body + ]; + + $entity_obj->invitations->each(function ($invitation) use ($data, $entity_string, $entity_obj, $template) { - $entity_obj->invitations->each(function ($invitation) use ($subject, $body, $entity_string, $entity_obj, $template) { if ($invitation->contact->send_email && $invitation->contact->email) { - $data = [ - 'subject' => $subject, - 'body' => $body - ]; $entity_obj->service()->markSent()->save(); + EmailEntity::dispatch($invitation, $invitation->company, $template, $data)->delay(now()->addSeconds(5)); + } + }); $entity_obj->last_sent_date = now(); + $entity_obj->save(); /*Only notify the admin ONCE, not once per contact/invite*/ - if ($entity_obj instanceof Invoice) { $this->entity_type = Invoice::class; $this->entity_transformer = InvoiceTransformer::class; - if ($entity_obj->invitations->count() >= 1) { + if ($entity_obj->invitations->count() >= 1) $entity_obj->entityEmailEvent($entity_obj->invitations->first(), 'invoice', $template); - } + } if ($entity_obj instanceof Quote) { $this->entity_type = Quote::class; $this->entity_transformer = QuoteTransformer::class; - if ($entity_obj->invitations->count() >= 1) { + if ($entity_obj->invitations->count() >= 1) event(new QuoteWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars(), 'quote')); - } + } if ($entity_obj instanceof Credit) { $this->entity_type = Credit::class; $this->entity_transformer = CreditTransformer::class; - if ($entity_obj->invitations->count() >= 1) { + if ($entity_obj->invitations->count() >= 1) event(new CreditWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars(), 'credit')); - } + } if ($entity_obj instanceof RecurringInvoice) { diff --git a/app/Jobs/Entity/EmailEntity.php b/app/Jobs/Entity/EmailEntity.php index 44a6eafd2f5d..7a291470e2c6 100644 --- a/app/Jobs/Entity/EmailEntity.php +++ b/app/Jobs/Entity/EmailEntity.php @@ -40,29 +40,32 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - public $invitation; + public $invitation; //The entity invitation - public $company; + public $company; //The company - public $settings; + public $settings; //The settings object - public $entity_string; + public $entity_string; //The entity string ie. invoice, quote, credit - public $reminder_template; + public $reminder_template; //The base template we are using - public $entity; + public $entity; //The entity object - public $html_engine; + public $html_engine; //The HTMLEngine object - public $email_entity_builder; + public $email_entity_builder; //The email builder which merges the template and text - public $template_data; + public $template_data; //The data to be merged into the template /** * EmailEntity constructor. + * + * * @param Invitation $invitation * @param Company $company * @param ?string $reminder_template + * @param array $template_data */ public function __construct($invitation, Company $company, ?string $reminder_template = null, $template_data = null) { @@ -93,12 +96,14 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue */ public function handle() { - if ($this->company->is_disabled) { + /* Don't fire emails if the company is disabled */ + if ($this->company->is_disabled) return true; - } + /* Set DB */ MultiDB::setDB($this->company->db); + /* Set the correct mail driver */ $this->setMailDriver(); try { @@ -106,7 +111,6 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue ->send( new TemplateEmail( $this->email_entity_builder, - $this->invitation->contact->user, $this->invitation->contact->client ) ); @@ -132,6 +136,7 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue } } + /* Switch statement to handling failure notifications */ private function entityEmailFailed($message) { switch ($this->entity_string) { @@ -145,30 +150,7 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue } } - // private function entityEmailSucceeded() - // { - // switch ($this->reminder_template) { - // case 'invoice': - // event(new InvoiceWasEmailed($this->invitation, $this->company, Ninja::eventVars())); - // break; - // case 'reminder1': - // event(new InvoiceReminderWasEmailed($this->invitation, $this->company, Ninja::eventVars(), Activity::INVOICE_REMINDER1_SENT)); - // break; - // case 'reminder2': - // event(new InvoiceReminderWasEmailed($this->invitation, $this->company, Ninja::eventVars(), Activity::INVOICE_REMINDER2_SENT)); - // break; - // case 'reminder3': - // event(new InvoiceReminderWasEmailed($this->invitation, $this->company, Ninja::eventVars(), Activity::INVOICE_REMINDER3_SENT)); - // break; - // case 'reminder_endless': - // event(new InvoiceReminderWasEmailed($this->invitation, $this->company, Ninja::eventVars(), Activity::INVOICE_REMINDER_ENDLESS_SENT)); - // break; - // default: - // # code... - // break; - // } - // } - + /* Builds the email builder object */ private function resolveEmailBuilder() { $class = 'App\Mail\Engine\\' . ucfirst(Str::camel($this->entity_string)) . "EmailEngine"; diff --git a/app/Jobs/Mail/EntityFailedSendMailer.php b/app/Jobs/Mail/EntityFailedSendMailer.php index 0f330f6f5c9b..3b8cf6be6e08 100644 --- a/app/Jobs/Mail/EntityFailedSendMailer.php +++ b/app/Jobs/Mail/EntityFailedSendMailer.php @@ -77,7 +77,6 @@ class EntityFailedSendMailer extends BaseMailerJob implements ShouldQueue */ public function handle() { - nlog("failed entity sent mailer"); /*If we are migrating data we don't want to fire these notification*/ if ($this->company->is_disabled) diff --git a/app/Jobs/Mail/EntitySentMailer.php b/app/Jobs/Mail/EntitySentMailer.php index b8c5bc988906..76aa2c7cd33b 100644 --- a/app/Jobs/Mail/EntitySentMailer.php +++ b/app/Jobs/Mail/EntitySentMailer.php @@ -72,12 +72,10 @@ class EntitySentMailer extends BaseMailerJob implements ShouldQueue */ public function handle() { - nlog("entity sent mailer"); /*If we are migrating data we don't want to fire these notification*/ - if ($this->company->is_disabled) { + if ($this->company->is_disabled) return true; - } //Set DB MultiDB::setDb($this->company->db); diff --git a/app/Jobs/Payment/EmailPayment.php b/app/Jobs/Payment/EmailPayment.php index c61c3452ed57..4f5c3f1a13d7 100644 --- a/app/Jobs/Payment/EmailPayment.php +++ b/app/Jobs/Payment/EmailPayment.php @@ -80,7 +80,7 @@ class EmailPayment extends BaseMailerJob implements ShouldQueue try { $mail = Mail::to($this->contact->email, $this->contact->present()->name()); - $mail->send(new TemplateEmail($email_builder, $this->contact->user, $this->contact->client)); + $mail->send(new TemplateEmail($email_builder, $this->contact->client)); } catch (\Exception $e) { nlog("mailing failed with message " . $e->getMessage()); event(new PaymentWasEmailedAndFailed($this->payment, $this->company, Mail::failures(), Ninja::eventVars())); diff --git a/app/Listeners/Invoice/InvoiceEmailedNotification.php b/app/Listeners/Invoice/InvoiceEmailedNotification.php index 08e9377dce1d..bb95fd6a50bf 100644 --- a/app/Listeners/Invoice/InvoiceEmailedNotification.php +++ b/app/Listeners/Invoice/InvoiceEmailedNotification.php @@ -41,22 +41,32 @@ class InvoiceEmailedNotification implements ShouldQueue $invoice->last_sent_date = now(); $invoice->save(); + /* We loop through each user and determine whether they need to be notified */ foreach ($event->invitation->company->company_users as $company_user) { + + /* The User */ $user = $company_user->user; + /* This is only here to handle the alternate message channels - ie Slack */ $notification = new EntitySentNotification($event->invitation, 'invoice'); + /* Returns an array of notification methods */ $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'invoice', ['all_notifications', 'invoice_sent']); + /* If one of the methods is email then we fire the EntitySentMailer */ if (($key = array_search('mail', $methods)) !== false && $first_notification_sent === true) { unset($methods[$key]); EntitySentMailer::dispatch($event->invitation, 'invoice', $user, $event->invitation->company, $event->template); + + /* This prevents more than one notification being sent */ $first_notification_sent = false; } + /* Override the methods in the Notification Class */ $notification->method = $methods; + /* Notify on the alternate channels */ $user->notify($notification); } } diff --git a/app/Mail/TemplateEmail.php b/app/Mail/TemplateEmail.php index eaecd1241ee6..42d9a5273c1b 100644 --- a/app/Mail/TemplateEmail.php +++ b/app/Mail/TemplateEmail.php @@ -23,18 +23,12 @@ class TemplateEmail extends Mailable private $build_email; - private $user; //the user the email will be sent from - private $client; - private $footer; - - public function __construct($build_email, User $user, Client $client) + public function __construct($build_email, Client $client) { $this->build_email = $build_email; - $this->user = $user; //this is inappropriate here, need to refactor 'user' in this context the 'user' could also be the 'system' - $this->client = $client; } diff --git a/resources/views/email/admin/generic.blade.php b/resources/views/email/admin/generic.blade.php index c2685697a1a2..0cbbf3144006 100644 --- a/resources/views/email/admin/generic.blade.php +++ b/resources/views/email/admin/generic.blade.php @@ -5,9 +5,9 @@ @endcomponent @endslot -@slot('greeting') - @lang($message) -@endslot +

+ {!! $message !!} +

@component('email.components.button', ['url' => $url]) @lang($button)