1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/Services/Email/EmailMailable.php

132 lines
4.3 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
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2023-01-15 04:44:23 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Email;
use App\Utils\Ninja;
use App\Models\Document;
2024-01-25 10:33:47 +01:00
use Illuminate\Support\Str;
2023-03-18 08:24:56 +01:00
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Attachment;
use Illuminate\Support\Facades\URL;
2024-01-25 10:33:47 +01:00
use Illuminate\Support\Facades\Cache;
2023-01-15 04:44:23 +01:00
use Illuminate\Mail\Mailables\Content;
2023-03-18 08:24:56 +01:00
use Illuminate\Mail\Mailables\Headers;
use Illuminate\Mail\Mailables\Envelope;
2023-01-15 04:44:23 +01:00
class EmailMailable extends Mailable
{
2023-03-09 00:38:08 +01:00
public int $max_attachment_size = 3000000;
2023-01-15 04:44:23 +01:00
/**
* Create a new message instance.
*
* @return void
*/
2023-02-16 02:36:09 +01:00
public function __construct(public EmailObject $email_object)
{
}
2023-01-15 04:44:23 +01:00
/**
* Get the message envelope.
*
* @return \Illuminate\Mail\Mailables\Envelope
*/
public function envelope()
2023-03-15 23:23:24 +01:00
{
2023-01-15 04:44:23 +01:00
return new Envelope(
2023-03-18 08:24:56 +01:00
subject: str_replace("<br>", "", $this->email_object->subject),
2023-01-15 04:44:23 +01:00
tags: [$this->email_object->company_key],
replyTo: $this->email_object->reply_to,
from: $this->email_object->from,
to: $this->email_object->to,
bcc: $this->email_object->bcc,
cc: $this->email_object->cc,
2023-01-15 04:44:23 +01:00
);
}
/**
* Get the message content definition.
*
* @return \Illuminate\Mail\Mailables\Content
*/
public function content()
{
2023-08-06 09:03:12 +02:00
$links = Document::query()->whereIn('id', $this->email_object->documents)
2023-03-09 00:38:08 +01:00
->where('size', '>', $this->max_attachment_size)
->cursor()
2023-03-18 08:24:56 +01:00
->map(function ($document) {
2024-01-25 10:33:47 +01:00
$hash = Str::random(64);
Cache::put($hash, ['db' => $this->email_object->company->db, 'doc_hash' => $document->hash], now()->addDays(7));
return "<a class='doc_links' href='" . URL::signedRoute('documents.hashed_download', ['hash' => $hash]) ."'>". $document->name ."</a>";
2023-03-18 08:24:56 +01:00
});
2023-03-09 00:38:08 +01:00
2023-01-15 04:44:23 +01:00
return new Content(
2023-01-15 11:20:16 +01:00
view: $this->email_object->html_template,
2023-01-15 04:44:23 +01:00
text: $this->email_object->text_template,
with: [
'text_body' => $this->email_object->text_body, //@todo this is a bit hacky here.
2023-01-15 04:44:23 +01:00
'body' => $this->email_object->body,
'settings' => $this->email_object->settings,
'whitelabel' => $this->email_object->whitelabel,
'logo' => $this->email_object->logo,
'signature' => $this->email_object->signature,
'company' => $this->email_object->company,
2023-03-09 00:38:08 +01:00
'greeting' => '',
'links' => array_merge($this->email_object->links, $links->toArray()),
2024-02-13 05:25:18 +01:00
'email_preferences' => (Ninja::isHosted() && in_array($this->email_object->settings->email_sending_method, ['default', 'mailgun']) && $this->email_object->invitation)
? URL::signedRoute('client.email_preferences', ['entity' => $this->email_object->invitation->getEntityString(), 'invitation_key' => $this->email_object->invitation->key])
: false,
2023-01-15 04:44:23 +01:00
]
);
}
/**
* Get the attachments for the message.
*
* @return array
*/
public function attachments()
{
$attachments = [];
2023-03-18 08:24:56 +01:00
$attachments = collect($this->email_object->attachments)->map(function ($file) {
2023-03-09 00:38:08 +01:00
return Attachment::fromData(fn () => base64_decode($file['file']), $file['name']);
});
2023-08-06 09:03:12 +02:00
$documents = Document::query()->whereIn('id', $this->email_object->documents)
2023-03-09 00:38:08 +01:00
->where('size', '<', $this->max_attachment_size)
2023-10-26 04:57:44 +02:00
->where('is_public', 1)
2023-03-09 00:38:08 +01:00
->cursor()
2023-03-18 08:24:56 +01:00
->map(function ($document) {
return Attachment::fromData(fn () => $document->getFile(), $document->name);
});
2023-03-09 00:38:08 +01:00
return $attachments->merge($documents)->toArray();
2023-01-15 04:44:23 +01:00
}
2024-01-14 05:05:00 +01:00
2023-01-15 04:44:23 +01:00
/**
* Get the message headers.
*
* @return \Illuminate\Mail\Mailables\Headers
*/
public function headers()
{
return new Headers(
messageId: null,
references: [],
text: $this->email_object->headers,
);
}
}