1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Mail/TemplateEmail.php

180 lines
6.6 KiB
PHP
Raw Normal View History

<?php
2020-08-06 05:04:09 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2020-08-06 05:04:09 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
2020-08-06 05:04:09 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-08-06 05:04:09 +02:00
*/
namespace App\Mail;
2022-07-27 06:57:17 +02:00
use App\Jobs\Entity\CreateEntityPdf;
2021-08-29 12:54:26 +02:00
use App\Jobs\Invoice\CreateUbl;
use App\Models\Account;
2020-02-15 12:49:31 +01:00
use App\Models\Client;
2021-02-02 09:51:12 +01:00
use App\Models\ClientContact;
2020-02-15 12:49:31 +01:00
use App\Models\User;
2021-04-15 15:56:20 +02:00
use App\Services\PdfMaker\Designs\Utilities\DesignHelpers;
2021-03-31 00:58:50 +02:00
use App\Utils\HtmlEngine;
2021-11-14 22:52:04 +01:00
use App\Utils\Ninja;
2021-04-15 15:56:20 +02:00
use App\Utils\TemplateEngine;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
2022-07-27 06:57:17 +02:00
use Illuminate\Support\Facades\Storage;
class TemplateEmail extends Mailable
{
private $build_email;
2020-02-15 12:49:31 +01:00
private $client;
2021-02-02 06:11:33 +01:00
private $contact;
2021-02-18 22:43:47 +01:00
private $company;
2021-02-22 01:18:52 +01:00
private $invitation;
public function __construct($build_email, ClientContact $contact, $invitation = null)
{
$this->build_email = $build_email;
2020-02-15 12:49:31 +01:00
2021-02-02 06:11:33 +01:00
$this->contact = $contact;
$this->client = $contact->client;
2021-02-18 22:43:47 +01:00
$this->company = $contact->company;
2021-02-22 01:18:52 +01:00
$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') {
2021-04-11 05:46:40 +02:00
$this->build_email->setBody(str_replace('$body', $this->build_email->getBody(), $this->client->getSetting('email_style_custom')));
}
2021-04-19 11:41:56 +02:00
$settings = $this->client->getMergedSettings();
2021-04-22 12:29:00 +02:00
if ($this->build_email->getTemplate() !== 'custom') {
$this->build_email->setBody(
DesignHelpers::parseMarkdownToHtml($this->build_email->getBody())
);
}
2021-04-15 15:56:20 +02:00
2020-02-15 12:49:31 +01:00
$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;
}
2021-04-15 15:56:20 +02:00
if (property_exists($settings, 'email_from_name') && strlen($settings->email_from_name) > 1) {
2021-09-03 14:59:48 +02:00
$email_from_name = $settings->email_from_name;
} else {
2021-09-03 14:59:48 +02:00
$email_from_name = $this->company->present()->name();
}
2021-09-03 14:59:48 +02:00
$this->from(config('mail.from.address'), $email_from_name);
2021-04-15 15:56:20 +02:00
if (strlen($settings->bcc_email) > 1) {
if (Ninja::isHosted()) {
$bccs = explode(',', str_replace(' ', '', $settings->bcc_email));
2022-10-05 23:13:42 +02:00
$this->bcc(array_slice($bccs, 0, 2));
//$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.
2022-06-18 00:54:58 +02:00
}
2021-06-17 14:47:34 +02:00
2020-11-25 15:19:52 +01:00
$this->subject($this->build_email->getSubject())
2022-03-04 00:55:02 +01:00
->text('email.template.text', [
'text_body' => $this->build_email->getTextBody(),
2020-10-20 01:37:33 +02:00
'whitelabel' => $this->client->user->account->isPaid() ? true : false,
'settings' => $settings,
2020-02-15 12:49:31 +01:00
])
->view($template_name, [
2021-02-02 06:11:33 +01:00
'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(),
2020-11-08 06:21:18 +01:00
'title' => '',
'signature' => $signature,
'settings' => $settings,
'company' => $company,
2020-09-28 23:54:12 +02:00
'whitelabel' => $this->client->user->account->isPaid() ? true : false,
'logo' => $this->company->present()->logo($settings),
2022-08-07 09:34:23 +02:00
]);
/*In the hosted platform we need to slow things down a little for Storage to catch up.*/
2022-07-27 06:57:17 +02:00
// if(Ninja::isHosted() && $this->invitation){
2022-07-27 06:57:17 +02:00
// $path = false;
2022-07-27 06:57:17 +02:00
// if($this->invitation->invoice)
// $path = $this->client->invoice_filepath($this->invitation).$this->invitation->invoice->numberFormatter().'.pdf';
// elseif($this->invitation->quote)
// $path = $this->client->quote_filepath($this->invitation).$this->invitation->quote->numberFormatter().'.pdf';
// elseif($this->invitation->credit)
// $path = $this->client->credit_filepath($this->invitation).$this->invitation->credit->numberFormatter().'.pdf';
2022-07-27 06:57:17 +02:00
// sleep(1);
2022-07-31 11:11:32 +02:00
// if($path && !Storage::disk(config('filesystems.default'))->exists($path)){
2022-07-27 06:57:17 +02:00
// sleep(2);
2022-07-27 06:57:17 +02:00
// if(!Storage::disk(config('filesystems.default'))->exists($path)) {
// (new CreateEntityPdf($this->invitation))->handle();
// sleep(2);
// }
2022-07-27 06:57:17 +02:00
// }
2022-07-27 06:57:17 +02:00
// }
2021-01-08 11:19:26 +01:00
// $file = (new CreateRawPdf($invitation, $invitation->company->db))->handle();
//22-10-2022 - Performance - To improve the performance/reliability of sending emails, attaching as Data is much better, stubs in place
foreach ($this->build_email->getAttachments() as $file) {
// if (is_string($file)) {
// // nlog($file);
// // $file_data = file_get_contents($file);
// // $this->attachData($file_data, basename($file));
// $this->attach($file);
// } elseif (is_array($file)) {
// // nlog($file['path']);
// // $file_data = file_get_contents($file['path']);
// // $this->attachData($file_data, $file['name']);
// $this->attach($file['path'], ['as' => $file['name'], 'mime' => null]);
// }
$this->attachData(base64_decode($file['file']), $file['name']);
}
if ($this->invitation && $this->invitation->invoice && $settings->ubl_email_attachment && $this->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) {
$ubl_string = (new CreateUbl($this->invitation->invoice))->handle();
if ($ubl_string) {
2021-09-09 09:23:47 +02:00
$this->attachData($ubl_string, $this->invitation->invoice->getFileName('xml'));
}
2021-08-29 12:54:26 +02:00
}
return $this;
}
}