Fixes for EmailEntity

This commit is contained in:
David Bomba 2021-02-16 23:56:12 +11:00
parent 6e4979f2a1
commit a056f95e13
5 changed files with 63 additions and 28 deletions

View File

@ -43,13 +43,13 @@ class GmailTransport extends Transport
{ {
/*We should nest the token in the message and then discard it as needed*/ /*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}"); 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); $this->beforeSendPerformed($message);

View File

@ -16,6 +16,8 @@ use App\Events\Invoice\InvoiceWasEmailed;
use App\Events\Invoice\InvoiceWasEmailedAndFailed; use App\Events\Invoice\InvoiceWasEmailedAndFailed;
use App\Jobs\Mail\BaseMailerJob; use App\Jobs\Mail\BaseMailerJob;
use App\Jobs\Mail\EntityFailedSendMailer; use App\Jobs\Mail\EntityFailedSendMailer;
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use App\Libraries\MultiDB; use App\Libraries\MultiDB;
use App\Mail\TemplateEmail; use App\Mail\TemplateEmail;
use App\Models\Activity; use App\Models\Activity;
@ -106,18 +108,29 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue
/* Set the correct mail driver */ /* Set the correct mail driver */
$this->setMailDriver(); $this->setMailDriver();
try { $nmo = new NinjaMailerObject;
Mail::to($this->invitation->contact->email, $this->invitation->contact->present()->name()) $nmo->mailable = new TemplateEmail($this->email_entity_builder,$this->invitation->contact);
->send( $nmo->company = $this->company;
new TemplateEmail( $nmo->settings = $this->settings;
$this->email_entity_builder, $nmo->to_user = $this->invitation->contact;
$this->invitation->contact $nmo->entity_string = $this->entity_string;
) $nmo->invitation = $this->invitation;
); $nmo->reminder_template = $this->reminder_template;
} catch (\Exception $e) {
$this->entityEmailFailed($e->getMessage()); NinjaMailerJob::dispatch($nmo);
$this->logMailError($e->getMessage(), $this->entity->client);
} // 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 */ /* Mark entity sent */
$this->entity->service()->markSent()->save(); $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) private function entityEmailFailed($message)
{ {
switch ($this->entity_string) { switch ($this->entity_string) {

View File

@ -12,10 +12,12 @@
namespace App\Jobs\Mail; namespace App\Jobs\Mail;
use App\DataMapper\Analytics\EmailFailure; use App\DataMapper\Analytics\EmailFailure;
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
use App\Jobs\Mail\NinjaMailerObject; use App\Jobs\Mail\NinjaMailerObject;
use App\Jobs\Util\SystemLogger; use App\Jobs\Util\SystemLogger;
use App\Libraries\Google\Google; use App\Libraries\Google\Google;
use App\Libraries\MultiDB; use App\Libraries\MultiDB;
use App\Mail\TemplateEmail;
use App\Models\ClientContact; use App\Models\ClientContact;
use App\Models\SystemLog; use App\Models\SystemLog;
use App\Models\User; use App\Models\User;
@ -60,9 +62,10 @@ class NinjaMailerJob implements ShouldQueue
if ($this->nmo->company->is_disabled) if ($this->nmo->company->is_disabled)
return true; return true;
/*Set the correct database*/
MultiDB::setDb($this->nmo->company->db); MultiDB::setDb($this->nmo->company->db);
//if we need to set an email driver do it now /* Set the email driver */
$this->setMailDriver(); $this->setMailDriver();
//send email //send email
@ -71,12 +74,29 @@ class NinjaMailerJob implements ShouldQueue
Mail::to($this->nmo->to_user->email) Mail::to($this->nmo->to_user->email)
->send($this->nmo->mailable); ->send($this->nmo->mailable);
} catch (\Exception $e) { } catch (\Exception $e) {
//$this->failed($e);
nlog("error failed with {$e->getMessage()}"); 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); $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;
}
} }
private function setMailDriver() private function setMailDriver()

View File

@ -29,4 +29,10 @@ class NinjaMailerObject
public $transport; //not yet used public $transport; //not yet used
/* Variable for cascading notifications */
public $entity_string = FALSE;
public $invitation = FALSE;
public $template = FALSE;
} }

View File

@ -20,7 +20,6 @@ use Illuminate\Queue\SerializesModels;
class TemplateEmail extends Mailable class TemplateEmail extends Mailable
{ {
//use Queueable, SerializesModels;
private $build_email; private $build_email;
@ -37,12 +36,6 @@ class TemplateEmail extends Mailable
$this->client = $contact->client; $this->client = $contact->client;
} }
/**
* Build the message.
*
* @return $this
* @throws \Laracasts\Presenter\Exceptions\PresenterException
*/
public function build() public function build()
{ {
$template_name = 'email.template.'.$this->build_email->getTemplate(); $template_name = 'email.template.'.$this->build_email->getTemplate();
@ -79,7 +72,10 @@ class TemplateEmail extends Mailable
'settings' => $settings, 'settings' => $settings,
'company' => $company, 'company' => $company,
'whitelabel' => $this->client->user->account->isPaid() ? true : false, 'whitelabel' => $this->client->user->account->isPaid() ? true : false,
]); ])
->withSwiftMessage(function ($message) use($company){
$message->getHeaders()->addTextHeader('Tag', $company->company_key);
});;
//conditionally attach files //conditionally attach files
if ($settings->pdf_email_attachment !== false && ! empty($this->build_email->getAttachments())) { if ($settings->pdf_email_attachment !== false && ! empty($this->build_email->getAttachments())) {