3 * @file src/Util/Emailer.php
5 namespace Friendica\Util;
7 use Friendica\Core\Hook;
8 use Friendica\Core\Logger;
10 use Friendica\Protocol\Email;
13 * 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 function send(string $fromName, string $fromEmail, string $replyTo, string $toEmail,
35 string $subject, string $msgHtml, string $msgText,
36 string $additionalMailHeader = '', int $uid = null)
38 $params['sent'] = false;
40 Hook::callAll('emailer_send_prepare', $params);
42 if ($params['sent']) {
46 $email_textonly = false;
48 $email_textonly = DI::pConfig()->get($uid, "system", "email_textonly");
51 $fromName = Email::encodeHeader(html_entity_decode($fromName, ENT_QUOTES, 'UTF-8'), 'UTF-8');
52 $messageSubject = Email::encodeHeader(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'), 'UTF-8');
54 // generate a mime boundary
55 $mimeBoundary =rand(0, 9)."-"
56 .rand(100000000, 999999999)."-"
57 .rand(100000000, 999999999)."=:"
60 // generate a multipart/alternative message header
61 $messageHeader = $additionalMailHeader .
62 "From: $fromName <{$fromEmail}>\n" .
63 "Reply-To: $fromName <{$replyTo}>\n" .
64 "MIME-Version: 1.0\n" .
65 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
67 // assemble the final multipart message body with the text and html types included
68 $textBody = chunk_split(base64_encode($msgText));
69 $htmlBody = chunk_split(base64_encode($msgHtml));
70 $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section
71 "Content-Type: text/plain; charset=UTF-8\n" .
72 "Content-Transfer-Encoding: base64\n\n" .
75 if (!$email_textonly && !is_null($msgHtml)) {
76 $multipartMessageBody .=
77 "--" . $mimeBoundary . "\n" . // text/html section
78 "Content-Type: text/html; charset=UTF-8\n" .
79 "Content-Transfer-Encoding: base64\n\n" .
82 $multipartMessageBody .=
83 "--" . $mimeBoundary . "--\n"; // message ending
85 if (DI::config()->get("system", "sendmail_params", true)) {
86 $sendmail_params = '-f ' . $fromEmail;
88 $sendmail_params = null;
94 'subject' => $messageSubject,
95 'body' => $multipartMessageBody,
96 'headers' => $messageHeader,
97 'parameters' => $sendmail_params,
101 Hook::callAll("emailer_send", $hookdata);
103 if ($hookdata['sent']) {
109 $hookdata['subject'],
111 $hookdata['headers'],
112 $hookdata['parameters']
114 Logger::log("header " . 'To: ' . $toEmail . "\n" . $messageHeader, Logger::DEBUG);
115 Logger::log("return value " . (($res)?"true":"false"), Logger::DEBUG);