1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/app/Services/Email/EmailDefaults.php

319 lines
9.1 KiB
PHP
Raw Normal View History

2023-01-15 04:44:23 +01:00
<?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\Services\Email;
use App\DataMapper\EmailTemplateDefaults;
use App\Models\Account;
use App\Models\Company;
use App\Services\Email\EmailObject;
use App\Utils\Ninja;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Mail;
use League\CommonMark\CommonMarkConverter;
use Illuminate\Mail\Attachment;
class EmailDefaults
{
2023-01-15 11:16:10 +01:00
/**
* The settings object for this email
* @var CompanySettings $settings
*/
2023-01-15 04:44:23 +01:00
protected $settings;
2023-01-15 11:16:10 +01:00
/**
* The HTML / Template to use for this email
* @var string $template
*/
2023-01-15 04:44:23 +01:00
private string $template;
2023-01-15 11:16:10 +01:00
/**
* The locale to use for
* translations for this email
*/
2023-01-15 04:44:23 +01:00
private string $locale;
2023-01-15 11:16:10 +01:00
/**
* @param EmailService $email_service The email service class
* @param EmailObject $email_object the email object class
*/
2023-01-15 04:44:23 +01:00
public function __construct(protected EmailService $email_service, public EmailObject $email_object){}
2023-01-15 11:16:10 +01:00
/**
* Entry point for generating
* the defaults for the email object
*
* @return EmailObject $email_object The email object
*/
2023-01-15 04:44:23 +01:00
public function run()
{
$this->settings = $this->email_object->settings;
$this->setLocale()
->setFrom()
->setTemplate()
->setBody()
->setSubject()
->setReplyTo()
->setBcc()
->setAttachments()
->setMetaData()
->setVariables();
return $this->email_object;
}
2023-01-15 11:16:10 +01:00
/**
* Sets the meta data for the Email object
*/
2023-01-15 04:44:23 +01:00
private function setMetaData(): self
{
$this->email_object->company_key = $this->email_service->company->company_key;
$this->email_object->logo = $this->email_service->company->present()->logo();
$this->email_object->signature = $this->email_object->signature ?: $this->settings->email_signature;
$this->email_object->whitelabel = $this->email_object->company->account->isPaid() ? true : false;
return $this;
}
2023-01-15 11:16:10 +01:00
/**
* Sets the locale
*/
2023-01-15 04:44:23 +01:00
private function setLocale(): self
{
if($this->email_object->client)
$this->locale = $this->email_object->client->locale();
elseif($this->email_object->vendor)
$this->locale = $this->email_object->vendor->locale();
else
$this->locale = $this->email_service->company->locale();
App::setLocale($this->locale);
App::forgetInstance('translator');
$t = app('translator');
$t->replace(Ninja::transformTranslations($this->settings));
return $this;
}
2023-01-15 11:16:10 +01:00
/**
* Sets the template
*/
2023-01-15 04:44:23 +01:00
private function setTemplate(): self
{
$this->template = $this->email_object->settings->email_style;
match($this->email_object->settings->email_style){
'light' => $this->template = 'email.template.client',
'dark' => $this->template = 'email.template.client',
'custom' => $this->template = 'email.template.custom',
default => $this->template = 'email.template.client',
};
$this->email_object->html_template = $this->template;
return $this;
}
2023-01-15 11:16:10 +01:00
/**
* Sets the FROM address
*/
2023-01-15 04:44:23 +01:00
private function setFrom(): self
2023-01-22 07:46:56 +01:00
{
if(Ninja::isHosted() && $this->email_object->settings->email_sending_method == 'default'){
$this->email_object->from = new Address(config('mail.from.address'), $this->email_service->company->owner()->name());
return $this;
}
2023-01-15 04:44:23 +01:00
if($this->email_object->from)
return $this;
$this->email_object->from = new Address($this->email_service->company->owner()->email, $this->email_service->company->owner()->name());
return $this;
}
2023-01-15 11:16:10 +01:00
/**
* Sets the body of the email
*/
2023-01-15 04:44:23 +01:00
private function setBody(): self
{
if($this->email_object->body){
$this->email_object->body = $this->email_object->body;
}
elseif(strlen($this->email_object->settings->{$this->email_object->email_template_body}) > 3){
$this->email_object->body = $this->email_object->settings->{$this->email_object->email_template_body};
}
else{
$this->email_object->body = EmailTemplateDefaults::getDefaultTemplate($this->email_object->email_template_body, $this->locale);
}
if($this->template == 'email.template.custom'){
$this->email_object->body = (str_replace('$body', $this->email_object->body, $this->email_object->settings->email_style_custom));
}
return $this;
}
2023-01-15 11:16:10 +01:00
/**
* Sets the subject of the email
*/
2023-01-15 04:44:23 +01:00
private function setSubject(): self
{
if ($this->email_object->subject) //where the user updates the subject from the UI
return $this;
elseif(strlen($this->email_object->settings->{$this->email_object->email_template_subject}) > 3)
$this->email_object->subject = $this->email_object->settings->{$this->email_object->email_template_subject};
else
$this->email_object->subject = EmailTemplateDefaults::getDefaultTemplate($this->email_object->email_template_subject, $this->locale);
return $this;
}
2023-01-15 11:16:10 +01:00
/**
* Sets the reply to of the email
*/
private function setReplyTo(): self
2023-01-15 04:44:23 +01:00
{
2023-01-15 11:16:10 +01:00
$reply_to_email = str_contains($this->email_object->settings->reply_to_email, "@") ? $this->email_object->settings->reply_to_email : $this->email_service->company->owner()->email;
2023-01-15 04:44:23 +01:00
2023-01-15 11:16:10 +01:00
$reply_to_name = strlen($this->email_object->settings->reply_to_name) > 3 ? $this->email_object->settings->reply_to_name : $this->email_service->company->owner()->present()->name();
$this->email_object->reply_to = array_merge($this->email_object->reply_to, [new Address($reply_to_email, $reply_to_name)]);
2023-01-15 04:44:23 +01:00
return $this;
}
2023-01-15 11:16:10 +01:00
/**
* Replaces the template placeholders
* with variable values.
*/
public function setVariables(): self
2023-01-15 04:44:23 +01:00
{
2023-01-15 11:16:10 +01:00
$this->email_object->body = strtr($this->email_object->body, $this->email_object->variables);
$this->email_object->subject = strtr($this->email_object->subject, $this->email_object->variables);
2023-01-15 04:44:23 +01:00
2023-01-15 11:16:10 +01:00
if($this->template != 'custom')
$this->email_object->body = $this->parseMarkdownToHtml($this->email_object->body);
2023-01-15 04:44:23 +01:00
return $this;
}
2023-01-15 11:16:10 +01:00
/**
* Sets the BCC of the email
*/
2023-01-15 04:44:23 +01:00
private function setBcc(): self
{
$bccs = [];
$bcc_array = [];
if (strlen($this->email_object->settings->bcc_email) > 1) {
if (Ninja::isHosted() && $this->email_service->company->account->isPaid()) {
$bccs = array_slice(explode(',', str_replace(' ', '', $this->email_object->settings->bcc_email)), 0, 2);
} else {
$bccs(explode(',', str_replace(' ', '', $this->email_object->settings->bcc_email)));
}
}
foreach($bccs as $bcc)
{
$bcc_array[] = new Address($bcc);
}
$this->email_object->bcc = array_merge($this->email_object->bcc, $bcc_array);
return $this;
}
2023-01-15 11:16:10 +01:00
/**
* Sets the CC of the email
* @todo at some point....
*/
2023-01-15 04:44:23 +01:00
private function buildCc()
{
return [
];
}
2023-01-15 11:16:10 +01:00
/**
* Sets the attachments for the email
*
* Note that we base64 encode these, as they
* sometimes may not survive serialization.
*
* We decode these in the Mailable later
*/
2023-01-15 04:44:23 +01:00
private function setAttachments(): self
{
$attachments = [];
if ($this->email_object->settings->document_email_attachment && $this->email_service->company->account->hasFeature(Account::FEATURE_DOCUMENTS)) {
foreach ($this->email_service->company->documents as $document) {
$attachments[] = ['file' => base64_encode($document->getFile()), 'name' => $document->name];
}
}
$this->email_object->attachments = array_merge($this->email_object->attachments, $attachments);
return $this;
}
2023-01-15 11:16:10 +01:00
/**
* Sets the headers for the email
*/
2023-01-15 04:44:23 +01:00
private function setHeaders(): self
{
if($this->email_object->invitation_key)
$this->email_object->headers = array_merge($this->email_object->headers, ['x-invitation-key' => $this->email_object->invitation_key]);
return $this;
}
2023-01-15 11:16:10 +01:00
/**
* Converts any markdown to HTML in the email
*
* @param string $markdown The body to convert
* @return string The parsed markdown response
*/
private function parseMarkdownToHtml(string $markdown): ?string
2023-01-15 04:44:23 +01:00
{
$converter = new CommonMarkConverter([
'allow_unsafe_links' => false,
]);
return $converter->convert($markdown);
}
}