From efee4fbd17a365a229090490c646cbb68b3aa371 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Thu, 4 Aug 2016 17:26:28 +0200 Subject: [PATCH] Add SimpleEmail --- .../lib/system/email/SimpleEmail.class.php | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 wcfsetup/install/files/lib/system/email/SimpleEmail.class.php 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 index 0000000000..13fbf92fe7 --- /dev/null +++ b/wcfsetup/install/files/lib/system/email/SimpleEmail.class.php @@ -0,0 +1,122 @@ + + * @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; + } +} -- 2.20.1