- md5
- 8062ecd59899dcb8e218c95494e861a9
- sha1
- 7bada9e697801e16e1da835fc40bbfaea3e4f06b
- sha256
- 35100caeb49ea6f346caa491a8397656e9bf1293d0758717e015986f05bda065
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Mail;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Mail\Exception\MailDisabledException;
use Joomla\Database\ParameterType;
use Joomla\Registry\Registry;
use PHPMailer\PHPMailer\Exception as phpmailerException;
/**
* Email Templating Class
*
* @since 4.0.0
*/
class MailTemplate
{
/**
* Mailer object to send the actual mail.
*
* @var \Joomla\CMS\Mail\Mail
* @since 4.0.0
*/
protected $mailer;
/**
* Identifier of the mail template.
*
* @var string
* @since 4.0.0
*/
protected $template_id;
/**
* Language of the mail template.
*
* @var string
*/
protected $language;
/**
*
* @var string[]
* @since 4.0.0
*/
protected $data = array();
/**
*
* @var string[]
* @since 4.0.0
*/
protected $attachments = array();
/**
* List of recipients of the email
*
* @var \stdClass[]
* @since 4.0.0
*/
protected $recipients = array();
/**
* Reply To of the email
*
* @var \stdClass
* @since 4.0.0
*/
protected $replyto;
/**
* Constructor for the mail templating class
*
* @param string $template_id Id of the mail template.
* @param string $language Language of the template to use.
* @param Mail $mailer Mail object to send the mail with.
*
* @since 4.0.0
*/
public function __construct($template_id, $language, Mail $mailer = null)
{
$this->template_id = $template_id;
$this->language = $language;
if ($mailer)
{
$this->mailer = $mailer;
}
else
{
$this->mailer = Factory::getMailer();
}
}
/**
* Add an attachment to the mail
*
* @param string $name Filename of the attachment
* @param string $file Either a filepath or filecontent
*
* @return void
*
* @since 4.0.0
*/
public function addAttachment($name, $file)
{
$attachment = new \stdClass;
$attachment->name = $name;
$attachment->file = $file;
$this->attachments[] = $attachment;
}
/**
* Adds recipients for this mail
*
* @param string $mail Mail address of the recipient
* @param string $name Name of the recipient
* @param string $type How should the recipient receive the mail? ('to', 'cc', 'bcc')
*
* @return void
*
* @since 4.0.0
*/
public function addRecipient($mail, $name = null, $type = 'to')
{
$recipient = new \stdClass;
$recipient->mail = $mail;
$recipient->name = $name ?? $mail;
$recipient->type = $type;
$this->recipients[] = $recipient;
}
/**
* Set reply to for this mail
*
* @param string $mail Mail address to reply to
* @param string $name Name
*
* @return void
*
* @since 4.0.0
*/
public function setReplyTo($mail, $name = '')
{
$reply = new \stdClass;
$reply->mail = $mail;
$reply->name = $name;
$this->replyto = $reply;
}
/**
* Add data to replace in the template
*
* @param array $data Associative array of strings to replace
*
* @return void
*
* @since 4.0.0
*/
public function addTemplateData($data)
{
$this->data = array_merge($this->data, $data);
}
/**
* Render and send the mail
*
* @return boolean True on success
*
* @since 4.0.0
* @throws MailDisabledException
* @throws phpmailerException
*/
public function send()
{
$config = ComponentHelper::getParams('com_mails');
$mail = self::getTemplate($this->template_id, $this->language);
/** @var Registry $params */
$params = $mail->params;
$app = Factory::getApplication();
if ($config->get('alternative_mailconfig'))
{
if ($this->mailer->Mailer === 'smtp' || $params->get('mailer') === 'smtp')
{
$smtpauth = ($params->get('smtpauth', $app->get('smtpauth')) == 0) ? null : 1;
$smtpuser = $params->get('smtpuser', $app->get('smtpuser'));
$smtppass = $params->get('smtppass', $app->get('smtppass'));
$smtphost = $params->get('smtphost', $app->get('smtphost'));
$smtpsecure = $params->get('smtpsecure', $app->get('smtpsecure'));
$smtpport = $params->get('smtpport', $app->get('smtpport'));
$this->mailer->useSmtp($smtpauth, $smtphost, $smtpuser, $smtppass, $smtpsecure, $smtpport);
}
if ($params->get('mailer') === 'sendmail')
{
$this->mailer->isSendmail();
}
$mailfrom = $params->get('mailfrom', $app->get('mailfrom'));
$fromname = $params->get('fromname', $app->get('fromname'));
if (MailHelper::isEmailAddress($mailfrom))
{
$this->mailer->setFrom(MailHelper::cleanLine($mailfrom), MailHelper::cleanLine($fromname), false);
}
}
$app->triggerEvent('onMailBeforeRendering', array($this->template_id, &$this));
$mail->subject = $this->replaceTags(Text::_($mail->subject), $this->data);
$this->mailer->setSubject($mail->subject);
if ($config->get('mail_style', 'plaintext') === 'plaintext')
{
$mail->body = $this->replaceTags(Text::_($mail->body), $this->data);
$this->mailer->setBody($mail->body);
}
if ($config->get('mail_style', 'plaintext') === 'html')
{
$this->mailer->IsHTML(true);
$mail->htmlbody = $this->replaceTags(Text::_($mail->htmlbody), $this->data);
$this->mailer->setBody($mail->htmlbody);
}
if ($config->get('mail_style', 'plaintext') === 'both')
{
$this->mailer->IsHTML(true);
$mail->htmlbody = $this->replaceTags(Text::_($mail->htmlbody), $this->data);
$this->mailer->setBody($mail->htmlbody);
$mail->body = $this->replaceTags(Text::_($mail->body), $this->data);
$this->mailer->AltBody = $mail->body;
}
if ($config->get('copy_mails') && $params->get('copyto'))
{
$this->mailer->addBcc($params->get('copyto'));
}
foreach ($this->recipients as $recipient)
{
switch ($recipient->type)
{
case 'cc':
$this->mailer->addcc($recipient->mail, $recipient->name);
break;
case 'bcc':
$this->mailer->addBcc($recipient->mail, $recipient->name);
break;
case 'to':
default:
$this->mailer->addAddress($recipient->mail, $recipient->name);
}
}
if ($this->replyto)
{
$this->mailer->addReplyTo($this->replyto->mail, $this->replyto->name);
}
$path = JPATH_ROOT . '/' . $config->get('attachment_folder') . '/';
foreach ((array) json_decode($mail->attachments) as $attachment)
{
if (is_file($path . $attachment->file))
{
$this->mailer->addAttachment($path . $attachment->file, $attachment->name ?? $attachment->file);
}
}
foreach ($this->attachments as $attachment)
{
if (is_file($attachment->file))
{
$this->mailer->addAttachment($attachment->file, $attachment->name);
}
else
{
$this->mailer->AddStringAttachment($attachment->file, $attachment->name);
}
}
return $this->mailer->Send();
}
/**
* Replace tags with their values recursively
*
* @param string $text The template to process
* @param array $tags An associative array to replace in the template
*
* @return string Rendered mail template
*
* @since 4.0.0
*/
protected function replaceTags($text, $tags)
{
foreach ($tags as $key => $value)
{
if (is_array($value))
{
$matches = array();
if (preg_match_all('/{' . strtoupper($key) . '}(.*?){\/' . strtoupper($key) . '}/s', $text, $matches))
{
foreach ($matches[0] as $i => $match)
{
$replacement = '';
foreach ($value as $subvalue)
{
if (is_array($subvalue))
{
$replacement .= $this->replaceTags($matches[1][$i], $subvalue);
}
}
$text = str_replace($match, $replacement, $text);
}
}
}
else
{
$text = str_replace('{' . strtoupper($key) . '}', $value, $text);
}
}
return $text;
}
/**
* Get a specific mail template
*
* @param string $key Template identifier
* @param string $language Language code of the template
*
* @return object An object with the data of the mail
*
* @since 4.0.0
*/
public static function getTemplate($key, $language)
{
$db = Factory::getDBO();
$query = $db->getQuery(true);
$query->select('*')
->from($db->quoteName('#__mail_templates'))
->where($db->quoteName('template_id') . ' = :key')
->whereIn($db->quoteName('language'), ['', $language], ParameterType::STRING)
->order($db->quoteName('language') . ' DESC')
->bind(':key', $key);
$db->setQuery($query);
$mail = $db->loadObject();
if ($mail)
{
$mail->params = new Registry($mail->params);
}
return $mail;
}
/**
* Insert a new mail template into the system
*
* @param string $key Mail template key
* @param string $subject A default subject (normally a translateable string)
* @param string $body A default body (normally a translateable string)
* @param array $tags Associative array of tags to replace
* @param string $htmlbody A default htmlbody (normally a translateable string)
*
* @return boolean True on success, false on failure
*
* @since 4.0.0
*/
public static function createTemplate($key, $subject, $body, $tags, $htmlbody = '')
{
$db = Factory::getDbo();
$template = new \stdClass;
$template->template_id = $key;
$template->language = '';
$template->subject = $subject;
$template->body = $body;
$template->htmlbody = $htmlbody;
$params = new \stdClass;
$params->tags = array($tags);
$template->params = json_encode($params);
return $db->insertObject('#__mail_templates', $template);
}
/**
* Update an existing mail template
*
* @param string $key Mail template key
* @param string $subject A default subject (normally a translateable string)
* @param string $body A default body (normally a translateable string)
* @param array $tags Associative array of tags to replace
* @param string $htmlbody A default htmlbody (normally a translateable string)
*
* @return boolean True on success, false on failure
*
* @since 4.0.0
*/
public static function updateTemplate($key, $subject, $body, $tags, $htmlbody = '')
{
$db = Factory::getDbo();
$template = new \stdClass;
$template->template_id = $key;
$template->language = '';
$template->subject = $subject;
$template->body = $body;
$template->htmlbody = $htmlbody;
$params = new \stdClass;
$params->tags = array($tags);
$template->params = json_encode($params);
return $db->updateObject('#__mail_templates', $template, ['template_id', 'language']);
}
/**
* Method to delete a mail template
*
* @param string $key The key of the mail template
*
* @return boolean True on success, false on failure
*
* @since 4.0.0
*/
public static function deleteTemplate($key)
{
$db = Factory::getDbo();
$query = $db->getQuery(true);
$query->delete($db->quoteName('#__mail_templates'))
->where($db->quoteName('template_id') . ' = :key')
->bind(':key', $key);
$db->setQuery($query);
return $db->execute();
}
}
The same content is in 3 releases, Joomla 4.0.0-beta3 to 4.0.0-beta5. See them all, or every version of this file.