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(array $params)
37 $params['sent'] = false;
39 Hook::callAll('emailer_send_prepare', $params);
41 if ($params['sent']) {
45 $email_textonly = false;
46 if (!empty($params['uid'])) {
47 $email_textonly = PConfig::get($params['uid'], "system", "email_textonly");
50 $fromName = Email::encodeHeader(html_entity_decode($params['fromName'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
51 $messageSubject = Email::encodeHeader(html_entity_decode($params['messageSubject'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
53 // generate a mime boundary
54 $mimeBoundary =rand(0, 9)."-"
55 .rand(100000000, 999999999)."-"
56 .rand(100000000, 999999999)."=:"
59 // generate a multipart/alternative message header
60 $messageHeader = ($params['additionalMailHeader'] ?? '') .
61 "From: $fromName <{$params['fromEmail']}>\n" .
62 "Reply-To: $fromName <{$params['replyTo']}>\n" .
63 "MIME-Version: 1.0\n" .
64 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
66 // assemble the final multipart message body with the text and html types included
67 $textBody = chunk_split(base64_encode($params['textVersion']));
68 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
69 $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section
70 "Content-Type: text/plain; charset=UTF-8\n" .
71 "Content-Transfer-Encoding: base64\n\n" .
74 if (!$email_textonly && !is_null($params['htmlVersion'])) {
75 $multipartMessageBody .=
76 "--" . $mimeBoundary . "\n" . // text/html section
77 "Content-Type: text/html; charset=UTF-8\n" .
78 "Content-Transfer-Encoding: base64\n\n" .
81 $multipartMessageBody .=
82 "--" . $mimeBoundary . "--\n"; // message ending
84 if (Config::get("system", "sendmail_params", true)) {
85 $sendmail_params = '-f ' . $params['fromEmail'];
87 $sendmail_params = null;
92 'to' => $params['toEmail'],
93 'subject' => $messageSubject,
94 'body' => $multipartMessageBody,
95 'headers' => $messageHeader,
96 'parameters' => $sendmail_params,
100 Hook::callAll("emailer_send", $hookdata);
102 if ($hookdata['sent']) {
108 $hookdata['subject'],
110 $hookdata['headers'],
111 $hookdata['parameters']
113 Logger::log("header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, Logger::DEBUG);
114 Logger::log("return value " . (($res)?"true":"false"), Logger::DEBUG);