2011-02-19 08:33:42 -05:00
|
|
|
<?php
|
2011-09-18 07:34:17 -04:00
|
|
|
|
|
|
|
require_once('include/email.php');
|
|
|
|
|
2014-09-06 09:52:53 -04:00
|
|
|
class Emailer {
|
2011-02-19 08:33:42 -05:00
|
|
|
/**
|
|
|
|
* 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
|
2014-09-06 09:52:53 -04:00
|
|
|
* @param additionalMailHeader additions to the smtp mail header
|
2014-12-29 10:04:51 -05:00
|
|
|
* @param optional uid user id of the destination user
|
2011-02-19 08:33:42 -05:00
|
|
|
*/
|
2014-09-06 09:52:53 -04:00
|
|
|
static public function send($params) {
|
|
|
|
|
2014-12-29 09:44:30 -05:00
|
|
|
call_hooks('emailer_send_prepare', $params);
|
|
|
|
|
2015-01-20 13:19:58 -05:00
|
|
|
$email_textonly = False;
|
|
|
|
if (x($params,"uid")) {
|
|
|
|
$email_textonly = get_pconfig($params['uid'], "system", "email_textonly");
|
|
|
|
}
|
|
|
|
|
2014-09-06 09:52:53 -04:00
|
|
|
$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');
|
2011-09-18 07:34:17 -04:00
|
|
|
|
2011-02-19 08:33:42 -05:00
|
|
|
// generate a mime boundary
|
|
|
|
$mimeBoundary =rand(0,9)."-"
|
|
|
|
.rand(10000000000,9999999999)."-"
|
|
|
|
.rand(10000000000,9999999999)."=:"
|
|
|
|
.rand(10000,99999);
|
|
|
|
|
|
|
|
// generate a multipart/alternative message header
|
|
|
|
$messageHeader =
|
2014-09-06 09:52:53 -04:00
|
|
|
$params['additionalMailHeader'] .
|
|
|
|
"From: $fromName <{$params['fromEmail']}>\n" .
|
|
|
|
"Reply-To: $fromName <{$params['replyTo']}>\n" .
|
2011-02-19 08:33:42 -05:00
|
|
|
"MIME-Version: 1.0\n" .
|
|
|
|
"Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
|
|
|
|
|
|
|
|
// assemble the final multipart message body with the text and html types included
|
2014-09-06 09:52:53 -04:00
|
|
|
$textBody = chunk_split(base64_encode($params['textVersion']));
|
|
|
|
$htmlBody = chunk_split(base64_encode($params['htmlVersion']));
|
2011-02-19 08:33:42 -05:00
|
|
|
$multipartMessageBody =
|
|
|
|
"--" . $mimeBoundary . "\n" . // plain text section
|
|
|
|
"Content-Type: text/plain; charset=UTF-8\n" .
|
|
|
|
"Content-Transfer-Encoding: base64\n\n" .
|
2015-01-20 13:19:58 -05:00
|
|
|
$textBody . "\n";
|
|
|
|
|
|
|
|
if (!$email_textonly && !is_null($htmlBody)){
|
|
|
|
$multipartMessageBody .=
|
|
|
|
"--" . $mimeBoundary . "\n" . // text/html section
|
|
|
|
"Content-Type: text/html; charset=UTF-8\n" .
|
|
|
|
"Content-Transfer-Encoding: base64\n\n" .
|
|
|
|
$htmlBody . "\n";
|
|
|
|
}
|
|
|
|
$multipartMessageBody .=
|
2011-02-19 08:33:42 -05:00
|
|
|
"--" . $mimeBoundary . "--\n"; // message ending
|
|
|
|
|
|
|
|
// send the message
|
2014-12-29 09:44:30 -05:00
|
|
|
$hookdata = array(
|
|
|
|
'to' => $params['toEmail'],
|
|
|
|
'subject' => $messageSubject,
|
|
|
|
'body' => $multipartMessageBody,
|
|
|
|
'headers' => $messageHeader
|
|
|
|
);
|
2015-01-20 13:19:58 -05:00
|
|
|
echo "<pre>"; var_dump($hookdata); killme();
|
2014-12-29 09:44:30 -05:00
|
|
|
call_hooks("emailer_send", $hookdata);
|
2011-02-19 08:33:42 -05:00
|
|
|
$res = mail(
|
2014-12-29 09:44:30 -05:00
|
|
|
$hookdata['to'], // send to address
|
|
|
|
$hookdata['subject'], // subject
|
|
|
|
$hookdata['body'], // message body
|
2014-12-29 10:40:04 -05:00
|
|
|
$hookdata['headers'] // message headers
|
2011-02-19 08:33:42 -05:00
|
|
|
);
|
2014-09-06 09:52:53 -04:00
|
|
|
logger("header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, LOGGER_DEBUG);
|
2014-09-07 05:20:06 -04:00
|
|
|
logger("return value " . (($res)?"true":"false"), LOGGER_DEBUG);
|
2014-09-07 11:46:31 -04:00
|
|
|
return $res;
|
2011-02-19 08:33:42 -05:00
|
|
|
}
|
|
|
|
}
|
2014-09-06 09:52:53 -04:00
|
|
|
?>
|