From 734a954228446e5d16510b9bff629d3e6ce84d95 Mon Sep 17 00:00:00 2001 From: Cameron Date: Sat, 16 Aug 2014 17:19:50 -0700 Subject: [PATCH] Additional changes so email templates conform to v2 specifications. --- e107_core/templates/email_template.php | 83 ++++++++++---- e107_handlers/mail.php | 69 ++++++++---- e107_handlers/user_model.php | 147 +++++++++++++++++-------- 3 files changed, 213 insertions(+), 86 deletions(-) diff --git a/e107_core/templates/email_template.php b/e107_core/templates/email_template.php index f01555658..e6397da8b 100644 --- a/e107_core/templates/email_template.php +++ b/e107_core/templates/email_template.php @@ -2,25 +2,14 @@ /* * e107 website system * - * Copyright (C) 2008-2013 e107 Inc (e107.org) + * Copyright (C) 2008-2014 e107 Inc (e107.org) * Released under the terms and conditions of the * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * * Templates for all emails * - * $URL: $ - * $Revision: 11315 $ - * $Id: $ */ -/** - * - * @package e107 - * @subpackage e107_templates - * @version $Id: mail_manager_class.php 11315 2010-02-10 18:18:01Z secretr $; - * -*/ - /** * This file defines the default templates for each type of email which may be sent. @@ -318,13 +307,7 @@ $MONTHLYUPDATE_TEMPLATE = array( ); -/* - * QUICK ADD USER EMAIL TEMPLATE - BODY. - * This is the email that is sent when an admin creates a user account in admin. "Quick Add User" - USRLAN_185 = A user account has been created for you at {SITEURL} with the following login:
Login Name: {LOGIN}
Password: {PASSWORD}

- USRLAN_186 = Please go to the site as soon as possible and log in, then change your password using the \'Settings\' option.

- You can also change other settings at the same time.

Note that your password cannot be recovered if you lose it. -*/ + $QUICKADDUSER_TEMPLATE = array( 'template_name' => 'Quick-Add-User', @@ -351,14 +334,13 @@ $QUICKADDUSER_TEMPLATE = array( // Default - test email and when no template specified. $EMAIL_TEMPLATE['default']['name'] = 'Default'; -$EMAIL_TEMPLATE['default']['overrides'] = ''; $EMAIL_TEMPLATE['default']['header'] = " @@ -374,4 +356,63 @@ $EMAIL_TEMPLATE['default']['footer'] = "

"; + +// Signup Template. + + +$EMAIL_TEMPLATE['signup']['subject'] = LAN_SIGNUP_96.' {SITENAME}'; +$EMAIL_TEMPLATE['signup']['header'] = $EMAIL_TEMPLATE['default']['header']; +$EMAIL_TEMPLATE['signup']['body'] = " +
+ ".LAN_EMAIL_01." {USERNAME},
+
". + LAN_SIGNUP_97." {SITENAME}
+ ".LAN_SIGNUP_21."
+
+ {ACTIVATION_LINK}
+
+ ".LAN_SIGNUP_59."
+
+ ".LAN_SIGNUP_18."
+
+ ".LAN_LOGINNAME.": {LOGINNAME}
+ ".LAN_PASSWORD.": {PASSWORD}
+
+ ".LAN_EMAIL_04."
+ ".LAN_EMAIL_05."
+
+ ".LAN_EMAIL_06."
+
+ {SITENAME=link}
+ {SITEURL} + +

".($includeSiteButton ? "" : '')." +
+ + "; +$EMAIL_TEMPLATE['signup']['footer'] = " + + "; +$EMAIL_TEMPLATE['signup']['cc'] = ""; +$EMAIL_TEMPLATE['signup']['bcc'] = ""; +$EMAIL_TEMPLATE['signup']['attachments'] = ""; + +/* + * QUICK ADD USER EMAIL TEMPLATE - BODY. + * This is the email that is sent when an admin creates a user account in admin. "Quick Add User" + USRLAN_185 = A user account has been created for you at {SITEURL} with the following login:
Login Name: {LOGIN}
Password: {PASSWORD}

+ USRLAN_186 = Please go to the site as soon as possible and log in, then change your password using the \'Settings\' option.

+ You can also change other settings at the same time.

Note that your password cannot be recovered if you lose it. +*/ +$EMAIL_TEMPLATE['quickadd']['header'] = $EMAIL_TEMPLATE['default']['header']; // will use default header above. +$EMAIL_TEMPLATE['quickadd']['body'] = USRLAN_185.USRLAN_186; +$EMAIL_TEMPLATE['quickadd']['footer'] = $EMAIL_TEMPLATE['default']['footer']; // will use default footer above. + + +// Notify (@see admin-> notify) // TODO + +$EMAIL_TEMPLATE['notify']['header'] = $EMAIL_TEMPLATE['default']['header']; // will use default header above. +$EMAIL_TEMPLATE['notify']['body'] = $EMAIL_TEMPLATE['default']['body']; // will use default header above. +$EMAIL_TEMPLATE['notify']['footer'] = $EMAIL_TEMPLATE['default']['footer']; // will use default header above. + ?> \ No newline at end of file diff --git a/e107_handlers/mail.php b/e107_handlers/mail.php index 779ec4593..ba5b7a62b 100644 --- a/e107_handlers/mail.php +++ b/e107_handlers/mail.php @@ -137,24 +137,24 @@ define('MAIL_LOG_PATH',e_LOG); class e107Email extends PHPMailer { - private $general_opts = array(); - private $logEnable = 0; // 0 = log disabled, 1 = 'dry run' (debug and log, no send). 2 = 'log all' (send, and log result) - private $logHandle = FALSE; // Save handle of log file if opened + private $general_opts = array(); + private $logEnable = 0; // 0 = log disabled, 1 = 'dry run' (debug and log, no send). 2 = 'log all' (send, and log result) + private $logHandle = FALSE; // Save handle of log file if opened - private $localUseVerp = FALSE; // Use our own variable - PHPMailer one doesn't work with all mailers - private $save_bouncepath = ''; // Used with VERP + private $localUseVerp = FALSE; // Use our own variable - PHPMailer one doesn't work with all mailers + private $save_bouncepath = ''; // Used with VERP - private $add_email = 0; // 1 includes email detail in log (if logging enabled, of course) - private $allow_html = 1; // Flag for HTML conversion - '1' = default, FALSE = disable, TRUE = force. - private $add_HTML_header = FALSE; // If TRUE, inserts a standard HTML header at the front of the HTML part of the email (set FALSE for BC) - private $SendCount = 0; // Keep track of how many emails sent since last SMTP open/connect (used for SMTP KeepAlive) - private $TotalSent = 0; // Info might be of interest - private $TotalErrors = 0; // Count errors in sending emails - private $pause_amount = 10; // Number of emails to send before pausing/resetting (or closing if SMTPkeepAlive set) - private $pause_time = 1; // Time to pause after sending a block of emails - - public $legacyBody = FALSE; // TRUE enables legacy conversion of plain text body to HTML in HTML emails + private $add_email = 0; // 1 includes email detail in log (if logging enabled, of course) + private $allow_html = 1; // Flag for HTML conversion - '1' = default, FALSE = disable, TRUE = force. + private $add_HTML_header = FALSE; // If TRUE, inserts a standard HTML header at the front of the HTML part of the email (set FALSE for BC) + private $SendCount = 0; // Keep track of how many emails sent since last SMTP open/connect (used for SMTP KeepAlive) + private $TotalSent = 0; // Info might be of interest + private $TotalErrors = 0; // Count errors in sending emails + private $pause_amount = 10; // Number of emails to send before pausing/resetting (or closing if SMTPkeepAlive set) + private $pause_time = 1; // Time to pause after sending a block of emails + public $legacyBody = false; // TRUE enables legacy conversion of plain text body to HTML in HTML emails + private $debug = false; // echos various debug info when set to true. /** * Constructor sets up all the global options, and sensible defaults - it should be the only place the prefs are accessed @@ -170,6 +170,11 @@ class e107Email extends PHPMailer $e107 = e107::getInstance(); $pref = e107::pref('core'); $tp = e107::getParser(); + + if(defined('MAIL_DEBUG')) + { + $this->debug = true; + } $this->CharSet = 'utf-8'; $this->SetLanguage(CORE_LC); @@ -188,7 +193,7 @@ class e107Email extends PHPMailer $this->allow_html = varset($pref['mail_sendstyle'],'textonly') == 'texthtml' ? true : 1; if (varsettrue($pref['mail_options'])) $this->general_opts = explode(',',$pref['mail_options'],''); - if (defined('MAIL_DEBUG')) echo 'Mail_options: '.$pref['mail_options'].' Count: '.count($this->general_opts).'
'; + if ($this->debug) echo 'Mail_options: '.$pref['mail_options'].' Count: '.count($this->general_opts).'
'; foreach ($this->general_opts as $k => $v) { $v = trim($v); @@ -196,7 +201,7 @@ class e107Email extends PHPMailer if (strpos($v,'hostname') === 0) { list(,$this->HostName) = explode('=',$v); - if (defined('MAIL_DEBUG')) echo "Host name set to: {$this->HostName}
"; + if ($this->debug) echo "Host name set to: {$this->HostName}
"; } } @@ -248,7 +253,7 @@ class e107Email extends PHPMailer $this->Port = 465; break; default : - if (defined('MAIL_DEBUG')) echo "Invalid option: {$smtp_options['secure']}
"; + if ($this->debug) echo "Invalid option: {$smtp_options['secure']}
"; } } $this->SMTPKeepAlive = varset($smtp_options['keepalive'],FALSE); // ***** Control this @@ -485,9 +490,11 @@ class e107Email extends PHPMailer break; } + $message = str_replace("\t", "", $message); // filter out tabs from templates; + if ($want_HTML !== FALSE) { - if (defined('MAIL_DEBUG')) echo "Generating multipart email
"; + if ($this->debug) echo "Generating multipart email
"; if ($add_HTML_header) { $message = "\n @@ -495,6 +502,12 @@ class e107Email extends PHPMailer } if ($this->legacyBody && !preg_match('/<(font|br|a|img|b)/i', $message)) // Assume html if it includes one of these tags { // Otherwise assume its a plain text message which needs some conversion to render in HTML + + if($this->debug == true) + { + echo 'Running legacyBody mode
'; + } + $message = htmlspecialchars($message,ENT_QUOTES,$this->CharSet); $message = preg_replace('%(http|ftp|https)(://\S+)%', '\1\2', $message); $message = preg_replace('/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i', '\\1\\2', $message); @@ -508,7 +521,7 @@ class e107Email extends PHPMailer } else { // generate the plain text as the sole part of the email - if (defined('MAIL_DEBUG')) echo "Generating plain text email
"; + if ($this->debug) echo "Generating plain text email
"; if (strpos($message,'') !== FALSE) { $text = strstr($message,''); @@ -586,11 +599,17 @@ class e107Email extends PHPMailer public function arraySet($eml) { + if($this->debug) + { + print_a($eml); + } + + if(vartrue($eml['template'])) // @see e107_core/templates/email_template.php { $tp = e107::getParser(); - if($tmpl = e107::getCoreTemplate('email','default', true, true)) // $EMAIL_TEMPLATE['default'] + if($tmpl = e107::getCoreTemplate('email',$eml['template'], true, true)) // $EMAIL_TEMPLATE['default'] { $filter = array("\n", "\t"); $tmpl['header'] = str_replace($filter,'', $tmpl['header']); @@ -790,6 +809,12 @@ class e107Email extends PHPMailer preg_match_all("/(src|background)=([\"\'])(.*)\\2/Ui", $message, $images); // Modified to accept single quotes as well if(isset($images[3])) { + + if($this->debug) + { + print_a($images[3]); + } + foreach($images[3] as $i => $url) { // do not change urls for absolute images (thanks to corvuscorax) @@ -818,7 +843,7 @@ class e107Email extends PHPMailer } else { - if (defined('MAIL_DEBUG')) echo "Add embedded image {$url} failed
"; + if ($this->debug) echo "Add embedded image {$url} failed
"; } } } diff --git a/e107_handlers/user_model.php b/e107_handlers/user_model.php index d3d99bc37..af786c111 100644 --- a/e107_handlers/user_model.php +++ b/e107_handlers/user_model.php @@ -995,6 +995,7 @@ class e_user_model extends e_admin_model // TODO - add some more useful methods, sc_* methods support class e_system_user extends e_user_model { + public $debug = false; /** * Constructor * @@ -1047,17 +1048,36 @@ class e_system_user extends e_user_model } $eml = $this->renderEmail($type, $userInfo); - if(empty($eml)) return false; + + + + if(empty($eml)) + { + if($this->debug) + { + echo '$eml returned nothing'; + } + return false; + } + else + { + if($this->debug) + { + echo '

$eml array

'; + print_a($eml); + } + } $mailer = e107::getEmail(); $mailer->template = $eml['template']; - unset($eml['template']); + // Custom e107 Header if($userInfo['user_id']) { - $mailer->AddCustomHeader("X-e107-id: {$userInfo['user_id']}"); + $eml['e107_header'] = $userInfo['user_id']; + // $mailer->AddCustomHeader("X-e107-id: {$userInfo['user_id']}"); } return $mailer->sendEmail($userInfo['user_email'], $userInfo['user_name'], $eml, false); @@ -1082,7 +1102,9 @@ class e_system_user extends e_user_model { $pref = e107::getPref(); $ret = array(); + $tp = e107::getParser(); + // mailer options if(isset($userInfo['mail_options']) && is_array($userInfo['mail_options'])) { @@ -1092,41 +1114,58 @@ class e_system_user extends e_user_model // required for signup and quickadd email type e107::coreLan('signup'); - // FIXME convert to the new template to avoid include on every call - // BC - if (file_exists(THEME.'email_template.php')) + + + $EMAIL_TEMPLATE = e107::getCoreTemplate('email'); + + if(!is_array($EMAIL_TEMPLATE)) //BC Fixes. pre v2 alpha3. { - include(THEME.'email_template.php'); - } - else - { - // new standards - include(e107::coreTemplatePath('email')); + // load from old location. (root of theme folder if it exists) + if (file_exists(THEME.'email_template.php')) + { + include(THEME.'email_template.php'); + } + else + { + // include core default. + include(e107::coreTemplatePath('email')); + } + + // BC Fixes. + $EMAIL_TEMPLATE['signup']['subject'] = $SIGNUPEMAIL_SUBJECT; + $EMAIL_TEMPLATE['signup']['cc'] = $SIGNUPEMAIL_CC; + $EMAIL_TEMPLATE['signup']['bcc'] = $SIGNUPEMAIL_BCC; + $EMAIL_TEMPLATE['signup']['attachments']= $SIGNUPEMAIL_ATTACHMENTS; + + $EMAIL_TEMPLATE['signup']['body'] = $SIGNUPEMAIL_TEMPLATE; + + $EMAIL_TEMPLATE['quickadd']['body'] = $QUICKADDUSER_TEMPLATE['email_body']; + $EMAIL_TEMPLATE['notify']['body'] = $NOTIFY_TEMPLATE['email_body']; + } - // FIXME by SecretR - email template mess - there are changes to emails and templates that need to be implemented here $template = ''; switch ($type) { case 'signup': - if(vartrue($SIGNUPPROVIDEREMAIL_TEMPLATE)) $template = $SIGNUPPROVIDEREMAIL_TEMPLATE; - else $template = $SIGNUPEMAIL_TEMPLATE; - $ret['template'] = false; // Don't allow additional headers (mailer) + $template = (vartrue($SIGNUPPROVIDEREMAIL_TEMPLATE)) ? $SIGNUPPROVIDEREMAIL_TEMPLATE : $EMAIL_TEMPLATE['signup']['body']; + $ret['template'] = false;// 'signup'; // false; // Don't allow additional headers (mailer) ?? break; case 'quickadd': - $template = $QUICKADDUSER_TEMPLATE['email_body']; // XXX quick fix - add the email templating engine - $ret['template'] = 'email'; // Don't allow additional headers (mailer) + $template = $EMAIL_TEMPLATE['quickadd']['body']; + $ret['template'] = 'quickadd'; // Don't allow additional headers (mailer) break; - case 'notify': //emailer changes - if(vartrue($userInfo['mail_body'])) $template = $userInfo['mail_body'];//$NOTIFY_HEADER.$userInfo['mail_body'].$NOTIFY_FOOTER; + case 'notify': + if(vartrue($userInfo['mail_body'])) $template = $userInfo['mail_body']; //$NOTIFY_HEADER.$userInfo['mail_body'].$NOTIFY_FOOTER; $ret['template'] = 'notify'; break; - case 'email'://emailer changes + case 'email': + case 'default': if(vartrue($userInfo['mail_body'])) $template = $userInfo['mail_body']; //$EMAIL_HEADER.$userInfo['mail_body'].$EMAIL_FOOTER; - $ret['template'] = 'email'; + $ret['template'] = 'default'; break; } @@ -1137,11 +1176,15 @@ class e_system_user extends e_user_model // signup email only if($type == 'signup') { + $HEAD = ''; + $FOOT = ''; + + $ret['e107_header'] = $userInfo['user_id']; - if (vartrue($SIGNUPEMAIL_CC)) { $ret['email_copy_to'] = $SIGNUPEMAIL_CC; } - if (vartrue($SIGNUPEMAIL_BCC)) { $ret['email_bcopy_to'] = $SIGNUPEMAIL_BCC; } + if (vartrue($EMAIL_TEMPLATE['signup']['cc'])) { $ret['email_copy_to'] = $EMAIL_TEMPLATE['signup']['cc']; } + if (vartrue($EMAIL_TEMPLATE['signup']['bcc'])) { $ret['email_bcopy_to'] = $EMAIL_TEMPLATE['signup']['bcc']; } if (vartrue($userInfo['email_attach'])) { $ret['email_attach'] = $userInfo['mail_attach']; } - elseif (vartrue($SIGNUPEMAIL_ATTACHMENTS)) { $ret['email_attach'] = $SIGNUPEMAIL_ATTACHMENTS; } + elseif (vartrue($EMAIL_TEMPLATE['signup']['attachments'])) { $ret['email_attach'] = $EMAIL_TEMPLATE['signup']['attachments']; } $style = vartrue($SIGNUPEMAIL_LINKSTYLE) ? "style='{$SIGNUPEMAIL_LINKSTYLE}'" : ""; @@ -1164,7 +1207,7 @@ class e_system_user extends e_user_model $replace[5] = $userInfo['user_name']; $search[6] = '{USERURL}'; - $replace[6] = varsettrue($userInfo['user_website']) ? $userInfo['user_website'] : ""; + $replace[6] = vartrue($userInfo['user_website']) ? $userInfo['user_website'] : ""; $search[7] = '{DISPLAYNAME}'; $replace[7] = $userInfo['user_login'] ? $userInfo['user_login'] : $userInfo['user_name']; @@ -1175,32 +1218,50 @@ class e_system_user extends e_user_model $search[9] = '{ACTIVATION_URL}'; $replace[9] = $userInfo['activation_url']; - $subject = str_replace($search, $replace, $SIGNUPEMAIL_SUBJECT); + $subject = str_replace($search, $replace, $EMAIL_TEMPLATE['signup']['subject']); + $ret['email_subject'] = $subject; $ret['send_html'] = TRUE; - $HEAD = "\n"; - $HEAD .= "\n"; - $HEAD .= "\n"; - $HEAD .= ($SIGNUPEMAIL_USETHEME == 1) ? "\n" : ""; - $HEAD .= "".LAN_SIGNUP_58."\n"; - - if($SIGNUPEMAIL_USETHEME == 2) + if(!varset($EMAIL_TEMPLATE['signup']['header'])) { - $CSS = file_get_contents(THEME."style.css"); - $HEAD .= ""; - } - $HEAD .= "\n"; - if(vartrue($SIGNUPEMAIL_BACKGROUNDIMAGE)) - { - $HEAD .= "\n"; + $HEAD = "\n"; + $HEAD .= "\n"; + $HEAD .= "\n"; + $HEAD .= ($SIGNUPEMAIL_USETHEME == 1) ? "\n" : ""; + $HEAD .= "".LAN_SIGNUP_58."\n"; + + if($SIGNUPEMAIL_USETHEME == 2) // @deprecated in favor of {STYLESHEET} + { + $CSS = file_get_contents(THEME."style.css"); + $HEAD .= ""; + } + + $HEAD .= "\n"; + if(vartrue($SIGNUPEMAIL_BACKGROUNDIMAGE)) // @deprecated. + { + $HEAD .= "\n"; + } + else + { + $HEAD .= "\n"; + } + } else { - $HEAD .= "\n"; + $HEAD = $tp->parseTemplate($EMAIL_TEMPLATE['signup']['header'], true); + } + + if(!varset($EMAIL_TEMPLATE['signup']['footer'])) + { + $FOOT = "\n\n\n"; + } + else + { + $FOOT = $tp->parseTemplate($EMAIL_TEMPLATE['signup']['footer'], true); } - $FOOT = "\n\n\n"; $ret['send_html'] = TRUE; $ret['email_body'] = e107::getParser()->parseTemplate(str_replace($search,$replace,$HEAD.$template.$FOOT), true);