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