Add wash certificate email functionality with attachments

Integrates a new method to send wash certificate emails, including support for attaching PDFs. Updates the email system to handle attachments and streamlines booking processes to send certificates upon completion.
This commit is contained in:
Jepp9350
2025-03-25 19:10:04 +01:00
parent 54940e7cd7
commit 6f36dc230a
2 changed files with 72 additions and 7 deletions
+66 -1
View File
@@ -8,6 +8,7 @@ require_once WD . '/modules/email/templates/email_template_footer.php';
require_once WD . '/modules/email/templates/email_template_head.php';
require_once WD . '/modules/email/templates/email_template_stripe_invoice.php';
require_once WD . '/modules/email/templates/email_template_booking_confirmation.php';
require_once WD . '/modules/email/templates/email_template_wash_certificate.php';
use email\email_c;
use email\templates\email_template_booking_confirmation;
@@ -15,11 +16,13 @@ use email\templates\email_template_footer;
use email\templates\email_template_head;
use email\templates\email_template_header;
use email\templates\email_template_stripe_invoice;
use email\templates\email_template_wash_certificate;
use Exception;
use interfaces\email_i;
use JsonException;
use MailerSend\Exceptions\MailerSendAssertException;
use MailerSend\Exceptions\MailerSendException;
use MailerSend\Helpers\Builder\Attachment;
use MailerSend\Helpers\Builder\EmailParams;
use MailerSend\Helpers\Builder\Header;
use MailerSend\Helpers\Builder\Recipient;
@@ -110,7 +113,7 @@ class email implements email_i
* @throws MailerSendAssertException
* @throws MailerSendException
*/
private function sendEmailMailerSend(string $to, string $recipient_name, string $subject, string $message, string $html = null, string $references = null): void
private function sendEmailMailerSend(string $to, string $recipient_name, string $subject, string $message, string $html = null, string $references = null, array $attachments = []): void
{
// Send POST request to email service
$mailersend = new MailerSend(['api_key' => $this->config->mailersend_api_key->getVariableValue()]);
@@ -127,6 +130,22 @@ class email implements email_i
->setHtml($html ?? $message)
->setReplyTo($this->config->smtp_reply_to->getVariableValue())
->setReplyToName($this->config->smtp_reply_to_name->getVariableValue());
if ($attachments) {
$attachments = array_map(function ($attachment) {
// Read the data from the path (Attachment[0]) and set the filename (Attachment[1])
$attachment[0] = file_get_contents($attachment[0]);
if ($attachment[0] === false) {
throw new Exception('Failed to read file: ' . $attachment[0]);
}
if (empty($attachment[1])) {
throw new Exception('Filename is empty');
}
return new Attachment($attachment[0], $attachment[1]);
}, $attachments);
}
if ($attachments) {
$emailParams->setAttachments($attachments);
}
if ($references) {
$emailParams->setHeaders([
@@ -226,4 +245,50 @@ class email implements email_i
$html
);
}
/**
* Send a wash certificate email
* @throws MailerSendException
* @throws ClientExceptionInterface
* @throws JsonException
* @throws MailerSendAssertException
*/
public function sendWashCertificateEmail(
$booking_id,
$company_name,
$contact_email,
$reference_number,
$wash_certificate_url,
$wash_certificate_email,
): void
{
// Generate HTML email
$html = self::generateHtmlHeader();
// Add email content
$html .= (new email_template_wash_certificate(
$booking_id,
$company_name,
))->generate_html();
// Add email footer
$html .= self::generateHtmlFooter();
// Attach the wash certificate
$pdf = (new pdf_store())->download($wash_certificate_url);
// Send email
$this->sendEmailMailerSend(
$wash_certificate_email,
$company_name,
'Truck Wash Booking ( ID: ' . $booking_id . ', REF: ' . $reference_number . ' )',
'',
$html,
null,
[
[
$pdf,
'wash_certificate_' . $booking_id . '_' . $reference_number . '.pdf'
]
]
);
}
}