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