invoiceninja/app/Services/Email/MailBuild.php

571 lines
19 KiB
PHP
Raw Normal View History

2023-02-14 10:03:54 +11:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Email;
2023-02-16 12:36:09 +11:00
use App\DataMapper\EmailTemplateDefaults;
use App\Jobs\Entity\CreateRawPdf;
use App\Jobs\Vendor\CreatePurchaseOrderPdf;
use App\Models\Account;
2023-02-14 15:33:55 +11:00
use App\Models\Client;
2023-02-16 12:36:09 +11:00
use App\Models\ClientContact;
2023-02-15 11:04:47 +11:00
use App\Models\Credit;
2023-02-15 00:30:16 +11:00
use App\Models\Expense;
2023-02-15 11:04:47 +11:00
use App\Models\Invoice;
use App\Models\PurchaseOrder;
2023-02-16 12:36:09 +11:00
use App\Models\Quote;
use App\Models\Task;
use App\Models\Vendor;
2023-02-14 23:31:12 +11:00
use App\Models\VendorContact;
2023-02-16 12:36:09 +11:00
use App\Utils\HtmlEngine;
use App\Utils\Ninja;
2023-02-15 11:04:47 +11:00
use App\Utils\Traits\MakesHash;
2023-02-14 23:31:12 +11:00
use App\Utils\VendorHtmlEngine;
2023-02-16 12:36:09 +11:00
use Illuminate\Contracts\Mail\Mailable;
use Illuminate\Mail\Mailables\Address;
2023-02-14 15:33:55 +11:00
use Illuminate\Support\Facades\App;
2023-02-15 00:01:55 +11:00
use Illuminate\Support\Facades\URL;
2023-02-14 21:04:07 +11:00
use League\CommonMark\CommonMarkConverter;
2023-02-14 15:33:55 +11:00
2023-02-14 10:03:54 +11:00
class MailBuild
{
2023-02-15 00:30:16 +11:00
use MakesHash;
2023-02-16 12:36:09 +11:00
/** @var mixed $settings */
2023-02-14 15:33:55 +11:00
protected $settings;
2023-02-14 21:04:07 +11:00
2023-02-15 00:01:55 +11:00
/** @var string $template */
2023-02-14 15:33:55 +11:00
private string $template;
2023-02-14 21:04:07 +11:00
2023-02-15 00:01:55 +11:00
/** @var string $locale */
2023-02-14 15:33:55 +11:00
private string $locale;
2023-02-14 21:04:07 +11:00
2023-02-15 00:01:55 +11:00
/** @var ?Client $client */
2023-02-14 15:33:55 +11:00
private ?Client $client;
2023-02-14 21:04:07 +11:00
2023-02-15 00:01:55 +11:00
/** @var ?Vendor $vendor */
2023-02-14 15:33:55 +11:00
private ?Vendor $vendor;
2023-02-14 23:31:12 +11:00
/** @var mixed $html_engine */
private mixed $html_engine;
2023-02-14 21:04:07 +11:00
2023-02-15 00:01:55 +11:00
/** @var array $variables */
2023-02-14 23:31:12 +11:00
private array $variables = [];
2023-02-15 00:01:55 +11:00
/** @var int $max_attachment_size */
public int $max_attachment_size = 3000000;
2023-02-14 21:04:07 +11:00
/**
* __construct
*
2023-02-15 00:01:55 +11:00
* @param MailEntity $mail_entity
2023-02-14 21:04:07 +11:00
* @return void
*/
2023-02-16 12:36:09 +11:00
public function __construct(public MailEntity $mail_entity)
{
}
2023-02-14 21:04:07 +11:00
/**
* Builds the mailable
*
* @return self
*/
public function run(): self
2023-02-14 15:33:55 +11:00
{
2023-02-14 21:04:07 +11:00
//resolve settings, if client existing - use merged - else default to company
$this->resolveEntities()
->setLocale()
2023-02-14 23:31:12 +11:00
->setMetaData()
2023-02-14 21:04:07 +11:00
->setFrom()
->setTo()
->setTemplate()
->setSubject()
->setBody()
->setReplyTo()
->setBcc()
->setAttachments()
->setVariables();
2023-02-14 15:33:55 +11:00
2023-02-14 21:04:07 +11:00
return $this;
2023-02-14 15:33:55 +11:00
}
2023-02-14 21:04:07 +11:00
/**
* Returns the mailable to the mailer
*
* @return Mailable
*/
public function getMailable(): Mailable
2023-02-14 15:33:55 +11:00
{
2023-02-15 00:01:55 +11:00
return new MailMailable($this->mail_entity->mail_object);
2023-02-14 15:33:55 +11:00
}
2023-02-14 21:04:07 +11:00
/**
* Resolve any class entities
*
* @return self
*/
2023-02-14 15:33:55 +11:00
private function resolveEntities(): self
{
2023-02-14 23:31:12 +11:00
$client_contact = $this->mail_entity?->invitation?->client_contact_id ? ClientContact::withTrashed()->find($this->mail_entity->invitation->client_contact_id) : null;
2023-02-14 15:33:55 +11:00
2023-02-14 23:31:12 +11:00
$this->client = $client_contact?->client;
$vendor_contact = $this->mail_entity?->invitation?->vendor_contact_id ? VendorContact::withTrashed()->find($this->mail_entity->invitation->vendor_contact_id) : null;
2023-02-14 15:33:55 +11:00
2023-02-14 23:31:12 +11:00
$this->vendor = $vendor_contact?->vendor;
2023-02-16 12:36:09 +11:00
if ($this->mail_entity?->invitation) {
if ($this->mail_entity->invitation?->invoice) {
2023-02-15 11:04:47 +11:00
$this->mail_entity->mail_object->entity_string = 'invoice';
$this->mail_entity->mail_object->entity_class = Invoice::class;
}
2023-02-16 12:36:09 +11:00
if ($this->mail_entity->invitation?->quote) {
$this->mail_entity->mail_object->entity_string = 'quote';
2023-02-15 11:04:47 +11:00
$this->mail_entity->mail_object->entity_class = Quote::class;
}
2023-02-16 12:36:09 +11:00
if ($this->mail_entity->invitation?->credit) {
$this->mail_entity->mail_object->entity_string = 'credit';
2023-02-15 11:04:47 +11:00
$this->mail_entity->mail_object->entity_class = Credit::class;
}
2023-02-16 12:36:09 +11:00
if ($this->mail_entity->invitation?->puchase_order) {
$this->mail_entity->mail_object->entity_string = 'purchase_order';
2023-02-15 11:04:47 +11:00
$this->mail_entity->mail_object->entity_class = PurchaseOrder::class;
}
}
2023-02-14 15:33:55 +11:00
return $this;
}
/**
* Sets the locale
2023-02-14 23:31:12 +11:00
* Sets the settings object depending on context
* Sets the HTML variables depending on context
2023-02-14 21:04:07 +11:00
*
* @return self
2023-02-14 15:33:55 +11:00
*/
private function setLocale(): self
{
2023-02-16 12:36:09 +11:00
if ($this->client) {
2023-02-14 21:04:07 +11:00
$this->locale = $this->client->locale();
$this->settings = $this->client->getMergedSettings();
2023-02-14 23:31:12 +11:00
2023-02-16 12:36:09 +11:00
if ($this->mail_entity->invitation) {
2023-02-14 23:31:12 +11:00
$this->variables = (new HtmlEngine($this->mail_entity->invitation))->makeValues();
2023-02-16 12:36:09 +11:00
}
} elseif ($this->vendor) {
2023-02-14 21:04:07 +11:00
$this->locale = $this->vendor->locale();
2023-02-14 23:31:12 +11:00
$this->settings = $this->mail_entity->company->settings;
2023-02-16 12:36:09 +11:00
if ($this->mail_entity->invitation) {
2023-02-14 23:31:12 +11:00
$this->variables = (new VendorHtmlEngine($this->mail_entity->invitation))->makeValues();
2023-02-16 12:36:09 +11:00
}
} else {
2023-02-14 15:33:55 +11:00
$this->locale = $this->mail_entity->company->locale();
2023-02-14 23:31:12 +11:00
$this->settings = $this->mail_entity->company->settings;
}
$this->mail_entity->mail_object->settings = $this->settings;
2023-02-14 15:33:55 +11:00
App::setLocale($this->locale);
App::forgetInstance('translator');
$t = app('translator');
$t->replace(Ninja::transformTranslations($this->settings));
return $this;
}
2023-02-15 00:01:55 +11:00
/**
* Sets the meta data for the Email object
*
* @return self
*/
private function setMetaData(): self
{
$this->mail_entity->mail_object->company_key = $this->mail_entity->company->company_key;
$this->mail_entity->mail_object->logo = $this->mail_entity->company->present()->logo();
$this->mail_entity->mail_object->signature = $this->mail_entity->mail_object->signature ?: $this->settings->email_signature;
$this->mail_entity->mail_object->whitelabel = $this->mail_entity->company->account->isPaid() ? true : false;
$this->mail_entity->mail_object->company = $this->mail_entity->company;
return $this;
}
2023-02-14 15:33:55 +11:00
/**
* Sets the template
2023-02-14 21:04:07 +11:00
*
* @return self
2023-02-14 15:33:55 +11:00
*/
private function setTemplate(): self
{
2023-02-14 21:04:07 +11:00
$this->template = $this->settings->email_style;
2023-02-14 15:33:55 +11:00
2023-02-16 12:36:09 +11:00
match ($this->settings->email_style) {
2023-02-14 15:33:55 +11:00
'light' => $this->template = 'email.template.client',
'dark' => $this->template = 'email.template.client',
'custom' => $this->template = 'email.template.custom',
default => $this->template = 'email.template.client',
2023-02-16 12:36:09 +11:00
};
2023-02-14 15:33:55 +11:00
2023-02-16 12:36:09 +11:00
$this->mail_entity->mail_object->html_template = $this->template;
2023-02-14 15:33:55 +11:00
return $this;
}
2023-02-14 21:04:07 +11:00
/**
* setTo
*
* @return self
*/
private function setTo(): self
{
2023-02-15 00:01:55 +11:00
$this->mail_entity->mail_object->to = array_merge(
2023-02-16 12:36:09 +11:00
$this->mail_entity->mail_object->to,
[new Address($this->mail_entity->invitation->contact->email, $this->mail_entity->invitation->contact->present()->name())]
);
2023-02-15 00:01:55 +11:00
2023-02-14 21:04:07 +11:00
return $this;
}
2023-02-14 15:33:55 +11:00
/**
* Sets the FROM address
2023-02-14 21:04:07 +11:00
*
* @return self
2023-02-14 15:33:55 +11:00
*/
private function setFrom(): self
2023-02-16 12:36:09 +11:00
{
if (Ninja::isHosted() && $this->settings->email_sending_method == 'default') {
2023-02-14 15:33:55 +11:00
$this->mail_entity->mail_object->from = new Address(config('mail.from.address'), $this->mail_entity->company->owner()->name());
return $this;
}
2023-02-16 12:36:09 +11:00
if ($this->mail_entity->mail_object->from) {
2023-02-14 15:33:55 +11:00
return $this;
2023-02-16 12:36:09 +11:00
}
2023-02-14 15:33:55 +11:00
$this->mail_entity->mail_object->from = new Address($this->mail_entity->company->owner()->email, $this->mail_entity->company->owner()->name());
return $this;
}
2023-02-14 21:04:07 +11:00
/**
* Sets the subject of the email
*
* @return self
*/
private function setSubject(): self
{
2023-02-16 12:36:09 +11:00
if ($this->mail_entity->mail_object->subject) { //where the user updates the subject from the UI
2023-02-14 21:04:07 +11:00
return $this;
2023-02-16 12:36:09 +11:00
} elseif (is_string($this->mail_entity->mail_object->email_template) && strlen($this->settings->{$this->resolveBaseEntityTemplate()}) > 3) {
2023-02-15 11:37:14 +11:00
$this->mail_entity->mail_object->subject = $this->settings->{$this->resolveBaseEntityTemplate()};
2023-02-16 12:36:09 +11:00
} else {
2023-02-15 11:04:47 +11:00
$this->mail_entity->mail_object->subject = EmailTemplateDefaults::getDefaultTemplate($this->resolveBaseEntityTemplate(), $this->locale);
2023-02-16 12:36:09 +11:00
}
2023-02-14 21:04:07 +11:00
return $this;
}
/**
2023-02-14 15:33:55 +11:00
* Sets the body of the email
2023-02-14 21:04:07 +11:00
*
* @return self
2023-02-14 15:33:55 +11:00
*/
private function setBody(): self
{
2023-02-16 12:36:09 +11:00
if ($this->mail_entity->mail_object->body) {
2023-02-14 15:33:55 +11:00
$this->mail_entity->mail_object->body = $this->mail_entity->mail_object->body;
2023-02-16 12:36:09 +11:00
} elseif (is_string($this->mail_entity->mail_object->email_template) && strlen($this->settings->{$this->resolveBaseEntityTemplate('body')}) > 3) {
2023-02-15 11:37:14 +11:00
$this->mail_entity->mail_object->body = $this->settings->{$this->resolveBaseEntityTemplate('body')};
2023-02-16 12:36:09 +11:00
} else {
2023-02-15 11:04:47 +11:00
$this->mail_entity->mail_object->body = EmailTemplateDefaults::getDefaultTemplate($this->resolveBaseEntityTemplate('body'), $this->locale);
2023-02-14 15:33:55 +11:00
}
2023-02-16 12:36:09 +11:00
if ($this->template == 'email.template.custom') {
$this->mail_entity->mail_object->body = (str_replace('$body', $this->mail_entity->mail_object->body, $this->settings->email_style_custom));
2023-02-14 15:33:55 +11:00
}
return $this;
}
2023-02-15 11:04:47 +11:00
/**
2023-02-16 12:36:09 +11:00
* Where no template is explicitly passed, we need to infer by the entity type -
2023-02-15 11:04:47 +11:00
* which is hopefully resolvable.
*
* @param string $type
* @return string
*/
private function resolveBaseEntityTemplate(string $type = 'subject'): string
{
2023-02-16 12:36:09 +11:00
if ($this->mail_entity->mail_object->email_template) {
match ($type) {
2023-02-15 11:37:14 +11:00
'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;
}
2023-02-15 11:04:47 +11:00
//handle statements being emailed
//handle custom templates these types won't have a resolvable entity_string
2023-02-16 12:36:09 +11:00
if (!$this->mail_entity->mail_object->entity_string) {
2023-02-15 11:04:47 +11:00
return 'email_template_invoice';
2023-02-16 12:36:09 +11:00
}
2023-02-15 11:04:47 +11:00
2023-02-16 12:36:09 +11:00
match ($type) {
2023-02-15 11:04:47 +11:00
'subject' => $template = "email_subject_{$this->mail_entity->mail_object->entity_string}",
'body' => $template = "email_template_{$this->mail_entity->mail_object->entity_string}",
default => $template = "email_template_invoice",
2023-02-16 12:36:09 +11:00
};
2023-02-15 11:04:47 +11:00
2023-02-16 12:36:09 +11:00
return $template;
2023-02-15 11:04:47 +11:00
}
2023-02-14 15:33:55 +11:00
2023-02-16 12:36:09 +11:00
/**
2023-02-14 21:04:07 +11:00
* Sets the attachments for the email
*
2023-02-16 12:36:09 +11:00
* Note that we base64 encode these, as they
2023-02-14 21:04:07 +11:00
* sometimes may not survive serialization.
*
* We decode these in the Mailable later
2023-02-16 12:36:09 +11:00
*
2023-02-14 21:04:07 +11:00
* @return self
2023-02-16 12:36:09 +11:00
*/
2023-02-14 21:04:07 +11:00
private function setAttachments(): self
2023-02-14 15:33:55 +11:00
{
2023-02-15 00:01:55 +11:00
$this->setContextAttachments();
2023-02-14 15:33:55 +11:00
2023-02-14 21:04:07 +11:00
if ($this->settings->document_email_attachment && $this->mail_entity->company->account->hasFeature(Account::FEATURE_DOCUMENTS)) {
2023-02-15 00:39:38 +11:00
$this->attachDocuments($this->mail_entity->company->documents);
2023-02-14 21:04:07 +11:00
}
2023-02-14 15:33:55 +11:00
return $this;
}
2023-02-15 00:01:55 +11:00
2023-02-15 00:39:38 +11:00
private function attachDocuments($documents): self
{
foreach ($documents as $document) {
2023-02-16 12:36:09 +11:00
if ($document->size > $this->max_attachment_size) {
2023-02-15 00:39:38 +11:00
$this->mail_entity->mail_object->attachment_links = array_merge($this->mail_entity->mail_object->attachment_links, [["<a class='doc_links' href='" . URL::signedRoute('documents.public_download', ['document_hash' => $document->hash]) ."'>". $document->name ."</a>"]]);
2023-02-16 12:36:09 +11:00
} else {
$this->mail_entity->mail_object->attachments = array_merge($this->mail_entity->mail_object->attachments, [['file' => base64_encode($document->getFile()), 'name' => $document->name]]);
}
2023-02-15 00:39:38 +11:00
}
return $this;
}
2023-02-15 00:01:55 +11:00
/**
* Depending on context we may need to resolve the
* attachment dependencies.
2023-02-16 12:36:09 +11:00
*
2023-02-15 00:01:55 +11:00
* ie. Resolve the entity.
* ie. Resolve if we should attach the Entity PDF
* ie. Create the Entity PDF
* ie. Inject the PDF
*
2023-02-15 00:30:16 +11:00
* @return self
2023-02-15 00:01:55 +11:00
*/
private function setContextAttachments(): self
{
2023-02-16 12:36:09 +11:00
if (!$this->settings->pdf_email_attachment || !$this->mail_entity->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) {
2023-02-15 00:30:16 +11:00
return $this;
2023-02-16 12:36:09 +11:00
}
2023-02-15 00:01:55 +11:00
2023-02-16 12:36:09 +11:00
if ($this->mail_entity->invitation?->purchase_order) {
2023-02-15 00:01:55 +11:00
$pdf = (new CreatePurchaseOrderPdf($this->mail_entity->invitation))->rawPdf();
2023-02-15 00:30:16 +11:00
$this->mail_entity->mail_object->attachments = array_merge($this->mail_entity->mail_object->attachments, [['file' => base64_encode($pdf), 'name' => $this->mail_entity->invitation->purchase_order->numberFormatter().'.pdf']]);
2023-02-15 00:01:55 +11:00
if ($this->vendor->getSetting('document_email_attachment') !== false && $this->mail_entity->company->account->hasFeature(Account::FEATURE_DOCUMENTS)) {
2023-02-15 00:39:38 +11:00
$this->attachDocuments($this->mail_entity->invitation->purchase_order->documents);
2023-02-15 00:01:55 +11:00
}
return $this;
}
2023-02-16 12:36:09 +11:00
if (!$this->mail_entity->mail_object->entity_string) {
2023-02-15 11:04:47 +11:00
return $this;
2023-02-16 12:36:09 +11:00
}
2023-02-15 00:30:16 +11:00
2023-02-15 00:01:55 +11:00
$pdf = ((new CreateRawPdf($this->mail_entity->invitation, $this->mail_entity->invitation->company->db))->handle());
2023-02-15 00:30:16 +11:00
2023-02-15 11:04:47 +11:00
$this->mail_entity->mail_object->attachments = array_merge($this->mail_entity->mail_object->attachments, [['file' => base64_encode($pdf), 'name' => $this->mail_entity->invitation->{$this->mail_entity->mail_object->entity_string}->numberFormatter().'.pdf']]);
2023-02-15 00:01:55 +11:00
if ($this->client->getSetting('document_email_attachment') !== false && $this->mail_entity->company->account->hasFeature(Account::FEATURE_DOCUMENTS)) {
2023-02-15 11:04:47 +11:00
$this->attachDocuments($this->mail_entity->invitation->{$this->mail_entity->mail_object->entity_string}->documents);
2023-02-15 00:01:55 +11:00
}
2023-02-16 12:36:09 +11:00
return $this;
2023-02-15 00:30:16 +11:00
2023-02-16 12:36:09 +11:00
if ($this->settings->ubl_email_attachment && $this->mail_entity->mail_object->entity_string == 'invoice') {
}
2023-02-15 00:01:55 +11:00
2023-02-16 12:36:09 +11:00
if ($this->mail_entity->mail_object->entity_string == 'invoice') {
2023-02-15 00:30:16 +11:00
$line_items = $this->mail_entity->invitation->invoice->line_items;
foreach ($line_items as $item) {
$expense_ids = [];
if (property_exists($item, 'expense_id')) {
$expense_ids[] = $item->expense_id;
}
if (count($expense_ids) > 0) {
$expenses = Expense::whereIn('id', $this->transformKeys($expense_ids))
->where('invoice_documents', 1)
->cursor()
->each(function ($expense) {
2023-02-16 12:36:09 +11:00
$this->attachDocuments($expense->documents);
2023-02-15 00:30:16 +11:00
});
}
$task_ids = [];
if (property_exists($item, 'task_id')) {
$task_ids[] = $item->task_id;
}
if (count($task_ids) > 0 && $this->mail_entity->company->invoice_task_documents) {
$tasks = Task::whereIn('id', $this->transformKeys($task_ids))
->cursor()
->each(function ($task) {
2023-02-16 12:36:09 +11:00
$this->attachDocuments($task->documents);
2023-02-15 00:30:16 +11:00
});
}
}
}
2023-02-16 12:36:09 +11:00
return $this;
2023-02-15 00:01:55 +11:00
}
2023-02-14 21:04:07 +11:00
2023-02-14 15:33:55 +11:00
/**
* Sets the reply to of the email
2023-02-16 12:36:09 +11:00
*
2023-02-14 21:04:07 +11:00
* @return self
2023-02-14 15:33:55 +11:00
*/
private function setReplyTo(): self
2023-02-14 10:03:54 +11:00
{
2023-02-14 21:04:07 +11:00
$reply_to_email = str_contains($this->settings->reply_to_email, "@") ? $this->settings->reply_to_email : $this->mail_entity->company->owner()->email;
2023-02-14 15:33:55 +11:00
2023-02-14 21:04:07 +11:00
$reply_to_name = strlen($this->settings->reply_to_name) > 3 ? $this->settings->reply_to_name : $this->mail_entity->company->owner()->present()->name();
2023-02-14 15:33:55 +11:00
$this->mail_entity->mail_object->reply_to = array_merge($this->mail_entity->mail_object->reply_to, [new Address($reply_to_email, $reply_to_name)]);
return $this;
}
/**
2023-02-16 12:36:09 +11:00
* Replaces the template placeholders
2023-02-14 15:33:55 +11:00
* with variable values.
2023-02-16 12:36:09 +11:00
*
2023-02-14 21:04:07 +11:00
* @return self
2023-02-14 15:33:55 +11:00
*/
public function setVariables(): self
{
2023-02-16 12:36:09 +11:00
if ($this->mail_entity->mail_object->variables) {
2023-02-14 21:04:07 +11:00
$this->mail_entity->mail_object->subject = strtr($this->mail_entity->mail_object->subject, $this->mail_entity->mail_object->variables);
$this->mail_entity->mail_object->body = strtr($this->mail_entity->mail_object->body, $this->mail_entity->mail_object->variables);
}
2023-02-14 23:31:12 +11:00
$this->mail_entity->mail_object->subject = strtr($this->mail_entity->mail_object->subject, $this->variables);
$this->mail_entity->mail_object->body = strtr($this->mail_entity->mail_object->body, $this->variables);
2023-02-14 15:33:55 +11:00
2023-02-16 12:36:09 +11:00
if ($this->template != 'custom') {
2023-02-14 15:33:55 +11:00
$this->mail_entity->mail_object->body = $this->parseMarkdownToHtml($this->mail_entity->mail_object->body);
2023-02-16 12:36:09 +11:00
}
2023-02-14 15:33:55 +11:00
return $this;
2023-02-14 10:03:54 +11:00
}
2023-02-14 15:33:55 +11:00
/**
* Sets the BCC of the email
2023-02-16 12:36:09 +11:00
*
2023-02-14 21:04:07 +11:00
* @return self
2023-02-14 15:33:55 +11:00
*/
private function setBcc(): self
{
$bccs = [];
$bcc_array = [];
2023-02-14 21:04:07 +11:00
if (strlen($this->settings->bcc_email) > 1) {
2023-02-14 15:33:55 +11:00
if (Ninja::isHosted() && $this->mail_entity->company->account->isPaid()) {
2023-02-14 21:04:07 +11:00
$bccs = array_slice(explode(',', str_replace(' ', '', $this->settings->bcc_email)), 0, 2);
2023-02-16 12:36:09 +11:00
} elseif (Ninja::isSelfHost()) {
2023-02-14 21:04:07 +11:00
$bccs = (explode(',', str_replace(' ', '', $this->settings->bcc_email)));
2023-02-14 15:33:55 +11:00
}
}
2023-02-16 12:36:09 +11:00
foreach ($bccs as $bcc) {
2023-02-14 15:33:55 +11:00
$bcc_array[] = new Address($bcc);
}
$this->mail_entity->mail_object->bcc = array_merge($this->mail_entity->mail_object->bcc, $bcc_array);
return $this;
}
/**
* Sets the CC of the email
* @todo at some point....
*/
private function buildCc()
{
return [
];
}
/**
* Sets the headers for the email
2023-02-16 12:36:09 +11:00
*
2023-02-14 21:04:07 +11:00
* @return self
2023-02-14 15:33:55 +11:00
*/
private function setHeaders(): self
{
2023-02-16 12:36:09 +11:00
if ($this->mail_entity->mail_object->invitation_key) {
2023-02-14 15:33:55 +11:00
$this->mail_entity->mail_object->headers = array_merge($this->mail_entity->mail_object->headers, ['x-invitation-key' => $this->mail_entity->mail_object->invitation_key]);
2023-02-16 12:36:09 +11:00
} elseif ($this->mail_entity->invitation) {
2023-02-14 21:04:07 +11:00
$this->mail_entity->mail_object->headers = array_merge($this->mail_entity->mail_object->headers, ['x-invitation-key' => $this->mail_entity->invitation->key]);
2023-02-16 12:36:09 +11:00
}
2023-02-14 15:33:55 +11:00
return $this;
}
/**
* Converts any markdown to HTML in the email
2023-02-16 12:36:09 +11:00
*
2023-02-14 15:33:55 +11:00
* @param string $markdown The body to convert
* @return string The parsed markdown response
*/
private function parseMarkdownToHtml(string $markdown): ?string
{
$converter = new CommonMarkConverter([
'allow_unsafe_links' => false,
]);
return $converter->convert($markdown);
}
2023-02-14 10:03:54 +11:00
}