Fixes for mailable

This commit is contained in:
David Bomba 2023-02-15 11:37:14 +11:00
parent a4f2d40d75
commit 453042f7e8
4 changed files with 26 additions and 10 deletions

View File

@ -23,6 +23,7 @@ use App\Models\Invoice;
use App\Models\PurchaseOrder;
use App\Models\Quote;
use App\Models\RecurringInvoice;
use App\Services\Email\MailEntity;
use App\Services\Email\MailObject;
use App\Transformers\CreditTransformer;
use App\Transformers\InvoiceTransformer;
@ -129,6 +130,10 @@ class EmailController extends BaseController
];
$mo = new MailObject;
$mo->subject = empty($subject) ? null : $subject;
$mo->body = empty($body) ? null : $body;
$mo->entity_string = $entity;
$mo->email_template = $template;
if(Ninja::isHosted() && !$entity_obj->company->account->account_sms_verified)
return response(['message' => 'Please verify your account to send emails.'], 400);
@ -137,12 +142,14 @@ class EmailController extends BaseController
return $this->sendPurchaseOrder($entity_obj, $data, $template);
}
$entity_obj->invitations->each(function ($invitation) use ($data, $entity_string, $entity_obj, $template) {
$entity_obj->invitations->each(function ($invitation) use ($data, $entity_string, $entity_obj, $template, $mo) {
if (! $invitation->contact->trashed() && $invitation->contact->email) {
$entity_obj->service()->markSent()->save();
EmailEntity::dispatch($invitation->fresh(), $invitation->company, $template, $data);
// EmailEntity::dispatch($invitation->fresh(), $invitation->company, $template, $data);
MailEntity::dispatch($invitation, $invitation->company->db, $mo);
}
});

View File

@ -286,8 +286,8 @@ class MailBuild
if ($this->mail_entity->mail_object->subject) //where the user updates the subject from the UI
return $this;
elseif(is_string($this->mail_entity->mail_object->email_template_subject) && strlen($this->settings->{$this->mail_entity->mail_object->email_template_subject}) > 3)
$this->mail_entity->mail_object->subject = $this->settings->{$this->mail_entity->mail_object->email_template_subject};
elseif(is_string($this->mail_entity->mail_object->email_template) && strlen($this->settings->{$this->resolveBaseEntityTemplate()}) > 3)
$this->mail_entity->mail_object->subject = $this->settings->{$this->resolveBaseEntityTemplate()};
else
$this->mail_entity->mail_object->subject = EmailTemplateDefaults::getDefaultTemplate($this->resolveBaseEntityTemplate(), $this->locale);
@ -306,8 +306,8 @@ class MailBuild
if($this->mail_entity->mail_object->body){
$this->mail_entity->mail_object->body = $this->mail_entity->mail_object->body;
}
elseif(is_string($this->mail_entity->mail_object->email_template_body) && strlen($this->settings->{$this->mail_entity->mail_object->email_template_body}) > 3){
$this->mail_entity->mail_object->body = $this->settings->{$this->mail_entity->mail_object->email_template_body};
elseif(is_string($this->mail_entity->mail_object->email_template) && strlen($this->settings->{$this->resolveBaseEntityTemplate('body')}) > 3){
$this->mail_entity->mail_object->body = $this->settings->{$this->resolveBaseEntityTemplate('body')};
}
else{
$this->mail_entity->mail_object->body = EmailTemplateDefaults::getDefaultTemplate($this->resolveBaseEntityTemplate('body'), $this->locale);
@ -330,6 +330,17 @@ class MailBuild
*/
private function resolveBaseEntityTemplate(string $type = 'subject'): string
{
if($this->mail_entity->mail_object->email_template){
match($type){
'subject' => $template = "email_subject_{$this->mail_entity->mail_object->email_template}",
'body' => $template = "email_template_{$this->mail_entity->mail_object->email_template}",
default => $template = "email_template_invoice",
};
return $template;
}
//handle statements being emailed
//handle custom templates these types won't have a resolvable entity_string
if(!$this->mail_entity->mail_object->entity_string)

View File

@ -57,7 +57,7 @@ class MailMailable extends Mailable
view: $this->mail_object->html_template,
text: $this->mail_object->text_template,
with: [
'text_body' => strip_tags($this->mail_object->body), //@todo this is a bit hacky here.
'text_body' => str_replace("<br>","\n", strip_tags($this->mail_object->body,"<br>")), //@todo this is a bit hacky here.
'body' => $this->mail_object->body,
'settings' => $this->mail_object->settings,
'whitelabel' => $this->mail_object->whitelabel,

View File

@ -62,9 +62,7 @@ class MailObject
public ?int $vendor_contact_id = null;
public ?string $email_template_body = null;
public ?string $email_template_subject = null;
public ?string $email_template = null; //this defines the template in short notation WITH the email_template prefix
public ?string $html_template = null;