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\Protocol\Email;
13 * @brief class to handle emailing
18 * Send a multipart/alternative message with Text and HTML versions
20 * @param array $params parameters
21 * fromName name of the sender
22 * fromEmail email of the sender
23 * replyTo address to direct responses
24 * toEmail destination email address
25 * messageSubject subject of the message
26 * htmlVersion html version of the message
27 * textVersion text only version of the message
28 * additionalMailHeader additions to the SMTP mail header
29 * optional uid user id of the destination user
32 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
34 public static function send(array $params)
36 $params['sent'] = false;
38 Hook::callAll('emailer_send_prepare', $params);
40 if ($params['sent']) {
44 $email_textonly = false;
45 if (!empty($params['uid'])) {
46 $email_textonly = DI::pConfig()->get($params['uid'], "system", "email_textonly");
49 $fromName = Email::encodeHeader(html_entity_decode($params['fromName'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
50 $messageSubject = Email::encodeHeader(html_entity_decode($params['messageSubject'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
52 // generate a mime boundary
53 $mimeBoundary =rand(0, 9)."-"
54 .rand(100000000, 999999999)."-"
55 .rand(100000000, 999999999)."=:"
58 // generate a multipart/alternative message header
59 $messageHeader = ($params['additionalMailHeader'] ?? '') .
60 "From: $fromName <{$params['fromEmail']}>\n" .
61 "Reply-To: $fromName <{$params['replyTo']}>\n" .
62 "MIME-Version: 1.0\n" .
63 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
65 // assemble the final multipart message body with the text and html types included
66 $textBody = chunk_split(base64_encode($params['textVersion']));
67 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
68 $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section
69 "Content-Type: text/plain; charset=UTF-8\n" .
70 "Content-Transfer-Encoding: base64\n\n" .
73 if (!$email_textonly && !is_null($params['htmlVersion'])) {
74 $multipartMessageBody .=
75 "--" . $mimeBoundary . "\n" . // text/html section
76 "Content-Type: text/html; charset=UTF-8\n" .
77 "Content-Transfer-Encoding: base64\n\n" .
80 $multipartMessageBody .=
81 "--" . $mimeBoundary . "--\n"; // message ending
83 if (Config::get("system", "sendmail_params", true)) {
84 $sendmail_params = '-f ' . $params['fromEmail'];
86 $sendmail_params = null;
91 'to' => $params['toEmail'],
92 'subject' => $messageSubject,
93 'body' => $multipartMessageBody,
94 'headers' => $messageHeader,
95 'parameters' => $sendmail_params,
99 Hook::callAll("emailer_send", $hookdata);
101 if ($hookdata['sent']) {
107 $hookdata['subject'],
109 $hookdata['headers'],
110 $hookdata['parameters']
112 Logger::log("header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, Logger::DEBUG);
113 Logger::log("return value " . (($res)?"true":"false"), Logger::DEBUG);