1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Mail/TemplateEmail.php
Shift 19080933b6
Apply Laravel coding style
Shift automatically applies the Laravel coding style - which uses the PSR-2 coding style as a base with some minor additions.

You may customize the code style applied by adding a [PHP CS Fixer][1] or [PHP CodeSniffer][2] ruleset to your project root. Feel free to use [Shift's Laravel ruleset][3] to help you get started.

For more information on customizing the code style applied by Shift, [watch this short video][4].

[1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer
[2]: https://github.com/squizlabs/PHP_CodeSniffer
[3]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200
[4]: https://laravelshift.com/videos/shift-code-style
2022-06-21 09:57:17 +00:00

146 lines
5.1 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Mail;
use App\Jobs\Invoice\CreateUbl;
use App\Models\Account;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\User;
use App\Services\PdfMaker\Designs\Utilities\DesignHelpers;
use App\Utils\HtmlEngine;
use App\Utils\Ninja;
use App\Utils\TemplateEngine;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class TemplateEmail extends Mailable
{
private $build_email;
private $client;
private $contact;
private $company;
private $invitation;
public function __construct($build_email, ClientContact $contact, $invitation = null)
{
$this->build_email = $build_email;
$this->contact = $contact;
$this->client = $contact->client;
$this->company = $contact->company;
$this->invitation = $invitation;
}
public function build()
{
$template_name = 'email.template.'.$this->build_email->getTemplate();
if ($this->build_email->getTemplate() == 'light' || $this->build_email->getTemplate() == 'dark') {
$template_name = 'email.template.client';
}
if ($this->build_email->getTemplate() == 'custom') {
$this->build_email->setBody(str_replace('$body', $this->build_email->getBody(), $this->client->getSetting('email_style_custom')));
}
$settings = $this->client->getMergedSettings();
if ($this->build_email->getTemplate() !== 'custom') {
$this->build_email->setBody(
DesignHelpers::parseMarkdownToHtml($this->build_email->getBody())
);
}
$company = $this->client->company;
if ($this->invitation) {
$html_variables = (new HtmlEngine($this->invitation))->makeValues();
$signature = str_replace(array_keys($html_variables), array_values($html_variables), $settings->email_signature);
} else {
$signature = $settings->email_signature;
}
if (property_exists($settings, 'email_from_name') && strlen($settings->email_from_name) > 1) {
$email_from_name = $settings->email_from_name;
} else {
$email_from_name = $this->company->present()->name();
}
$this->from(config('mail.from.address'), $email_from_name);
if (strlen($settings->bcc_email) > 1) {
if (Ninja::isHosted()) {
$bccs = explode(',', str_replace(' ', '', $settings->bcc_email));
$this->bcc(reset($bccs)); //remove whitespace if any has been inserted.
} else {
$this->bcc(explode(',', str_replace(' ', '', $settings->bcc_email)));
}//remove whitespace if any has been inserted.
}
$this->subject($this->build_email->getSubject())
->text('email.template.text', [
'text_body' => $this->build_email->getTextBody(),
'whitelabel' => $this->client->user->account->isPaid() ? true : false,
'settings' => $settings,
])
->view($template_name, [
'greeting' => ctrans('texts.email_salutation', ['name' => $this->contact->present()->name()]),
'body' => $this->build_email->getBody(),
'footer' => $this->build_email->getFooter(),
'view_link' => $this->build_email->getViewLink(),
'view_text' => $this->build_email->getViewText(),
'title' => '',
'signature' => $signature,
'settings' => $settings,
'company' => $company,
'whitelabel' => $this->client->user->account->isPaid() ? true : false,
'logo' => $this->company->present()->logo($settings),
])
->withSwiftMessage(function ($message) use ($company) {
$message->getHeaders()->addTextHeader('Tag', $company->company_key);
$message->invitation = $this->invitation;
});
/*In the hosted platform we need to slow things down a little for Storage to catch up.*/
if (Ninja::isHosted()) {
sleep(1);
}
foreach ($this->build_email->getAttachments() as $file) {
if (is_string($file)) {
$this->attach($file);
} elseif (is_array($file)) {
$this->attach($file['path'], ['as' => $file['name'], 'mime' => $file['mime']]);
}
}
if ($this->invitation && $this->invitation->invoice && $settings->ubl_email_attachment && $this->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) {
$ubl_string = CreateUbl::dispatchNow($this->invitation->invoice);
if ($ubl_string) {
$this->attachData($ubl_string, $this->invitation->invoice->getFileName('xml'));
}
}
return $this;
}
}