1
1
Fork 0

cleanup multipart email sending code

remove 'EmailNotification' class and rename 'enotify' class as 'Emailer'
This commit is contained in:
fabrixxm 2014-09-06 15:52:53 +02:00
parent 6679d18826
commit 16a07e6d83
3 changed files with 62 additions and 110 deletions

View file

@ -2,7 +2,7 @@
require_once('include/email.php'); require_once('include/email.php');
class EmailNotification { class Emailer {
/** /**
* Send a multipart/alternative message with Text and HTML versions * Send a multipart/alternative message with Text and HTML versions
* *
@ -13,12 +13,12 @@ class EmailNotification {
* @param messageSubject subject of the message * @param messageSubject subject of the message
* @param htmlVersion html version of the message * @param htmlVersion html version of the message
* @param textVersion text only 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($fromName,'UTF-8');
$messageSubject = email_header_encode($messageSubject,'UTF-8');
$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 // generate a mime boundary
$mimeBoundary =rand(0,9)."-" $mimeBoundary =rand(0,9)."-"
@ -28,14 +28,15 @@ class EmailNotification {
// generate a multipart/alternative message header // generate a multipart/alternative message header
$messageHeader = $messageHeader =
"From: {$fromName} <{$fromEmail}>\n" . $params['additionalMailHeader'] .
"Reply-To: {$replyTo}\n" . "From: $fromName <{$params['fromEmail']}>\n" .
"Reply-To: $fromName <{$params['replyTo']}>\n" .
"MIME-Version: 1.0\n" . "MIME-Version: 1.0\n" .
"Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\""; "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
// assemble the final multipart message body with the text and html types included // assemble the final multipart message body with the text and html types included
$textBody = chunk_split(base64_encode($textVersion)); $textBody = chunk_split(base64_encode($params['textVersion']));
$htmlBody = chunk_split(base64_encode($htmlVersion)); $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
$multipartMessageBody = $multipartMessageBody =
"--" . $mimeBoundary . "\n" . // plain text section "--" . $mimeBoundary . "\n" . // plain text section
"Content-Type: text/plain; charset=UTF-8\n" . "Content-Type: text/plain; charset=UTF-8\n" .
@ -49,12 +50,13 @@ class EmailNotification {
// send the message // send the message
$res = mail( $res = mail(
$toEmail, // send to address $params['toEmail'], // send to address
$messageSubject, // subject $messageSubject, // subject
$multipartMessageBody, // message body $multipartMessageBody, // message body
$messageHeader // message headers $messageHeader // message headers
); );
logger("sendTextHtmlEmail: END"); logger("header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, LOGGER_DEBUG);
logger("return value " . $res, LOGGER_DEBUG);
} }
} }
?> ?>

View file

@ -249,6 +249,12 @@ function email_header_encode($in_str, $charset) {
return $out_str; 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) { function email_send($addr, $subject, $headers, $item) {
//$headers .= 'MIME-Version: 1.0' . "\n"; //$headers .= 'MIME-Version: 1.0' . "\n";
//$headers .= 'Content-Type: text/html; charset=UTF-8' . "\n"; //$headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";

View file

@ -1,10 +1,12 @@
<?php <?php
require_once('include/Emailer.php');
require_once('include/email.php'); require_once('include/email.php');
require_once('include/bbcode.php');
require_once('include/html2bbcode.php');
function notification($params) { function notification($params) {
logger('notification: entry', LOGGER_DEBUG); #logger('notification()', LOGGER_DEBUG);
$a = get_app(); $a = get_app();
@ -13,27 +15,6 @@ function notification($params) {
push_lang($params['language']); push_lang($params['language']);
$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: <notification.".$hostname.">\n";
$additional_mail_header .= "List-Archive: <".$a->get_baseurl()."/notifications/system>\n";
if(array_key_exists('item',$params)) { if(array_key_exists('item',$params)) {
$title = $params['item']['title']; $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: <notification.".$hostname.">\n";
$additional_mail_header .= "List-Archive: <".$a->get_baseurl()."/notifications/system>\n";
$h = array( $h = array(
'params' => $params, 'params' => $params,
'subject' => $subject, 'subject' => $subject,
@ -274,7 +284,6 @@ function notification($params) {
$itemlink = $h['itemlink']; $itemlink = $h['itemlink'];
require_once('include/html2bbcode.php');
do { do {
$dups = false; $dups = false;
@ -304,7 +313,7 @@ function notification($params) {
if($datarray['abort']) { if($datarray['abort']) {
pop_lang(); pop_lang();
return; return False;
} }
// create notification entry in DB // create notification entry in DB
@ -332,7 +341,7 @@ function notification($params) {
$notify_id = $r[0]['id']; $notify_id = $r[0]['id'];
else { else {
pop_lang(); pop_lang();
return; return False;
} }
// we seem to have a lot of duplicate comment notifications due to race conditions, mostly from forums // 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']) { if($notify_id != $p[0]['id']) {
pop_lang(); pop_lang();
return; return False;
} }
} }
$itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id; $itemlink = $a->get_baseurl() . '/notify/view/' . $notify_id;
$msg = replace_macros($epreamble,array('$itemlink' => $itemlink)); $msg = replace_macros($epreamble,array('$itemlink' => $itemlink));
$r = q("update notify set msg = '%s' where id = %d and uid = %d", $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 // send email notification if notification preferences permit
require_once('include/bbcode.php');
if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) { if((intval($params['notify_flags']) & intval($params['type'])) || $params['type'] == NOTIFY_SYSTEM) {
logger('notification: sending notification email'); logger('notification: sending notification email');
@ -410,7 +412,7 @@ function notification($params) {
} else { } else {
// If not, just "follow" the thread. // If not, just "follow" the thread.
$additional_mail_header .= "References: <${id_for_parent}>\nIn-Reply-To: <${id_for_parent}>\n"; $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); // 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, 'fromName' => $sender_name,
'fromEmail' => $sender_email, 'fromEmail' => $sender_email,
'replyTo' => $sender_email, 'replyTo' => $sender_email,
@ -506,69 +508,11 @@ function notification($params) {
'textVersion' => $email_text_body, 'textVersion' => $email_text_body,
'additionalMailHeader' => $datarray['headers'], '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);
}
}
?> ?>