3 * @file src/Util/Emailer.php
5 namespace Friendica\Util;
7 use Friendica\Core\Addon;
8 use Friendica\Core\PConfig;
9 use Friendica\Protocol\Email;
12 * @brief class to handle emailing
17 * Send a multipart/alternative message with Text and HTML versions
19 * @param array $params parameters
20 * fromName name of the sender
21 * fromEmail email fo the sender
22 * replyTo replyTo address to direct responses
23 * toEmail destination email address
24 * messageSubject subject of the message
25 * htmlVersion html version of the message
26 * textVersion text only version of the message
27 * additionalMailHeader additions to the smtp mail header
28 * optional uid user id of the destination user
32 public static function send($params)
34 Addon::callHooks('emailer_send_prepare', $params);
36 $email_textonly = false;
37 if (x($params, "uid")) {
38 $email_textonly = PConfig::get($params['uid'], "system", "email_textonly");
41 $fromName = Email::encodeHeader(html_entity_decode($params['fromName'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
42 $messageSubject = Email::encodeHeader(html_entity_decode($params['messageSubject'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
44 // generate a mime boundary
45 $mimeBoundary =rand(0, 9)."-"
46 .rand(100000000, 999999999)."-"
47 .rand(100000000, 999999999)."=:"
50 // generate a multipart/alternative message header
51 $messageHeader = $params['additionalMailHeader'] .
52 "From: $fromName <{$params['fromEmail']}>\n" .
53 "Reply-To: $fromName <{$params['replyTo']}>\n" .
54 "MIME-Version: 1.0\n" .
55 "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
57 // assemble the final multipart message body with the text and html types included
58 $textBody = chunk_split(base64_encode($params['textVersion']));
59 $htmlBody = chunk_split(base64_encode($params['htmlVersion']));
60 $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section
61 "Content-Type: text/plain; charset=UTF-8\n" .
62 "Content-Transfer-Encoding: base64\n\n" .
65 if (!$email_textonly && !is_null($params['htmlVersion'])) {
66 $multipartMessageBody .=
67 "--" . $mimeBoundary . "\n" . // text/html section
68 "Content-Type: text/html; charset=UTF-8\n" .
69 "Content-Transfer-Encoding: base64\n\n" .
72 $multipartMessageBody .=
73 "--" . $mimeBoundary . "--\n"; // message ending
77 'to' => $params['toEmail'],
78 'subject' => $messageSubject,
79 'body' => $multipartMessageBody,
80 'headers' => $messageHeader
82 //echo "<pre>"; var_dump($hookdata); killme();
83 Addon::callHooks("emailer_send", $hookdata);
85 $hookdata['to'], // send to address
86 $hookdata['subject'], // subject
87 $hookdata['body'], // message body
88 $hookdata['headers'] // message headers
90 logger("header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, LOGGER_DEBUG);
91 logger("return value " . (($res)?"true":"false"), LOGGER_DEBUG);