]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Emailer.php
Merge pull request #7248 from nupplaphil/bugs/6916-fatal-network
[friendica.git] / src / Util / Emailer.php
index a2be983d6c03cb7ca78645831b5b836639161700..4310046c234411b1f6fd6907f8613a4075ebc7c3 100644 (file)
@@ -4,12 +4,14 @@
  */
 namespace Friendica\Util;
 
+use Friendica\Core\Config;
+use Friendica\Core\Hook;
+use Friendica\Core\Logger;
 use Friendica\Core\PConfig;
-
-require_once 'include/email.php';
+use Friendica\Protocol\Email;
 
 /**
- * @breif class to handle emailing
+ * @brief class to handle emailing
  */
 class Emailer
 {
@@ -17,29 +19,36 @@ class Emailer
         * Send a multipart/alternative message with Text and HTML versions
         *
         * @param array $params parameters
-        *                      fromName name of the sender
-        *                      fromEmail                        email fo the sender
-        *                      replyTo                      replyTo address to direct responses
-        *                      toEmail                      destination email address
-        *                      messageSubject       subject of the message
-        *                      htmlVersion                  html version of the message
-        *                      textVersion                  text only version of the message
-        *                      additionalMailHeader additions to the smtp mail header
+        *                      fromName             name of the sender
+        *                      fromEmail            email of the sender
+        *                      replyTo              address to direct responses
+        *                      toEmail              destination email address
+        *                      messageSubject       subject of the message
+        *                      htmlVersion          html version of the message
+        *                      textVersion          text only version of the message
+        *                      additionalMailHeader additions to the SMTP mail header
         *                      optional             uid user id of the destination user
         *
-        * @return object
+        * @return bool
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public static function send($params)
+       public static function send(array $params)
        {
-               call_hooks('emailer_send_prepare', $params);
+               $params['sent'] = false;
+
+               Hook::callAll('emailer_send_prepare', $params);
+
+               if ($params['sent']) {
+                       return true;
+               }
 
                $email_textonly = false;
-               if (x($params, "uid")) {
+               if (!empty($params['uid'])) {
                        $email_textonly = PConfig::get($params['uid'], "system", "email_textonly");
                }
 
-               $fromName = email_header_encode(html_entity_decode($params['fromName'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
-               $messageSubject = email_header_encode(html_entity_decode($params['messageSubject'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
+               $fromName = Email::encodeHeader(html_entity_decode($params['fromName'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
+               $messageSubject = Email::encodeHeader(html_entity_decode($params['messageSubject'], ENT_QUOTES, 'UTF-8'), 'UTF-8');
 
                // generate a mime boundary
                $mimeBoundary   =rand(0, 9)."-"
@@ -48,7 +57,7 @@ class Emailer
                                .rand(10000, 99999);
 
                // generate a multipart/alternative message header
-               $messageHeader = $params['additionalMailHeader'] .
+               $messageHeader = defaults($params, 'additionalMailHeader', '') .
                                                "From: $fromName <{$params['fromEmail']}>\n" .
                                                "Reply-To: $fromName <{$params['replyTo']}>\n" .
                                                "MIME-Version: 1.0\n" .
@@ -72,23 +81,37 @@ class Emailer
                $multipartMessageBody .=
                        "--" . $mimeBoundary . "--\n";                                  // message ending
 
+               if (Config::get("system", "sendmail_params", true)) {
+                       $sendmail_params = '-f ' . $params['fromEmail'];
+               } else {
+                       $sendmail_params = null;
+               }
+
                // send the message
-               $hookdata = array(
+               $hookdata = [
                        'to' => $params['toEmail'],
                        'subject' => $messageSubject,
                        'body' => $multipartMessageBody,
-                       'headers' => $messageHeader
-               );
-               //echo "<pre>"; var_dump($hookdata); killme();
-               call_hooks("emailer_send", $hookdata);
+                       'headers' => $messageHeader,
+                       'parameters' => $sendmail_params,
+                       'sent' => false,
+               ];
+
+               Hook::callAll("emailer_send", $hookdata);
+
+               if ($hookdata['sent']) {
+                       return true;
+               }
+
                $res = mail(
-                       $hookdata['to'],                                                        // send to address
-                       $hookdata['subject'],                                           // subject
-                       $hookdata['body'],                                                      // message body
-                       $hookdata['headers']                                            // message headers
+                       $hookdata['to'],
+                       $hookdata['subject'],
+                       $hookdata['body'],
+                       $hookdata['headers'],
+                       $hookdata['parameters']
                );
-               logger("header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, LOGGER_DEBUG);
-               logger("return value " . (($res)?"true":"false"), LOGGER_DEBUG);
+               Logger::log("header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, Logger::DEBUG);
+               Logger::log("return value " . (($res)?"true":"false"), Logger::DEBUG);
                return $res;
        }
 }