From: Tim Düsterhus Date: Thu, 4 Aug 2016 15:26:28 +0000 (+0200) Subject: Add SimpleEmail X-Git-Tag: 3.0.0_Beta_1~813 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=efee4fbd17a365a229090490c646cbb68b3aa371;p=GitHub%2FWoltLab%2FWCF.git Add SimpleEmail --- 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; + } +}