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