Add SimpleEmail
authorTim Düsterhus <duesterhus@woltlab.com>
Thu, 4 Aug 2016 15:26:28 +0000 (17:26 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Thu, 4 Aug 2016 15:26:28 +0000 (17:26 +0200)
wcfsetup/install/files/lib/system/email/SimpleEmail.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/lib/system/email/SimpleEmail.class.php b/wcfsetup/install/files/lib/system/email/SimpleEmail.class.php
new file mode 100644 (file)
index 0000000..13fbf92
--- /dev/null
@@ -0,0 +1,122 @@
+<?php
+namespace wcf\system\email;
+use wcf\data\user\User;
+use wcf\system\email\mime\PlainTextMimePart;
+use wcf\system\email\mime\MimePartFacade;
+use wcf\system\email\mime\HtmlTextMimePart;
+
+/**
+ * Simplifies creating and sending a new Email.
+ * 
+ * @author     Tim Duesterhus
+ * @copyright  2001-2016 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Email
+ * @since      3.0
+ */
+class SimpleEmail {
+       /**
+        * the underlying email object
+        * @var Email
+        */
+       private $email = null;
+       
+       /**
+        * the text/plain version of the message body
+        * @var \wcf\system\email\mime\PlainTextMimePart
+        */
+       private $textPlain = null;
+       
+       /**
+        * the text/html version of the message body
+        * @var \wcf\system\email\mime\HtmlTextMimePart
+        */
+       private $textHtml = null;
+       
+       /**
+        * Creates the underlying Email object.
+        */
+       public function __construct() {
+               $this->email = new Email();
+       }
+       
+       /**
+        * @see Email::setSubject()
+        */
+       public function setSubject($subject) {
+               $this->email->setSubject($subject);
+       }
+       
+       /**
+        * Sets the recipient of this email.
+        * This method clears any previous recipient of
+        * the email.
+        * 
+        * @param       User            $user
+        */
+       public function setRecipient(User $user) {
+               if (!$user->userID) throw new \InvalidArgumentException('The $user must not be a guest');
+               
+               $recipients = $this->email->getRecipients();
+               foreach ($recipients as $recipient) {
+                       $this->email->removeRecipient($recipient['mailbox']);
+               }
+               
+               $this->email->addRecipient(new UserMailbox($user));
+       }
+       
+       /**
+        * Sets the text/plain version of this message.
+        * An empty string clears this version (not recommended!).
+        * 
+        * @param       string  $message
+        * @see         PlainTextMimePart
+        */
+       public function setMessage($message) {
+               $this->textPlain = $message ? new PlainTextMimePart($message) : null;
+               
+               $this->fixBody();
+       }
+       
+       /**
+        * Sets the text/html version of this message.
+        * An empty string clears this version.
+        * 
+        * @param       string  $message
+        * @see         HtmlTextMimePart
+        */
+       public function setHtmlMessage($message) {
+               $this->textHtml = $message ? new HtmlTextMimePart($message) : null;
+               
+               $this->fixBody();
+       }
+       
+       /**
+        * Sets the proper email body based on $textHtml and $textPlain.
+        */
+       private function fixBody() {
+               $parts = [];
+               if ($this->textHtml) $parts[] = $this->textHtml;
+               if ($this->textPlain) $parts[] = $this->textPlain;
+               
+               $this->email->setBody(new MimePartFacade($parts));
+       }
+       
+       /**
+        * Queues this email for delivery.
+        * 
+        * @see Email::send()
+        */
+       public function send() {
+               $this->email->send();
+       }
+       
+       /**
+        * Returns the underlying email object
+        * 
+        * @return      Email
+        */
+       public function getEmail() {
+               return $this->email;
+       }
+}