Add email attachment support and improve message formatting

This commit is contained in:
Focuslinkstech 2025-03-13 11:05:32 +01:00
parent 3372da2ac4
commit 66d67cb61d

View File

@ -120,7 +120,7 @@ class Message
} }
} }
public static function sendEmail($to, $subject, $body) public static function sendEmail($to, $subject, $body, $attachmentPath = null)
{ {
global $config, $PAGES_PATH, $debug_mail; global $config, $PAGES_PATH, $debug_mail;
if (empty($body)) { if (empty($body)) {
@ -130,7 +130,6 @@ class Message
return ""; return "";
} }
run_hook('send_email', [$to, $subject, $body]); #HOOK run_hook('send_email', [$to, $subject, $body]); #HOOK
self::logMessage('Email', $to, $body, 'Success');
if (empty($config['smtp_host'])) { if (empty($config['smtp_host'])) {
$attr = ""; $attr = "";
if (!empty($config['mail_from'])) { if (!empty($config['mail_from'])) {
@ -140,6 +139,7 @@ class Message
$attr .= "Reply-To: " . $config['mail_reply_to'] . "\r\n"; $attr .= "Reply-To: " . $config['mail_reply_to'] . "\r\n";
} }
mail($to, $subject, $body, $attr); mail($to, $subject, $body, $attr);
self::logMessage('Email', $to, $body, 'Success');
} else { } else {
$mail = new PHPMailer(); $mail = new PHPMailer();
$mail->isSMTP(); $mail->isSMTP();
@ -161,6 +161,10 @@ class Message
$mail->addAddress($to); $mail->addAddress($to);
$mail->Subject = $subject; $mail->Subject = $subject;
// Attachments
if (!empty($attachmentPath)) {
$mail->addAttachment($attachmentPath);
}
if (!file_exists($PAGES_PATH . DIRECTORY_SEPARATOR . 'Email.html')) { if (!file_exists($PAGES_PATH . DIRECTORY_SEPARATOR . 'Email.html')) {
if (!copy($PAGES_PATH . '_template' . DIRECTORY_SEPARATOR . 'Email.html', $PAGES_PATH . DIRECTORY_SEPARATOR . 'Email.html')) { if (!copy($PAGES_PATH . '_template' . DIRECTORY_SEPARATOR . 'Email.html', $PAGES_PATH . DIRECTORY_SEPARATOR . 'Email.html')) {
@ -176,6 +180,7 @@ class Message
$html = str_replace('[[Body]]', nl2br($body), $html); $html = str_replace('[[Body]]', nl2br($body), $html);
$mail->isHTML(true); $mail->isHTML(true);
$mail->Body = $html; $mail->Body = $html;
$mail->Body = $html;
} else { } else {
$mail->isHTML(false); $mail->isHTML(false);
$mail->Body = $body; $mail->Body = $body;
@ -393,7 +398,8 @@ class Message
} }
} }
public static function getMessageType($type, $message){ public static function getMessageType($type, $message)
{
if (strpos($message, "<divider>") === false) { if (strpos($message, "<divider>") === false) {
return $message; return $message;
} }