| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2026 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* Chat e-mail notification trait. |
| 16 |
* |
| 17 |
* @since 1.8 |
| 18 |
*/ |
| 19 |
trait VBOChatNotificationEmail |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Sends an e-mail notification to the specified user. |
| 23 |
* |
| 24 |
* @param VBOChatMessage $message The message instance. |
| 25 |
* @param string $email The e-mail address of the recipient. |
| 26 |
* @param string|null $name An optional name of the recipient. |
| 27 |
* |
| 28 |
* @return bool |
| 29 |
*/ |
| 30 |
public function sendEmailNotification(VBOChatMessage $message, string $email, ?string $name = null) |
| 31 |
{ |
| 32 |
// get sender e-mail |
| 33 |
$senderMail = VBOFactory::getConfig()->get('senderemail'); |
| 34 |
|
| 35 |
// create a lookup with all the injectable tags |
| 36 |
$placeholders = [ |
| 37 |
'recipient' => $name ?: $email, |
| 38 |
'sender' => $message->getSenderName(), |
| 39 |
'message' => (string) $message->getMessage(), |
| 40 |
'context' => $message->getContext()->getSubject(), |
| 41 |
'url' => $message->getContext()->getUrl(), |
| 42 |
]; |
| 43 |
|
| 44 |
if (strlen($placeholders['message']) === 0) { |
| 45 |
// message without content, only attachments added |
| 46 |
$placeholders['message'] = '<em>' . JText::translate('VBO_CHAT_MESSAGE_MAIL_NOTIFICATION_NOCONT') . '</em>'; |
| 47 |
} |
| 48 |
|
| 49 |
// create e-mail subject |
| 50 |
$subject = JText::translate('VBO_CHAT_MESSAGE_MAIL_NOTIFICATION_SUBJECT'); |
| 51 |
|
| 52 |
// create e-mail body |
| 53 |
$body = JText::translate('VBO_CHAT_MESSAGE_MAIL_NOTIFICATION_BODY'); |
| 54 |
|
| 55 |
// inject placeholders to both the subject and body |
| 56 |
foreach ([&$subject, &$body] as &$str) { |
| 57 |
// iterate all tags |
| 58 |
foreach ($placeholders as $tagName => $tagValue) { |
| 59 |
// inject tag within the template |
| 60 |
$str = str_ireplace('{' . $tagName . '}', $tagValue, $str); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// init mail data wrapper |
| 65 |
$mail = VBOMailWrapper::getInstance() |
| 66 |
->setSender($senderMail, $message->getSenderName()) |
| 67 |
->setRecipient($email) |
| 68 |
->setReply($senderMail) |
| 69 |
->setSubject($subject) |
| 70 |
->setContent($body); |
| 71 |
|
| 72 |
foreach ($message->getAttachments() as $attachment) { |
| 73 |
$mail->addAttachment($attachment->getPath()); |
| 74 |
} |
| 75 |
|
| 76 |
if ($mail->isHtml()) { |
| 77 |
// replace new lines with <br> tags |
| 78 |
$mail->setContent(nl2br($mail->getContent())); |
| 79 |
} |
| 80 |
|
| 81 |
try { |
| 82 |
// send e-mail through the current platform service |
| 83 |
$result = VBOFactory::getPlatform()->getMailer()->send($mail); |
| 84 |
} catch (Exception $error) { |
| 85 |
// silently catch the error |
| 86 |
$result = false; |
| 87 |
} |
| 88 |
|
| 89 |
return $result; |
| 90 |
} |
| 91 |
} |
| 92 |
|