/** * Adds an email to the queue. * * @return int The id of the inserted mail. * @param string $subject The subject for the email. * @param string $template The template to use. * @param array[optional] $variables Variables that should be assigned in the email. * @param string[optional] $toEmail The to-address for the email. * @param string[optional] $toName The to-name for the email. * @param string[optional] $fromEmail The from-address for the mail. * @param string[optional] $fromName The from-name for the mail. * @param string[optional] $replyToEmail The replyto-address for the mail. * @param string[optional] $replyToName The replyto-name for the mail. * @param bool[optional] $queue Should the mail be queued? * @param int[optional] $sendOn When should the email be send, only used when $queue is true. * @param bool[optional] $isRawHTML If this is true $template will be handled as raw HTML, so no parsing of $variables is done. * @param string[optional] $plainText The plain text version. * @param array[optional] $attachments Paths to attachments to include. */ public static function addEmail($subject, $template, array $variables = null, $toEmail = null, $toName = null, $fromEmail = null, $fromName = null, $replyToEmail = null, $replyToName = null, $queue = false, $sendOn = null, $isRawHTML = false, $plainText = null, array $attachments = null) { // redefine $subject = (string) strip_tags($subject); $template = (string) $template; // set defaults $to = FrontendModel::getModuleSetting('core', 'mailer_to'); $from = FrontendModel::getModuleSetting('core', 'mailer_from'); $replyTo = FrontendModel::getModuleSetting('core', 'mailer_reply_to'); $utm = array('utm_source' => 'mail', 'utm_medium' => 'email', 'utm_campaign' => SpoonFilter::urlise($subject)); // set recipient/sender headers $email['to_email'] = empty($toEmail) ? (string) $to['email'] : $toEmail; $email['to_name'] = empty($toName) ? (string) $to['name'] : $toName; $email['from_email'] = empty($fromEmail) ? (string) $from['email'] : $fromEmail; $email['from_name'] = empty($fromName) ? (string) $from['name'] : $fromName; $email['reply_to_email'] = empty($replyToEmail) ? (string) $replyTo['email'] : $replyToEmail; $email['reply_to_name'] = empty($replyToName) ? (string) $replyTo['name'] : $replyToName; // validate if (!empty($email['to_email']) && !SpoonFilter::isEmail($email['to_email'])) { throw new FrontendException('Invalid e-mail address for recipient.'); } if (!empty($email['from_email']) && !SpoonFilter::isEmail($email['from_email'])) { throw new FrontendException('Invalid e-mail address for sender.'); } if (!empty($email['reply_to_email']) && !SpoonFilter::isEmail($email['reply_to_email'])) { throw new FrontendException('Invalid e-mail address for reply-to address.'); } // build array $email['subject'] = SpoonFilter::htmlentitiesDecode($subject); if ($isRawHTML) { $email['html'] = $template; } else { $email['html'] = self::getTemplateContent($template, $variables); } if ($plainText !== null) { $email['plain_text'] = $plainText; } $email['created_on'] = FrontendModel::getUTCDate(); // init var $matches = array(); // get internal links preg_match_all('|href="/(.*)"|i', $email['html'], $matches); // any links? if (!empty($matches[0])) { // init vars $search = array(); $replace = array(); // loop the links foreach ($matches[0] as $key => $link) { $search[] = $link; $replace[] = 'href="' . SITE_URL . '/' . $matches[1][$key] . '"'; } // replace $email['html'] = str_replace($search, $replace, $email['html']); } // init var $matches = array(); // get internal urls preg_match_all('|src="/(.*)"|i', $email['html'], $matches); // any links? if (!empty($matches[0])) { // init vars $search = array(); $replace = array(); // loop the links foreach ($matches[0] as $key => $link) { $search[] = $link; $replace[] = 'src="' . SITE_URL . '/' . $matches[1][$key] . '"'; } // replace $email['html'] = str_replace($search, $replace, $email['html']); } // init var $matches = array(); // match links preg_match_all('/href="(http:\\/\\/(.*))"/iU', $email['html'], $matches); // any links? if (isset($matches[0]) && !empty($matches[0])) { // init vars $searchLinks = array(); $replaceLinks = array(); // loop old links foreach ($matches[1] as $i => $link) { $searchLinks[] = $matches[0][$i]; $replaceLinks[] = 'href="' . FrontendModel::addURLParameters($link, $utm) . '"'; } // replace $email['html'] = str_replace($searchLinks, $replaceLinks, $email['html']); } // attachments added if (!empty($attachments)) { // add attachments one by one foreach ($attachments as $attachment) { // only add existing files if (SpoonFile::exists($attachment)) { $email['attachments'][] = $attachment; } } // serialize :) if (!empty($email['attachments'])) { $email['attachments'] = serialize($email['attachments']); } } // set send date if ($queue) { if ($sendOn === null) { $email['send_on'] = FrontendModel::getUTCDate('Y-m-d H') . ':00:00'; } else { $email['send_on'] = FrontendModel::getUTCDate('Y-m-d H:i:s', (int) $sendOn); } } // insert the email into the database $id = FrontendModel::getDB(true)->insert('emails', $email); // trigger event FrontendModel::triggerEvent('core', 'after_email_queued', array('id' => $id)); // if queue was not enabled, send this mail right away if (!$queue) { self::send($id); } // return return $id; }
/** * Set the url * * @param string $url The url to assiociate the item with. */ public function setUrl($url) { // redefine var $url = (string) $url; // if link doesn't start with http, we prepend the URL of the site if (substr($url, 0, 7) != 'http://') { $url = SITE_URL . $url; } $url = FrontendModel::addURLParameters($url, $this->utm); $url = htmlspecialchars_decode($url); // call parent parent::setUrl($url); }
/** * Process links, will prepend SITE_URL if needed and append UTM-parameters * * @return string * @param string $content The content to process. */ public function processLinks($content) { // redefine $content = (string) $content; // replace URLs and images $search = array('href="/', 'src="/'); $replace = array('href="' . SITE_URL . '/', 'src="' . SITE_URL . '/'); // replace links to files $content = str_replace($search, $replace, $content); // init var $matches = array(); // match links preg_match_all('/href="(http:\\/\\/(.*))"/iU', $content, $matches); // any links? if (isset($matches[1]) && !empty($matches[1])) { // init vars $searchLinks = array(); $replaceLinks = array(); // loop old links foreach ($matches[1] as $i => $link) { $searchLinks[] = $matches[0][$i]; $replaceLinks[] = 'href="' . FrontendModel::addURLParameters($link, $this->utm) . '"'; } // replace $content = str_replace($searchLinks, $replaceLinks, $content); } // return content return $content; }