diff --git a/app/Helpers/Mail/GmailTransport.php b/app/Helpers/Mail/GmailTransport.php index be7e29df6753..e8641e8b8fd1 100644 --- a/app/Helpers/Mail/GmailTransport.php +++ b/app/Helpers/Mail/GmailTransport.php @@ -43,13 +43,13 @@ class GmailTransport extends Transport { /*We should nest the token in the message and then discard it as needed*/ - $token = $message->get('GmailToken'); + $token = $message->getHeaders()->get('GmailToken'); nlog("gmail transporter token = {$token}"); - $message->remove('GmailToken'); + $message->getHeaders()->remove('GmailToken'); - nlog("inside gmail sender with token {$this->token}"); + nlog("inside gmail sender with token {$token}"); $this->beforeSendPerformed($message); diff --git a/app/Jobs/Entity/EmailEntity.php b/app/Jobs/Entity/EmailEntity.php index a84a034ddd78..5711d5dccdef 100644 --- a/app/Jobs/Entity/EmailEntity.php +++ b/app/Jobs/Entity/EmailEntity.php @@ -16,6 +16,8 @@ use App\Events\Invoice\InvoiceWasEmailed; use App\Events\Invoice\InvoiceWasEmailedAndFailed; use App\Jobs\Mail\BaseMailerJob; use App\Jobs\Mail\EntityFailedSendMailer; +use App\Jobs\Mail\NinjaMailerJob; +use App\Jobs\Mail\NinjaMailerObject; use App\Libraries\MultiDB; use App\Mail\TemplateEmail; use App\Models\Activity; @@ -106,18 +108,29 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue /* Set the correct mail driver */ $this->setMailDriver(); - try { - Mail::to($this->invitation->contact->email, $this->invitation->contact->present()->name()) - ->send( - new TemplateEmail( - $this->email_entity_builder, - $this->invitation->contact - ) - ); - } catch (\Exception $e) { - $this->entityEmailFailed($e->getMessage()); - $this->logMailError($e->getMessage(), $this->entity->client); - } + $nmo = new NinjaMailerObject; + $nmo->mailable = new TemplateEmail($this->email_entity_builder,$this->invitation->contact); + $nmo->company = $this->company; + $nmo->settings = $this->settings; + $nmo->to_user = $this->invitation->contact; + $nmo->entity_string = $this->entity_string; + $nmo->invitation = $this->invitation; + $nmo->reminder_template = $this->reminder_template; + + NinjaMailerJob::dispatch($nmo); + + // try { + // Mail::to($this->invitation->contact->email, $this->invitation->contact->present()->name()) + // ->send( + // new TemplateEmail( + // $this->email_entity_builder, + // $this->invitation->contact + // ) + // ); + // } catch (\Exception $e) { + // $this->entityEmailFailed($e->getMessage()); + // $this->logMailError($e->getMessage(), $this->entity->client); + // } /* Mark entity sent */ $this->entity->service()->markSent()->save(); @@ -136,7 +149,7 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue } } - /* Switch statement to handling failure notifications */ + /* Switch statement to handle failure notifications */ private function entityEmailFailed($message) { switch ($this->entity_string) { diff --git a/app/Jobs/Mail/NinjaMailerJob.php b/app/Jobs/Mail/NinjaMailerJob.php index b643055807d5..9a8733dec735 100644 --- a/app/Jobs/Mail/NinjaMailerJob.php +++ b/app/Jobs/Mail/NinjaMailerJob.php @@ -12,10 +12,12 @@ namespace App\Jobs\Mail; use App\DataMapper\Analytics\EmailFailure; +use App\Events\Invoice\InvoiceWasEmailedAndFailed; use App\Jobs\Mail\NinjaMailerObject; use App\Jobs\Util\SystemLogger; use App\Libraries\Google\Google; use App\Libraries\MultiDB; +use App\Mail\TemplateEmail; use App\Models\ClientContact; use App\Models\SystemLog; use App\Models\User; @@ -60,9 +62,10 @@ class NinjaMailerJob implements ShouldQueue if ($this->nmo->company->is_disabled) return true; + /*Set the correct database*/ MultiDB::setDb($this->nmo->company->db); - //if we need to set an email driver do it now + /* Set the email driver */ $this->setMailDriver(); //send email @@ -71,11 +74,28 @@ class NinjaMailerJob implements ShouldQueue Mail::to($this->nmo->to_user->email) ->send($this->nmo->mailable); } catch (\Exception $e) { - //$this->failed($e); + nlog("error failed with {$e->getMessage()}"); - if ($this->nmo->to_user instanceof ClientContact) { + + if ($this->nmo->to_user instanceof ClientContact) $this->logMailError($e->getMessage(), $this->nmo->to_user->client); - } + + if($this->nmo->entity_string) + $this->entityEmailFailed($e->getMessage()); + } + } + + /* Switch statement to handle failure notifications */ + private function entityEmailFailed($message) + { + switch ($this->nmo->entity_string) { + case 'invoice': + event(new InvoiceWasEmailedAndFailed($this->nmo->invitation, $this->nmo->company, $message, $this->nmo->reminder_template, Ninja::eventVars())); + break; + + default: + # code... + break; } } diff --git a/app/Jobs/Mail/NinjaMailerObject.php b/app/Jobs/Mail/NinjaMailerObject.php index 55756083d190..0d8007608b83 100644 --- a/app/Jobs/Mail/NinjaMailerObject.php +++ b/app/Jobs/Mail/NinjaMailerObject.php @@ -29,4 +29,10 @@ class NinjaMailerObject public $transport; //not yet used + /* Variable for cascading notifications */ + public $entity_string = FALSE; + + public $invitation = FALSE; + + public $template = FALSE; } diff --git a/app/Mail/TemplateEmail.php b/app/Mail/TemplateEmail.php index 57a2736c1e45..00d2a08f81c7 100644 --- a/app/Mail/TemplateEmail.php +++ b/app/Mail/TemplateEmail.php @@ -20,7 +20,6 @@ use Illuminate\Queue\SerializesModels; class TemplateEmail extends Mailable { - //use Queueable, SerializesModels; private $build_email; @@ -37,12 +36,6 @@ class TemplateEmail extends Mailable $this->client = $contact->client; } - /** - * Build the message. - * - * @return $this - * @throws \Laracasts\Presenter\Exceptions\PresenterException - */ public function build() { $template_name = 'email.template.'.$this->build_email->getTemplate(); @@ -79,7 +72,10 @@ class TemplateEmail extends Mailable 'settings' => $settings, 'company' => $company, 'whitelabel' => $this->client->user->account->isPaid() ? true : false, - ]); + ]) + ->withSwiftMessage(function ($message) use($company){ + $message->getHeaders()->addTextHeader('Tag', $company->company_key); + });; //conditionally attach files if ($settings->pdf_email_attachment !== false && ! empty($this->build_email->getAttachments())) {