Attach links instead of files for attachments that are greater than 3mb in size

This commit is contained in:
David Bomba 2023-02-03 09:56:58 +11:00
parent 6d07e78280
commit 46bda415c2
11 changed files with 153 additions and 30 deletions

View File

@ -176,25 +176,17 @@ class NinjaMailerJob implements ShouldQueue
* this merges a text string with a json object * this merges a text string with a json object
* need to harvest the ->Message property using the following * need to harvest the ->Message property using the following
*/ */
if(stripos($e->getMessage(), 'code 406') || stripos($e->getMessage(), 'code 300')) if(stripos($e->getMessage(), 'code 406') || stripos($e->getMessage(), 'code 300') || stripos($e->getMessage(), 'code 413'))
{ {
$message = "Either Attachment too large, or recipient has been suppressed."; $message = "Either Attachment too large, or recipient has been suppressed.";
$this->fail(); $this->fail();
$this->logMailError($e->getMessage(), $this->company->clients()->first());
$this->cleanUpMailers(); $this->cleanUpMailers();
return; return;
// $response = $e->getResponse();
// $message_body = json_decode($response->getBody()->getContents());
// if($message_body && property_exists($message_body, 'Message')){
// $message = $message_body->Message;
// }
/*Do not retry if this is a postmark specific issue such as invalid recipient. */
} }
//only report once, not on all tries //only report once, not on all tries

View File

@ -27,6 +27,8 @@ class BaseEmailEngine implements EngineInterface
public $attachments = []; public $attachments = [];
public $attachment_links = [];
public $link; public $link;
public $text; public $text;
@ -37,6 +39,8 @@ class BaseEmailEngine implements EngineInterface
public $text_footer; public $text_footer;
public int $max_attachment_size = 3000000;
public function setFooter($footer) public function setFooter($footer)
{ {
$this->footer = $footer; $this->footer = $footer;
@ -95,6 +99,14 @@ class BaseEmailEngine implements EngineInterface
return $this; return $this;
} }
public function setAttachmentLinks($links)
{
$this->attachment_links = array_merge($this->getAttachmentLinks(), $links);
return $this;
}
public function setViewLink($link) public function setViewLink($link)
{ {
$this->link = $link; $this->link = $link;
@ -131,6 +143,11 @@ class BaseEmailEngine implements EngineInterface
return $this->attachments; return $this->attachments;
} }
public function getAttachmentLinks()
{
return $this->attachment_links;
}
public function getFooter() public function getFooter()
{ {
return $this->footer; return $this->footer;

View File

@ -18,6 +18,7 @@ use App\Utils\Ninja;
use App\Utils\Number; use App\Utils\Number;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Lang; use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\URL;
class CreditEmailEngine extends BaseEmailEngine class CreditEmailEngine extends BaseEmailEngine
{ {
@ -136,10 +137,18 @@ class CreditEmailEngine extends BaseEmailEngine
// Storage::url // Storage::url
foreach ($this->credit->documents as $document) { foreach ($this->credit->documents as $document) {
if($document->size > $this->max_attachment_size)
$this->setAttachmentLinks(["<a class='doc_links' href='" . URL::signedRoute('documents.public_download', ['document_hash' => $document->hash]) ."'>". $document->name ."</a>"]);
else
$this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]);
} }
foreach ($this->credit->company->documents as $document) { foreach ($this->credit->company->documents as $document) {
if($document->size > $this->max_attachment_size)
$this->setAttachmentLinks(["<a class='doc_links' href='" . URL::signedRoute('documents.public_download', ['document_hash' => $document->hash]) ."'>". $document->name ."</a>"]);
else
$this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]); $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]);
} }
} }

View File

@ -23,6 +23,7 @@ use App\Utils\Number;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Lang; use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\URL;
class InvoiceEmailEngine extends BaseEmailEngine class InvoiceEmailEngine extends BaseEmailEngine
{ {
@ -136,12 +137,21 @@ class InvoiceEmailEngine extends BaseEmailEngine
//attach third party documents //attach third party documents
if ($this->client->getSetting('document_email_attachment') !== false && $this->invoice->company->account->hasFeature(Account::FEATURE_DOCUMENTS)) { if ($this->client->getSetting('document_email_attachment') !== false && $this->invoice->company->account->hasFeature(Account::FEATURE_DOCUMENTS)) {
// Storage::url // Storage::url
foreach ($this->invoice->documents as $document) { foreach ($this->invoice->documents as $document) {
if($document->size > $this->max_attachment_size)
$this->setAttachmentLinks(["<a class='doc_links' href='" . URL::signedRoute('documents.public_download', ['document_hash' => $document->hash]) ."'>". $document->name ."</a>"]);
else
$this->setAttachments([['file' => base64_encode($document->getFile()), 'path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]); $this->setAttachments([['file' => base64_encode($document->getFile()), 'path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]);
} }
foreach ($this->invoice->company->documents as $document) { foreach ($this->invoice->company->documents as $document) {
if($document->size > $this->max_attachment_size)
$this->setAttachmentLinks(["<a class='doc_links' href='" . URL::signedRoute('documents.public_download', ['document_hash' => $document->hash]) ."'>". $document->name ."</a>"]);
else
$this->setAttachments([['file' => base64_encode($document->getFile()), 'path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]); $this->setAttachments([['file' => base64_encode($document->getFile()), 'path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]);
} }
@ -160,7 +170,12 @@ class InvoiceEmailEngine extends BaseEmailEngine
->cursor() ->cursor()
->each(function ($expense) { ->each(function ($expense) {
foreach ($expense->documents as $document) { foreach ($expense->documents as $document) {
if($document->size > $this->max_attachment_size)
$this->setAttachmentLinks(["<a class='doc_links' href='" . URL::signedRoute('documents.public_download', ['document_hash' => $document->hash]) ."'>". $document->name ."</a>"]);
else
$this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]); $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]);
} }
}); });
} }
@ -176,7 +191,12 @@ class InvoiceEmailEngine extends BaseEmailEngine
->cursor() ->cursor()
->each(function ($task) { ->each(function ($task) {
foreach ($task->documents as $document) { foreach ($task->documents as $document) {
if($document->size > $this->max_attachment_size)
$this->setAttachmentLinks(["<a class='doc_links' href='" . URL::signedRoute('documents.public_download', ['document_hash' => $document->hash]) ."'>". $document->name ."</a>"]);
else
$this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]); $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]);
} }
}); });
} }

View File

@ -19,6 +19,7 @@ use App\Utils\Ninja;
use App\Utils\Number; use App\Utils\Number;
use App\Utils\Traits\MakesDates; use App\Utils\Traits\MakesDates;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\URL;
class PaymentEmailEngine extends BaseEmailEngine class PaymentEmailEngine extends BaseEmailEngine
{ {
@ -100,6 +101,10 @@ class PaymentEmailEngine extends BaseEmailEngine
if ($this->client->getSetting('document_email_attachment') !== false) if ($this->client->getSetting('document_email_attachment') !== false)
{ {
foreach ($invoice->documents as $document) { foreach ($invoice->documents as $document) {
if($document->size > $this->max_attachment_size)
$this->setAttachmentLinks(["<a class='doc_links' href='" . URL::signedRoute('documents.public_download', ['document_hash' => $document->hash]) ."'>". $document->name ."</a>"]);
else
$this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]); $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]);
} }
} }

View File

@ -27,6 +27,7 @@ use App\Utils\Traits\MakesHash;
use App\Utils\VendorHtmlEngine; use App\Utils\VendorHtmlEngine;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Lang; use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\URL;
class PurchaseOrderEmailEngine extends BaseEmailEngine class PurchaseOrderEmailEngine extends BaseEmailEngine
{ {
@ -126,11 +127,6 @@ class PurchaseOrderEmailEngine extends BaseEmailEngine
->setTextBody($text_body); ->setTextBody($text_body);
if ($this->vendor->getSetting('pdf_email_attachment') !== false && $this->purchase_order->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) { if ($this->vendor->getSetting('pdf_email_attachment') !== false && $this->purchase_order->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) {
// if (Ninja::isHosted()) {
// $this->setAttachments([$this->purchase_order->pdf_file_path($this->invitation, 'url', true)]);
// } else {
// $this->setAttachments([$this->purchase_order->pdf_file_path($this->invitation)]);
// }
$pdf = (new CreatePurchaseOrderPdf($this->invitation))->rawPdf(); $pdf = (new CreatePurchaseOrderPdf($this->invitation))->rawPdf();
@ -143,13 +139,19 @@ class PurchaseOrderEmailEngine extends BaseEmailEngine
// Storage::url // Storage::url
foreach ($this->purchase_order->documents as $document) { foreach ($this->purchase_order->documents as $document) {
if($document->size > $this->max_attachment_size)
$this->setAttachmentLinks(["<a class='doc_links' href='" . URL::signedRoute('documents.public_download', ['document_hash' => $document->hash]) ."'>". $document->name ."</a>"]);
else
$this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => null]]); $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => null]]);
// $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]);
} }
foreach ($this->purchase_order->company->documents as $document) { foreach ($this->purchase_order->company->documents as $document) {
if($document->size > $this->max_attachment_size)
$this->setAttachmentLinks(["<a class='doc_links' href='" . URL::signedRoute('documents.public_download', ['document_hash' => $document->hash]) ."'>". $document->name ."</a>"]);
else
$this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => null]]); $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => null]]);
// $this->setAttachments([['path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, 'file' => base64_encode($document->getFile())]]);
} }
} }

View File

@ -18,6 +18,7 @@ use App\Utils\Ninja;
use App\Utils\Number; use App\Utils\Number;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Lang; use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\URL;
class QuoteEmailEngine extends BaseEmailEngine class QuoteEmailEngine extends BaseEmailEngine
{ {
@ -128,10 +129,18 @@ class QuoteEmailEngine extends BaseEmailEngine
// Storage::url // Storage::url
foreach ($this->quote->documents as $document) { foreach ($this->quote->documents as $document) {
if($document->size > $this->max_attachment_size)
$this->setAttachmentLinks(["<a class='doc_links' href='" . URL::signedRoute('documents.public_download', ['document_hash' => $document->hash]) ."'>". $document->name ."</a>"]);
else
$this->setAttachments([['file' => base64_encode($document->getFile()), 'path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]); $this->setAttachments([['file' => base64_encode($document->getFile()), 'path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]);
} }
foreach ($this->quote->company->documents as $document) { foreach ($this->quote->company->documents as $document) {
if($document->size > $this->max_attachment_size)
$this->setAttachmentLinks(["<a class='doc_links' href='" . URL::signedRoute('documents.public_download', ['document_hash' => $document->hash]) ."'>". $document->name ."</a>"]);
else
$this->setAttachments([['file' => base64_encode($document->getFile()), 'path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]); $this->setAttachments([['file' => base64_encode($document->getFile()), 'path' => $document->filePath(), 'name' => $document->name, 'mime' => NULL, ]]);
} }
} }

View File

@ -116,6 +116,7 @@ class TemplateEmail extends Mailable
'company' => $company, 'company' => $company,
'whitelabel' => $this->client->user->account->isPaid() ? true : false, 'whitelabel' => $this->client->user->account->isPaid() ? true : false,
'logo' => $this->company->present()->logo($settings), 'logo' => $this->company->present()->logo($settings),
'links' => $this->build_email->getAttachmentLinks(),
]); ]);
foreach ($this->build_email->getAttachments() as $file) { foreach ($this->build_email->getAttachments() as $file) {

View File

@ -109,6 +109,7 @@ class VendorTemplateEmail extends Mailable
'company' => $this->company, 'company' => $this->company,
'whitelabel' => $this->vendor->user->account->isPaid() ? true : false, 'whitelabel' => $this->vendor->user->account->isPaid() ? true : false,
'logo' => $this->company->present()->logo($settings), 'logo' => $this->company->present()->logo($settings),
'links' => $this->build_email->getAttachmentLinks(),
]); ]);

View File

@ -109,6 +109,21 @@
z-index:200 !important; z-index:200 !important;
position: relative; position: relative;
} }
.doc_links {
padding-bottom: 10px;
display: inline-block;
}
a {
text-decoration: none;
color: inherit !important;
}
</style> </style>
<!--[if gte mso 9]> <!--[if gte mso 9]>
@ -160,6 +175,14 @@
style="display: inline-block;background-color: {{ $primary_color }}; color: #ffffff; text-transform: uppercase;letter-spacing: 2px; text-decoration: none; font-size: 13px; font-weight: 600;"> style="display: inline-block;background-color: {{ $primary_color }}; color: #ffffff; text-transform: uppercase;letter-spacing: 2px; text-decoration: none; font-size: 13px; font-weight: 600;">
</a> </a>
</div> </div>
<div>
@isset($links)
@foreach($links as $link)
{!! $link ?? '' !!}<br>
@endforeach
@endisset
</div>
</div> </div>
</td> </td>
</tr> </tr>

View File

@ -64,4 +64,48 @@ class CompanyDocumentsTest extends TestCase
// $this->assertFalse(Storage::exists($document->url)); // $this->assertFalse(Storage::exists($document->url));
} }
// public function testSignedRoutes()
// {
// $company_key = $this->company->company_key;
// $original_count = Document::whereCompanyId($this->company->id)->count();
// $image = UploadedFile::fake()->image('avatar.jpg');
// $document = (new UploadFile(
// $image,
// UploadFile::IMAGE,
// $this->user,
// $this->company,
// $this->invoice))->handle();
// $this->assertNotNull($document);
// // $url = \Illuminate\Support\Facades\URL::signedRoute('api.documents.show', ['document' => $document->hashed_id]);
// $url = \Illuminate\Support\Facades\URL::signedRoute('documents.public_download', ['document_hash' => $document->hash]);
// nlog($url);
// $response = $this->withHeaders([
// 'X-API-SECRET' => config('ninja.api_secret'),
// 'X-API-TOKEN' => $this->token,
// ])->get($url);
// $content = $response->streamedContent();
// nlog($content);
// $response->assertStatus(200);
// $arr = $response->json();
// $this->assertFalse($arr);
// }
} }