3 * @file src/Util/Emailer.php
5 namespace Friendica\Util;
7 use Friendica\Core\Addon;
8 use Friendica\Core\Config;
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 fo the sender
24 * replyTo 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
34 public static function send($params)
36 Addon::callHooks('emailer_send_prepare', $params);
38 $email_textonly = false;
39 if (x($params, "uid")) {
40 $email_textonly = PConfig::get($params['uid'], "system", "email_textonly");
43 $fromName = Email::encodeHeader(html_entity_decode($params['fromName'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
44 $messageSubject = Email::encodeHeader(html_entity_decode($params['messageSubject'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
46 // generate a mime boundary
47 $mimeBoundary =rand(0, 9)."-"
48 .rand(100000000, 999999999)."-"
49 .rand(100000000, 999999999)."=:"
52 // generate a multipart/alternative message header
53 $messageHeader = defaults($params, 'additionalMailHeader', '') .
54 "From: $fromName <{$params['fromEmail']}>\n" .
55 "Reply-To: $fromName <{$params['replyTo']}>\n" .
56 "MIME-Version: 1.0\n" .
57 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
59 // assemble the final multipart message body with the text and html types included
60 $textBody = chunk_split(base64_encode($params['textVersion']));
61 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
62 $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section
63 "Content-Type: text/plain; charset=UTF-8\n" .
64 "Content-Transfer-Encoding: base64\n\n" .
67 if (!$email_textonly && !is_null($params['htmlVersion'])) {
68 $multipartMessageBody .=
69 "--" . $mimeBoundary . "\n" . // text/html section
70 "Content-Type: text/html; charset=UTF-8\n" .
71 "Content-Transfer-Encoding: base64\n\n" .
74 $multipartMessageBody .=
75 "--" . $mimeBoundary . "--\n"; // message ending
77 if (Config::get("system", "sendmail_params", true)) {
78 $sendmail_params = '-f ' . $params['fromEmail'];
80 $sendmail_params = null;
85 'to' => $params['toEmail'],
86 'subject' => $messageSubject,
87 'body' => $multipartMessageBody,
88 'headers' => $messageHeader,
89 'parameters' => $sendmail_params
91 //echo "<pre>"; var_dump($hookdata); killme();
92 Addon::callHooks("emailer_send", $hookdata);
98 $hookdata['parameters']
100 Logger::log("header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, Logger::DEBUG);
101 Logger::log("return value " . (($res)?"true":"false"), Logger::DEBUG);