3 * @file src/Util/Emailer.php
5 namespace Friendica\Util;
7 use Friendica\Core\Config;
8 use Friendica\Core\Hook;
9 use Friendica\Core\Logger;
10 use Friendica\Core\PConfig;
11 use Friendica\Protocol\Email;
14 * @brief class to handle emailing
19 * Send a multipart/alternative message with Text and HTML versions
21 * @param array $params parameters
22 * fromName name of the sender
23 * fromEmail email of the sender
24 * replyTo address to direct responses
25 * toEmail destination email address
26 * messageSubject subject of the message
27 * htmlVersion html version of the message
28 * textVersion text only version of the message
29 * additionalMailHeader additions to the SMTP mail header
30 * optional uid user id of the destination user
33 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
35 public static function send($params)
37 Hook::callAll('emailer_send_prepare', $params);
39 $email_textonly = false;
40 if (!empty($params['uid'])) {
41 $email_textonly = PConfig::get($params['uid'], "system", "email_textonly");
44 $fromName = Email::encodeHeader(html_entity_decode($params['fromName'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
45 $messageSubject = Email::encodeHeader(html_entity_decode($params['messageSubject'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
47 // generate a mime boundary
48 $mimeBoundary =rand(0, 9)."-"
49 .rand(100000000, 999999999)."-"
50 .rand(100000000, 999999999)."=:"
53 // generate a multipart/alternative message header
54 $messageHeader = defaults($params, 'additionalMailHeader', '') .
55 "From: $fromName <{$params['fromEmail']}>\n" .
56 "Reply-To: $fromName <{$params['replyTo']}>\n" .
57 "MIME-Version: 1.0\n" .
58 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
60 // assemble the final multipart message body with the text and html types included
61 $textBody = chunk_split(base64_encode($params['textVersion']));
62 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
63 $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section
64 "Content-Type: text/plain; charset=UTF-8\n" .
65 "Content-Transfer-Encoding: base64\n\n" .
68 if (!$email_textonly && !is_null($params['htmlVersion'])) {
69 $multipartMessageBody .=
70 "--" . $mimeBoundary . "\n" . // text/html section
71 "Content-Type: text/html; charset=UTF-8\n" .
72 "Content-Transfer-Encoding: base64\n\n" .
75 $multipartMessageBody .=
76 "--" . $mimeBoundary . "--\n"; // message ending
78 if (Config::get("system", "sendmail_params", true)) {
79 $sendmail_params = '-f ' . $params['fromEmail'];
81 $sendmail_params = null;
86 'to' => $params['toEmail'],
87 'subject' => $messageSubject,
88 'body' => $multipartMessageBody,
89 'headers' => $messageHeader,
90 'parameters' => $sendmail_params
93 Hook::callAll("emailer_send", $hookdata);
100 $hookdata['parameters']
102 Logger::log("header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, Logger::DEBUG);
103 Logger::log("return value " . (($res)?"true":"false"), Logger::DEBUG);