Add MailerSend email service integration and templates
Introduced MailerSend as an email provider with essential configuration options, including API key management and "reply-to" support. Enhanced email functionality by implementing a modular template system (header, footer, and content) for both text and HTML formats. Updated the existing email infrastructure to handle Stripe invoice notifications via MailerSend with conditional fallback planned for default SMTP services.
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace email\config;
|
||||
|
||||
use Exception;
|
||||
use traits\module_config_variable;
|
||||
|
||||
class email_mailersend_api_key_c
|
||||
{
|
||||
use module_config_variable;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
self::setupConfigVariable(
|
||||
'Email',
|
||||
'mailersend_api_key',
|
||||
'string',
|
||||
false,
|
||||
null,
|
||||
'The API key for the MailerSend email service',
|
||||
'mlsn.7dd13651...',
|
||||
true,
|
||||
''
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace email\config;
|
||||
|
||||
use Exception;
|
||||
use traits\module_config_variable;
|
||||
|
||||
class email_mailersend_enabled_c
|
||||
{
|
||||
use module_config_variable;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
self::setupConfigVariable(
|
||||
'Email',
|
||||
'mailersend_enabled',
|
||||
'bool',
|
||||
true,
|
||||
null,
|
||||
'Whether the email service should use MailerSend for sending emails',
|
||||
'1',
|
||||
false,
|
||||
'false'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace email\config;
|
||||
|
||||
use Exception;
|
||||
use traits\module_config_variable;
|
||||
|
||||
class email_smtp_reply_to_c
|
||||
{
|
||||
use module_config_variable;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
self::setupConfigVariable(
|
||||
'Email',
|
||||
'smtp_reply_to',
|
||||
'string',
|
||||
true,
|
||||
null,
|
||||
'The email address to reply to',
|
||||
'user@example.com',
|
||||
false,
|
||||
''
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace email\config;
|
||||
|
||||
use Exception;
|
||||
use traits\module_config_variable;
|
||||
|
||||
class email_smtp_reply_to_name_c
|
||||
{
|
||||
use module_config_variable;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
self::setupConfigVariable(
|
||||
'Email',
|
||||
'smtp_reply_to_name',
|
||||
'string',
|
||||
true,
|
||||
null,
|
||||
'The name to reply to',
|
||||
'Company Name Support',
|
||||
false,
|
||||
''
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace email;
|
||||
/** Universal */
|
||||
require_once WD . '/modules/email/config/email_enabled_c.php';
|
||||
require_once WD . '/modules/email/config/email_smtp_from_c.php';
|
||||
require_once WD . '/modules/email/config/email_smtp_from_name_c.php';
|
||||
require_once WD . '/modules/email/config/email_smtp_reply_to_c.php';
|
||||
require_once WD . '/modules/email/config/email_smtp_reply_to_name_c.php';
|
||||
|
||||
/** SMTP */
|
||||
require_once WD . '/modules/email/config/email_smtp_encryption_c.php';
|
||||
require_once WD . '/modules/email/config/email_smtp_host_c.php';
|
||||
require_once WD . '/modules/email/config/email_smtp_password_c.php';
|
||||
require_once WD . '/modules/email/config/email_smtp_port_c.php';
|
||||
require_once WD . '/modules/email/config/email_smtp_username_c.php';
|
||||
require_once WD . '/modules/email/config/email_smtp_from_c.php';
|
||||
require_once WD . '/modules/email/config/email_smtp_from_name_c.php';
|
||||
/** Mailersend */
|
||||
require_once WD . '/modules/email/config/email_mailersend_enabled_c.php';
|
||||
require_once WD . '/modules/email/config/email_mailersend_api_key_c.php';
|
||||
|
||||
use email\config\email_enabled_c;
|
||||
use email\config\email_mailersend_api_key_c;
|
||||
use email\config\email_mailersend_enabled_c;
|
||||
use email\config\email_smtp_encryption_c;
|
||||
use email\config\email_smtp_from_c;
|
||||
use email\config\email_smtp_from_name_c;
|
||||
use email\config\email_smtp_host_c;
|
||||
use email\config\email_smtp_password_c;
|
||||
use email\config\email_smtp_port_c;
|
||||
use email\config\email_smtp_reply_to_c;
|
||||
use email\config\email_smtp_reply_to_name_c;
|
||||
use email\config\email_smtp_username_c;
|
||||
use traits\module_config_t;
|
||||
|
||||
@@ -72,6 +84,29 @@ class email_c
|
||||
*/
|
||||
public email_smtp_from_name_c $smtp_from_name;
|
||||
|
||||
/**
|
||||
* The status of Mailersend, whether it is enabled or not
|
||||
* @var email_mailersend_enabled_c
|
||||
*/
|
||||
public email_mailersend_enabled_c $mailersend_enabled;
|
||||
|
||||
/**
|
||||
* The API key for the Mailersend email service
|
||||
* @var email_mailersend_api_key_c
|
||||
*/
|
||||
public email_mailersend_api_key_c $mailersend_api_key;
|
||||
|
||||
/**
|
||||
* The email address to reply to
|
||||
* @var email_smtp_reply_to_c The email address to reply to
|
||||
*/
|
||||
public email_smtp_reply_to_c $smtp_reply_to;
|
||||
/**
|
||||
* The name to reply to
|
||||
* @var email_smtp_reply_to_name_c The name to reply to
|
||||
*/
|
||||
public email_smtp_reply_to_name_c $smtp_reply_to_name;
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -84,7 +119,11 @@ class email_c
|
||||
email_smtp_password_c::class,
|
||||
email_smtp_encryption_c::class,
|
||||
email_smtp_from_c::class,
|
||||
email_smtp_from_name_c::class
|
||||
email_smtp_from_name_c::class,
|
||||
email_mailersend_enabled_c::class,
|
||||
email_mailersend_api_key_c::class,
|
||||
email_smtp_reply_to_c::class,
|
||||
email_smtp_reply_to_name_c::class
|
||||
]);
|
||||
$this->enabled = new email_enabled_c();
|
||||
$this->smtp_host = new email_smtp_host_c();
|
||||
@@ -94,6 +133,10 @@ class email_c
|
||||
$this->smtp_encryption = new email_smtp_encryption_c();
|
||||
$this->smtp_from = new email_smtp_from_c();
|
||||
$this->smtp_from_name = new email_smtp_from_name_c();
|
||||
$this->mailersend_enabled = new email_mailersend_enabled_c();
|
||||
$this->mailersend_api_key = new email_mailersend_api_key_c();
|
||||
$this->smtp_reply_to = new email_smtp_reply_to_c();
|
||||
$this->smtp_reply_to_name = new email_smtp_reply_to_name_c();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace email\helpers;
|
||||
|
||||
trait email_template
|
||||
{
|
||||
protected string $subject;
|
||||
|
||||
abstract function generate_html(): string;
|
||||
|
||||
abstract function generate_text(): string;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace email\templates;
|
||||
|
||||
use email\helpers\email_template;
|
||||
|
||||
class email_template_footer
|
||||
{
|
||||
use email_template;
|
||||
|
||||
public function generate_text(): string
|
||||
{
|
||||
return "Truckwash ApS | CVR: 41004355 | https://truckwash.dk | Send os en email på cph@truckwash.dk - Ring til os på (+45) 43 71 78 86";
|
||||
}
|
||||
|
||||
public function generate_html(): string
|
||||
{
|
||||
ob_start();
|
||||
# Start of the html
|
||||
?>
|
||||
|
||||
<!-- Footer -->
|
||||
<br><br>
|
||||
<div style="max-width: 600px;">
|
||||
<img src="https://usercontent.one/wp/www.truckwash.dk/wp-content/uploads/2021/04/truckwash-banner-png.png"
|
||||
alt="Truck Wash logo" style="width: 25%; height: auto; display: block; margin: 0 auto;">
|
||||
<p style="text-align: center;"><a href="https://truckwash.dk/">Truckwash ApS</a> | <a
|
||||
href="https://datacvr.virk.dk/enhed/virksomhed/41004355"> CVR: 41004355</a> | <a
|
||||
href="https://truckwash.dk/">https://truckwash.dk</a></p>
|
||||
<p style="text-align: center;"><a href="mailto:cph@truckwash.dk"
|
||||
style="color: black; text-decoration: none;">Send os en email på
|
||||
cph@truckwash.dk</a> - <a href="tel:+4543717886" style="color: black; text-decoration: none;">Ring
|
||||
til os på (+45) 43 71 78 86</a></p>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
# End of the html
|
||||
return ob_get_clean();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace email\templates;
|
||||
|
||||
use email\helpers\email_template;
|
||||
|
||||
class email_template_head
|
||||
{
|
||||
use email_template;
|
||||
|
||||
public function generate_text(): string
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
public function generate_html(): string
|
||||
{
|
||||
ob_start();
|
||||
# Start of the html
|
||||
?>
|
||||
<!-- Styles -->
|
||||
<style>
|
||||
.email-template {
|
||||
width: 100%;
|
||||
max-width: 600px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
background-color: #f9f9f9;
|
||||
font-family: Arial, sans-serif;
|
||||
}
|
||||
|
||||
.email-template h1 {
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.email-template p {
|
||||
color: #333;
|
||||
}
|
||||
</style>
|
||||
|
||||
<?php
|
||||
# End of the html
|
||||
return ob_get_clean();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace email\templates;
|
||||
|
||||
use email\helpers\email_template;
|
||||
|
||||
class email_template_header
|
||||
{
|
||||
use email_template;
|
||||
|
||||
public function generate_text(): string
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
public function generate_html(): string
|
||||
{
|
||||
ob_start();
|
||||
# Start of the html
|
||||
?>
|
||||
|
||||
<!-- Logo -->
|
||||
<div style="max-width: 600px;">
|
||||
<img src="https://usercontent.one/wp/www.truckwash.dk/wp-content/uploads/2021/04/truckwash-banner-png.png"
|
||||
alt="Truck Wash logo"
|
||||
style="width: 100%; max-width: 600px; height: auto; display: block; margin: 0 auto;">
|
||||
</div>
|
||||
|
||||
<?php
|
||||
# End of the html
|
||||
return ob_get_clean();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace email\templates;
|
||||
|
||||
use email\helpers\email_template;
|
||||
|
||||
class email_template_stripe_invoice
|
||||
{
|
||||
use email_template;
|
||||
|
||||
protected int $order_id;
|
||||
protected string $stripe_payment_link;
|
||||
protected string $name;
|
||||
|
||||
public function __construct(int $order_id, string $stripe_payment_link, string $name)
|
||||
{
|
||||
$this->order_id = $order_id;
|
||||
$this->stripe_payment_link = $stripe_payment_link;
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public function generate_text(): string
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
public function generate_html(): string
|
||||
{
|
||||
ob_start();
|
||||
# Start of the html
|
||||
?>
|
||||
|
||||
<!-- Email template -->
|
||||
<p>Kære <?= $this->name ?>,</p>
|
||||
<p>Tak for din bestilling hos Truck Wash. Vi har sendt dig en faktura på Stripe.
|
||||
Du kan betale fakturaen ved at klikke på linket nedenfor:</p>
|
||||
<p><a href="<?= $this->stripe_payment_link ?>">Betal faktura for ordre <?= $this->order_id ?></a></p>
|
||||
<!-- End of the email template -->
|
||||
|
||||
<?php
|
||||
# End of the html
|
||||
return ob_get_clean();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user