Fix secure Mail addon
authornupplaPhil <admin@philipp.info>
Fri, 31 Jan 2020 18:32:17 +0000 (19:32 +0100)
committernupplaPhil <admin@philipp.info>
Fri, 31 Jan 2020 18:32:17 +0000 (19:32 +0100)
src/Object/EMail/IEmail.php
src/Object/Email.php
src/Util/Emailer.php

index 6a159c0..ec8ae88 100644 (file)
@@ -3,13 +3,14 @@
 namespace Friendica\Object\EMail;
 
 use Friendica\Util\Emailer;
+use JsonSerializable;
 
 /**
  * Interface for a single mail, which can be send through Emailer::send()
  *
  * @see Emailer::send()
  */
-interface IEmail
+interface IEmail extends JsonSerializable
 {
        /**
         * Gets the senders name for this email
@@ -68,4 +69,27 @@ interface IEmail
         * @return string
         */
        function getAdditionalMailHeader();
+
+       /**
+        * Returns the current email with a new recipient
+        *
+        * @param string $address The email of the recipient
+        * @param int    $uid   The (optional) UID of the recipient for further infos
+        *
+        * @return static
+        */
+       function withRecipient(string $address, int $uid);
+
+       /**
+        * @param string $plaintext a new plaintext message for this email
+        * @param string $html      a new html message for this email (optional)
+        *
+        * @return static
+        */
+       function withMessage(string $plaintext, string $html = null);
+
+       /**
+        * @return string
+        */
+       function __toString();
 }
index 4f9c4e7..32d1382 100644 (file)
@@ -23,7 +23,7 @@ class Email implements IEmail
 
        /** @var string */
        private $subject;
-       /** @var string */
+       /** @var string|null */
        private $msgHtml;
        /** @var string */
        private $msgText;
@@ -117,40 +117,52 @@ class Email implements IEmail
        }
 
        /**
-        * Returns the current email with a new recipient
-        *
-        * @param string $email The email of the recipient
-        * @param int    $uid   The (optional) UID of the recipient for further infos
-        *
-        * @return static
+        * {@inheritDoc}
         */
-       public function withRecipient(string $email, int $uid = null)
+       public function withRecipient(string $address, int $uid = null)
        {
                $newEmail            = clone $this;
-               $newEmail->toAddress = $email;
+               $newEmail->toAddress = $address;
                $newEmail->toUid     = $uid;
 
                return $newEmail;
        }
 
        /**
-        * Creates a new Email instance based on a given prototype
-        *
-        * @param static $prototype The base prototype
-        * @param array  $data      The delta-data (key must be an existing property)
+        * {@inheritDoc}
+        */
+       public function withMessage(string $plaintext, string $html = null)
+       {
+               $newMail          = clone $this;
+               $newMail->msgText = $plaintext;
+               $newMail->msgHtml = $html;
+
+               return $newMail;
+       }
+
+       /**
+        * Returns the properties of the email as an array
         *
-        * @return static The new email instance
+        * @return array
         */
-       public static function createFromPrototype(Email $prototype, array $data = [])
+       private function toArray()
        {
-               $newMail = clone $prototype;
+               return get_object_vars($this);
+       }
 
-               foreach ($data as $key => $value) {
-                       if (property_exists($newMail, $key)) {
-                               $newMail->{$key} = $value;
-                       }
-               }
+       /**
+        * @inheritDoc
+        */
+       public function __toString()
+       {
+               return json_encode($this->toArray());
+       }
 
-               return $newMail;
+       /**
+        * @inheritDoc
+        */
+       public function jsonSerialize()
+       {
+               return $this->toArray();
        }
 }
index 19755be..cb59d48 100644 (file)
@@ -45,8 +45,12 @@ class Emailer
         */
        public function send(IEmail $email)
        {
+               $this->logger->warning('start', ['email' => $email]);
+
                Hook::callAll('emailer_send_prepare', $email);
 
+               $this->logger->warning('end', ['email' => $email]);
+
                if (empty($email)) {
                        return true;
                }