From 3a238bf62293314a55aadf4cf33eab7c2768da29 Mon Sep 17 00:00:00 2001 From: Cameron Date: Tue, 12 May 2015 01:45:00 -0700 Subject: [PATCH] Email notification improvements and fixes. (work in progress). --- e107_admin/mailout.php | 7 +- e107_core/templates/email_template.php | 95 +++++------------- e107_handlers/event_class.php | 2 + e107_handlers/mail.php | 3 + e107_handlers/mail_manager_class.php | 13 ++- e107_handlers/notify_class.php | 83 +++++++++++++--- e107_plugins/user/e_mailout.php | 97 ++++++++++++++++++ unsubscribe.php | 131 +++++++++++++++++++++++++ 8 files changed, 346 insertions(+), 85 deletions(-) create mode 100644 e107_plugins/user/e_mailout.php create mode 100644 unsubscribe.php diff --git a/e107_admin/mailout.php b/e107_admin/mailout.php index d438cc75e..bcd1811ac 100644 --- a/e107_admin/mailout.php +++ b/e107_admin/mailout.php @@ -294,8 +294,8 @@ class mailout_main_ui extends e_admin_ui 'mail_fail_count' => array('title' => LAN_MAILOUT_128, 'noedit'=>true, 'type'=>'number'), 'mail_bounce_count' => array('title' => LAN_MAILOUT_144, 'noedit'=>true, 'type'=>'number'), - 'mail_start_send' => array('title' => LAN_MAILOUT_131,'noedit'=>true, 'type'=>'number', 'proc' => 'sdatetime'), - 'mail_end_send' => array('title' => LAN_MAILOUT_132, 'noedit'=>true, 'type'=>'number', 'proc' => 'sdatetime'), + 'mail_start_send' => array('title' => LAN_MAILOUT_131,'noedit'=>true, 'type'=>'datestamp'), + 'mail_end_send' => array('title' => LAN_MAILOUT_132, 'noedit'=>true, 'type'=>'datestamp'), 'mail_create_date' => array('title' => LAN_MAILOUT_130, 'type'=>null, 'noedit'=>true, 'data'=>'int'), 'mail_creator' => array('title' => LAN_MAILOUT_85, 'type'=>null, 'noedit'=>true, 'data'=>'int'), 'mail_create_app' => array('title' => LAN_MAILOUT_133, 'type'=>null, 'noedit'=>true,'data'=>'str'), @@ -705,7 +705,8 @@ class mailout_main_ui extends e_admin_ui 'USERID' =>'555', 'MAILREF' => '123', 'NEWSLETTER' => SITEURL."newsletter/?id=example1234567", - 'UNSUBSCRIBE' => SITEURL."unsubscribe/?id=example1234567", + 'UNSUBSCRIBE' => SITEURL."unsubscribe.php?id=example1234567", + 'UNSUBSCRIBE_MESSAGE'=> "This email was sent to test@email.com. If you don't want to receive these emails in the future, please unsubscribe. ", 'ACTIVATION_LINK' => "http://whereever.to.activate.com/", 'USERURL' => "www.user-website.com", 'PASSWORD' => "test-password", diff --git a/e107_core/templates/email_template.php b/e107_core/templates/email_template.php index 61972402d..9084f4f6f 100644 --- a/e107_core/templates/email_template.php +++ b/e107_core/templates/email_template.php @@ -84,73 +84,6 @@ $EMAIL_OVERRIDES = array( ); */ -// Not used in signup email -/* -$EMAIL_HEADER = " - - - -{STYLESHEET} - - -
-"; - - -$EMAIL_BODY = 'Software malfunction - no email body text specified for template'; // Help debug - -// Not used in signup email -$EMAIL_FOOTER = " -

-{SITENAME=link} -
- -"; - - -$EMAIL_PLAINTEXT = ''; -*/ - - - -//------------------------------------------------------------- -// 'SIGNUP' TEMPLATE -//------------------------------------------------------------- -//@Deprecated -/* -$SIGNUPEMAIL_TEMPLATE = " -
-
-".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}
-{SITEURL} -

".($includeSiteButton ? "" : '')." -
-
-"; -*/ - - -// ----------------------- Everything above this line is deprecated ------------------------------- // - /** Standardized v2 template rewrite * @@ -174,6 +107,7 @@ $EMAIL_TEMPLATE['default']['header'] = " @@ -260,9 +194,30 @@ $EMAIL_TEMPLATE['quickadduser']['footer'] = $EMAIL_TEMPLATE['default']['footer' // ------- Notify (@see admin-> notify) $EMAIL_TEMPLATE['notify']['subject'] = '{SITENAME}: {SUBJECT} '; -$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. +$EMAIL_TEMPLATE['notify']['header'] = " + + + + + + + +
+ "; +$EMAIL_TEMPLATE['notify']['body'] = "
{MEDIA1}{BODY}
"; +$EMAIL_TEMPLATE['notify']['footer'] = "

+ {SITENAME=link} +
{UNSUBSCRIBE_MESSAGE}
+
+ + "; // ------ User-Specific Templates diff --git a/e107_handlers/event_class.php b/e107_handlers/event_class.php index 37b7cbdf9..03731674b 100644 --- a/e107_handlers/event_class.php +++ b/e107_handlers/event_class.php @@ -66,7 +66,9 @@ class e107_event 'admin_news_created' => NN_LAN_3, 'admin_news_updated' => NN_LAN_4, 'admin_news_deleted' => NN_LAN_5, + 'admin_news_subscribers' => "Notification of News Subscribers", // TODO LAN 'user_news_submit' => NN_LAN_2, + ), 'mail' => array( diff --git a/e107_handlers/mail.php b/e107_handlers/mail.php index ea63288ae..19c7ca126 100644 --- a/e107_handlers/mail.php +++ b/e107_handlers/mail.php @@ -527,6 +527,7 @@ class e107Email extends PHPMailer */ public function makeBody($message,$want_HTML = 1, $add_HTML_header = FALSE) { + switch (varset($this->general_opts['textonly'],'off')) { case 'pref' : // Disable HTML as default @@ -541,6 +542,8 @@ class e107Email extends PHPMailer if ($want_HTML !== FALSE) { + $message = e107::getParser()->toHtml("[html]".$message."[/html]",true); + if ($this->debug) echo "Generating multipart email
"; if ($add_HTML_header) { diff --git a/e107_handlers/mail_manager_class.php b/e107_handlers/mail_manager_class.php index 1b71206a7..fa1a05839 100644 --- a/e107_handlers/mail_manager_class.php +++ b/e107_handlers/mail_manager_class.php @@ -154,6 +154,7 @@ class e107MailManager 'mail_content' => array( 'mail_source_id' => 'int', 'mail_content_status' => 'int', + 'mail_total_count' => 'int', 'mail_togo_count' => 'int', 'mail_sent_count' => 'int', 'mail_fail_count' => 'int', @@ -170,7 +171,8 @@ class e107MailManager 'mail_subject' => 'todb', 'mail_body' => 'todb', 'mail_body_templated' => 'todb', - 'mail_other' => 'string' // Don't want entities here! + 'mail_other' => 'string', // Don't want entities here! + 'mail_media' => 'string' ) ); @@ -276,6 +278,12 @@ class e107MailManager } $res['mail_other'] = e107::serialize($res1,false); // Ready to write to DB + + if (!empty($res['mail_media'])) + { + $res['mail_media'] = e107::serialize($res['mail_media']); + } + return $res; } @@ -1808,7 +1816,10 @@ class e107MailManager // To many recipients to send at once - add to the emailing queue // @TODO - handle any other relevant $extra fields + $emailData['mail_total_count'] = count($recipientData); + $result = $this->saveEmail($emailData, TRUE); + if ($result === FALSE) { // TODO: Handle error diff --git a/e107_handlers/notify_class.php b/e107_handlers/notify_class.php index 90ee3f913..1b8ca6d07 100644 --- a/e107_handlers/notify_class.php +++ b/e107_handlers/notify_class.php @@ -117,20 +117,23 @@ class notify * @todo handle 'everyone except' clauses (email address filter done) * @todo set up pref to not notify originator of event which caused notify (see $blockOriginator) */ - function send($id, $subject, $message) + function send($id, $subject, $message, $media=array()) { $tp = e107::getParser(); $sql = e107::getDb(); $subject = $tp->toEmail(SITENAME.': '.$subject); - $message = $tp->toEmail($message); + $message = $tp->replaceConstants($message, "full"); + // $message = $tp->toEmail($message); $emailFilter = ''; $notifyTarget = $this->notify_prefs['event'][$id]['class']; + if ($notifyTarget == '-email') { $emailFilter = $this->notify_prefs['event'][$id]['email']; } + $blockOriginator = FALSE; // TODO: set this using a pref $recipients = array(); @@ -143,7 +146,7 @@ class notify ); } } - elseif (is_numeric($this->notify_prefs['event'][$id]['class'])) + elseif (is_numeric($notifyTarget)) { switch ($notifyTarget) { @@ -157,14 +160,17 @@ class notify $qry = "`user_ban` = 0"; break; default : - $qry = "user_ban = 0 AND user_class REGEXP '(^|,)(".$this->notify_prefs['event'][$id]['class'].")(,|$)'"; + $qry = "user_ban = 0 AND user_class REGEXP '(^|,)(".$notifyTarget.")(,|$)'"; break; } - $qry = 'SELECT user_id,user_name,user_email FROM `#user` WHERE '.$qry; + + $qry = 'SELECT user_id,user_name,user_email,user_join,user_lastvisit FROM `#user` WHERE '.$qry; + if ($blockOriginator) { $qry .= ' AND `user_id` != '.USERID; } + if (false !== ($count = $sql->gen($qry))) { // Now add email addresses to the list @@ -172,10 +178,28 @@ class notify { if ($row['user_email'] != $emailFilter) { - $recipients[] = array('mail_recipient_id' => $row['user_id'], - 'mail_recipient_name' => $row['user_name'], // Should this use realname? - 'mail_recipient_email' => $row['user_email'] - ); + + $unsubscribe = array('date'=>$row['user_join'],'email'=>$row['user_email'],'id'=>$row['user_id'], 'plugin'=>'user', 'userclass'=>$notifyTarget); + $urlQuery = http_build_query($unsubscribe,null,'&'); + $exclude = array(e_UC_MEMBER,e_UC_ADMIN, e_UC_MAINADMIN); // no unsubscribe for these classes. + $unsubUrl = SITEURL."unsubscribe.php?id=".base64_encode($urlQuery); + $unsubMessage = "This message was sent to ".$row['user_email'].". If you don't want to receive these emails in the future, please unsubscribe."; + + + $recipients[] = array( + 'mail_recipient_id' => $row['user_id'], + 'mail_recipient_name' => $row['user_name'], // Should this use realname? + 'mail_recipient_email' => $row['user_email'], + 'mail_target_info' => array( + 'USERID' => $row['user_id'], + 'DISPLAYNAME' => $row['user_name'], + // 'SIGNUP_LINK' => '', + 'USERNAME' => $row['user_name'], + 'USERLASTVISIT' => $row['user_lastvisit'], + 'UNSUBSCRIBE' => (!in_array($notifyTarget, $exclude)) ? $unsubUrl : '', + 'UNSUBSCRIBE_MESSAGE' => (!in_array($notifyTarget, $exclude)) ? $unsubMessage : '' + ) + ); } } } @@ -199,6 +223,7 @@ class notify // Create the mail body $mailData = array( + 'mail_total_count' => count($recipients), 'mail_content_status' => MAIL_STATUS_TEMP, 'mail_create_app' => 'notify', 'mail_title' => 'NOTIFY', @@ -207,10 +232,19 @@ class notify 'mail_sender_name' => e107::getPref('siteadmin'), 'mail_notify_complete' => 0, // NEVER notify when this email sent!!!!! 'mail_body' => $message, - 'template' => 'notify' + 'template' => 'notify', + 'mail_send_style' => 'notify' ); + + if(!empty($media) && is_array($media)) + { + foreach($media as $k=>$v) + { + $mailData['mail_media'][$k] = array('path'=>$v); + } + } - $result = $mailer->sendEmails('NOTIFY_TEMPLATE', $mailData, $recipients); + $result = $mailer->sendEmails('notify', $mailData, $recipients); e107::getLog()->e_log_event(10,-1,'NOTIFY',$subject,$message,FALSE,LOG_TO_ROLLING); } else @@ -336,6 +370,33 @@ class notify + function notify_admin_news_created($data) + { + $this->notify_newspost($data); + } + + + function notify_admin_news_subscribers($data) + { + $tp = e107::getParser(); + $url = e107::getUrl()->create('news/view/item', $data,'full=1'); + $message = "".$tp->toHtml($data['news_title']).""; + $img = explode(",",$data['news_thumbnail']); + + + if (vartrue($data['news_summary'])){ $message .= '

'.$tp->toEmail($data['news_summary']); } + + $message .= "View now"; + + $this->send('admin_news_updated', $data['news_title'], $message, $img); + + print_a($message); + return $message; + + } + + + } diff --git a/e107_plugins/user/e_mailout.php b/e107_plugins/user/e_mailout.php new file mode 100644 index 000000000..6a650472c --- /dev/null +++ b/e107_plugins/user/e_mailout.php @@ -0,0 +1,97 @@ +e107 = e107::getInstance(); + //$this->adminHandler = e107::getRegistry('_mailout_admin'); // Get the mailer admin object - we want to use some of its functions + } + + + /** + * Manage Bounces. + */ + public function bounce($data) + { + e107::getLog()->add('Newsletter Bounce', $data, E_LOG_INFORMATIVE, 'BOUNCE'); + } + + + /** + * @param $mode - check || process + * @param array $data - usually email, date, id - but dependent on unsubscribe link above. + */ + function unsubscribe($mode, $data=null) + { + if($mode == 'check') // check that a matching email,id,creation-date exists. + { + return e107::getDb()->select('user','*', 'user_class!="" AND user_id='.intval($data['id'])." AND user_join=".intval($data['date'])." AND user_email=\"".$data['email']."\"", true); + } + + print_a($data); + + if($mode == 'process') // Update record. Return true on success, and false on error. + { + $update = array( + 'cust_subscribed' => 0, + 'cust_unsubscribed' => time(), + 'WHERE' => "user_id=".intval($data['id'])." AND cust_datestamp=".intval($data['date'])." AND cust_email=\"".$data['email']."\"" + ); + + // return e107::getDb()->update('user',$update); + } + + } + + +} + + + +?> \ No newline at end of file diff --git a/unsubscribe.php b/unsubscribe.php new file mode 100644 index 000000000..0e8ef1715 --- /dev/null +++ b/unsubscribe.php @@ -0,0 +1,131 @@ +simulation(); + + $mailoutPlugins = e107::getConfig()->get('e_mailout_list'); + + if(!vartrue($_GET['id'])) + { + return; + } + + $tmp = base64_decode($_GET['id']); + + parse_str($tmp,$data); + + e107::getMessage()->addDebug(print_a($data,true)); + + $plugin = vartrue($data['plugin'],false); + + + if(empty($data) || !e107::isInstalled($plugin) || !in_array($plugin, $mailoutPlugins)) + { + $this->invalidURL(); + return; + } + + $ml = e107::getAddon($plugin,'e_mailout'); + + if(vartrue($_POST['remove']) && !empty($data)) + { + if($ml->unsubscribe('process',$data)!=false) + { + $text = "

".$data['email']." has been removed from ".$ml->mailerName.".

"; + $mes->addSuccess($text); + } + else + { + $text = "

There was a problem when attempting to remove ".$data['email']." from ".$ml->mailerName.".

"; + $mes->addError($text); + } + + echo "
".$mes->render()."
"; + return; + } + + if($ml->unsubscribe('check',$data) != false) + { + $text = "

We are very sorry for the inconvenience.
Please click the button below to remove ".$data['email']." from the ".$ml->mailerName.".

"; + $text .= $frm->open('unsub','post',e_REQUEST_URI); + $text .= $frm->button('remove','Remove ','submit'); + $text .= $frm->close(); + + $mes->setTitle('Unsubscribe',E_MESSAGE_INFO)->addInfo($text); + + echo "
".$mes->render()."
"; + return; + + } + else + { + $this->invalidURL(); + return; + } + } + + + + + function simulation() + { + $row = array(); + $row['datestamp'] = time(); + $row['email'] = "test@test.com"; + $row['id'] = 23; + + $unsubscribe = array('date'=>$row['datestamp'],'email'=>$row['email'],'id'=>$row['id'],'plugin'=>'user'); + + $urlQuery = http_build_query($unsubscribe,null,'&'); + + $_GET['id'] = base64_encode($urlQuery); + + e107::getMessage()->addDebug("urlQuery = ".$urlQuery); + //echo "urlQuery = ".$urlQuery."
"; + + e107::getMessage()->addDebug(e_SELF."?id=".$_GET['id']); + + } + + + + + function invalidURL() + { + $mes = e107::getMessage(); + $mes->addWarning("Invalid URL"); + echo "
".$mes->render()."
"; + return; + + } + + +} + + +new unsubscribe; + + + + + +require_once(FOOTERF); +exit; + + +?> \ No newline at end of file