From 16a07e6d83dc4eff600bf1c12c0d638977ab44d6 Mon Sep 17 00:00:00 2001 From: fabrixxm Date: Sat, 6 Sep 2014 15:52:53 +0200 Subject: [PATCH] cleanup multipart email sending code remove 'EmailNotification' class and rename 'enotify' class as 'Emailer' --- .../{EmailNotification.php => Emailer.php} | 28 ++-- include/email.php | 6 + include/enotify.php | 138 ++++++------------ 3 files changed, 62 insertions(+), 110 deletions(-) rename include/{EmailNotification.php => Emailer.php} (65%) diff --git a/include/EmailNotification.php b/include/Emailer.php similarity index 65% rename from include/EmailNotification.php rename to include/Emailer.php index 8861e8f5d8..a5b600e36f 100644 --- a/include/EmailNotification.php +++ b/include/Emailer.php @@ -2,7 +2,7 @@ require_once('include/email.php'); -class EmailNotification { +class Emailer { /** * Send a multipart/alternative message with Text and HTML versions * @@ -13,13 +13,13 @@ class EmailNotification { * @param messageSubject subject of the message * @param htmlVersion html version of the message * @param textVersion text only version of the message + * @param additionalMailHeader additions to the smtp mail header */ - static public function sendTextHtmlEmail($fromName,$fromEmail,$replyTo,$toEmail,$messageSubject,$htmlVersion,$textVersion) { + static public function send($params) { + + $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8'); + $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8'); - $fromName = email_header_encode($fromName,'UTF-8'); - $messageSubject = email_header_encode($messageSubject,'UTF-8'); - - // generate a mime boundary $mimeBoundary =rand(0,9)."-" .rand(10000000000,9999999999)."-" @@ -28,14 +28,15 @@ class EmailNotification { // generate a multipart/alternative message header $messageHeader = - "From: {$fromName} <{$fromEmail}>\n" . - "Reply-To: {$replyTo}\n" . + $params['additionalMailHeader'] . + "From: $fromName <{$params['fromEmail']}>\n" . + "Reply-To: $fromName <{$params['replyTo']}>\n" . "MIME-Version: 1.0\n" . "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\""; // assemble the final multipart message body with the text and html types included - $textBody = chunk_split(base64_encode($textVersion)); - $htmlBody = chunk_split(base64_encode($htmlVersion)); + $textBody = chunk_split(base64_encode($params['textVersion'])); + $htmlBody = chunk_split(base64_encode($params['htmlVersion'])); $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section "Content-Type: text/plain; charset=UTF-8\n" . @@ -49,12 +50,13 @@ class EmailNotification { // send the message $res = mail( - $toEmail, // send to address + $params['toEmail'], // send to address $messageSubject, // subject $multipartMessageBody, // message body $messageHeader // message headers ); - logger("sendTextHtmlEmail: END"); + logger("header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, LOGGER_DEBUG); + logger("return value " . $res, LOGGER_DEBUG); } } -?> \ No newline at end of file +?> diff --git a/include/email.php b/include/email.php index dec8c93db7..0f24a42497 100644 --- a/include/email.php +++ b/include/email.php @@ -249,6 +249,12 @@ function email_header_encode($in_str, $charset) { return $out_str; } +/** + * email_send is used by NETWORK_EMAIL and NETWORK_EMAIL2 code + * (not to notify the user, but to send items to email contacts + * + * TODO: this could be changed to use the Emailer class + */ function email_send($addr, $subject, $headers, $item) { //$headers .= 'MIME-Version: 1.0' . "\n"; //$headers .= 'Content-Type: text/html; charset=UTF-8' . "\n"; diff --git a/include/enotify.php b/include/enotify.php index 970ece82eb..2dca5a609c 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -1,10 +1,12 @@ get_baseurl(true); - $thanks = t('Thank You,'); - $sitename = $a->config['sitename']; - $site_admin = sprintf( t('%s Administrator'), $sitename); - - $sender_name = $product; - $hostname = $a->get_hostname(); - if(strpos($hostname,':')) - $hostname = substr($hostname,0,strpos($hostname,':')); - - $sender_email = t('noreply') . '@' . $hostname; - $additional_mail_header = ""; - - $additional_mail_header .= "Precedence: list\n"; - $additional_mail_header .= "X-Friendica-Host: ".$hostname."\n"; - $additional_mail_header .= "X-Friendica-Platform: ".FRIENDICA_PLATFORM."\n"; - $additional_mail_header .= "X-Friendica-Version: ".FRIENDICA_VERSION."\n"; - $additional_mail_header .= "List-ID: \n"; - $additional_mail_header .= "List-Archive: <".$a->get_baseurl()."/notifications/system>\n"; if(array_key_exists('item',$params)) { $title = $params['item']['title']; @@ -250,6 +231,35 @@ function notification($params) { } + + /*$email = prepare_notificaion_mail($params, $subject, $preamble, $body, $sitelink, $tsitelink, $hsitelink, $itemlink); + if ($email) Emailer::send($email); + pop_lang();*/ + + + $banner = t('Friendica Notification'); + $product = FRIENDICA_PLATFORM; + $siteurl = $a->get_baseurl(true); + $thanks = t('Thank You,'); + $sitename = $a->config['sitename']; + $site_admin = sprintf( t('%s Administrator'), $sitename); + + $sender_name = $product; + $hostname = $a->get_hostname(); + if(strpos($hostname,':')) + $hostname = substr($hostname,0,strpos($hostname,':')); + + $sender_email = t('noreply') . '@' . $hostname; + + + $additional_mail_header = ""; + $additional_mail_header .= "Precedence: list\n"; + $additional_mail_header .= "X-Friendica-Host: ".$hostname."\n"; + $additional_mail_header .= "X-Friendica-Platform: ".FRIENDICA_PLATFORM."\n"; + $additional_mail_header .= "X-Friendica-Version: ".FRIENDICA_VERSION."\n"; + $additional_mail_header .= "List-ID: \n"; + $additional_mail_header .= "List-Archive: <".$a->get_baseurl()."/notifications/system>\n"; + $h = array( 'params' => $params, 'subject' => $subject, @@ -274,7 +284,6 @@ function notification($params) { $itemlink = $h['itemlink']; - require_once('include/html2bbcode.php'); do { $dups = false; @@ -304,7 +313,7 @@ function notification($params) { if($datarray['abort']) { pop_lang(); - return; + return False; } // create notification entry in DB @@ -332,7 +341,7 @@ function notification($params) { $notify_id = $r[0]['id']; else { pop_lang(); - return; + return False; } // we seem to have a lot of duplicate comment notifications due to race conditions, mostly from forums @@ -356,17 +365,11 @@ function notification($params) { if($notify_id != $p[0]['id']) { pop_lang(); - return; + return False; } } - - - - - - $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id; $msg = replace_macros($epreamble,array('$itemlink' => $itemlink)); $r = q("update notify set msg = '%s' where id = %d and uid = %d", @@ -378,7 +381,6 @@ function notification($params) { // send email notification if notification preferences permit - require_once('include/bbcode.php'); if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) { logger('notification: sending notification email'); @@ -410,7 +412,7 @@ function notification($params) { } else { // If not, just "follow" the thread. $additional_mail_header .= "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n"; - logger("include/enotify: There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG); + logger("There's already a notification for this parent:\n" . print_r($r, true), LOGGER_DEBUG); } } @@ -494,9 +496,9 @@ function notification($params) { // logger('text: ' . $email_text_body); - // use the EmailNotification library to send the message + // use the Emailer class to send the message - enotify::send(array( + Emailer::send(array( 'fromName' => $sender_name, 'fromEmail' => $sender_email, 'replyTo' => $sender_email, @@ -506,69 +508,11 @@ function notification($params) { 'textVersion' => $email_text_body, 'additionalMailHeader' => $datarray['headers'], )); + return True; } - pop_lang(); + return False; } -require_once('include/email.php'); - -class enotify { - /** - * Send a multipart/alternative message with Text and HTML versions - * - * @param fromName name of the sender - * @param fromEmail email fo the sender - * @param replyTo replyTo address to direct responses - * @param toEmail destination email address - * @param messageSubject subject of the message - * @param htmlVersion html version of the message - * @param textVersion text only version of the message - * @param additionalMailHeader additions to the smtp mail header - */ - static public function send($params) { - - $fromName = email_header_encode(html_entity_decode($params['fromName'],ENT_QUOTES,'UTF-8'),'UTF-8'); - $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'],ENT_QUOTES,'UTF-8'),'UTF-8'); - - // generate a mime boundary - $mimeBoundary =rand(0,9)."-" - .rand(10000000000,9999999999)."-" - .rand(10000000000,9999999999)."=:" - .rand(10000,99999); - - // generate a multipart/alternative message header - $messageHeader = - $params['additionalMailHeader'] . - "From: $fromName <{$params['fromEmail']}>\n" . - "Reply-To: $fromName <{$params['replyTo']}>\n" . - "MIME-Version: 1.0\n" . - "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\""; - - // assemble the final multipart message body with the text and html types included - $textBody = chunk_split(base64_encode($params['textVersion'])); - $htmlBody = chunk_split(base64_encode($params['htmlVersion'])); - $multipartMessageBody = - "--" . $mimeBoundary . "\n" . // plain text section - "Content-Type: text/plain; charset=UTF-8\n" . - "Content-Transfer-Encoding: base64\n\n" . - $textBody . "\n" . - "--" . $mimeBoundary . "\n" . // text/html section - "Content-Type: text/html; charset=UTF-8\n" . - "Content-Transfer-Encoding: base64\n\n" . - $htmlBody . "\n" . - "--" . $mimeBoundary . "--\n"; // message ending - - // send the message - $res = mail( - $params['toEmail'], // send to address - $messageSubject, // subject - $multipartMessageBody, // message body - $messageHeader // message headers - ); - logger("notification: enotify::send header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, LOGGER_DEBUG); - logger("notification: enotify::send returns " . $res, LOGGER_DEBUG); - } -} ?>