Add SimpleEmail::setMessageID()
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / email / SimpleEmail.class.php
CommitLineData
efee4fbd 1<?php
a9229942 2
efee4fbd 3namespace wcf\system\email;
a9229942 4
efee4fbd 5use wcf\data\user\User;
efee4fbd 6use wcf\system\email\mime\HtmlTextMimePart;
ba800174
MS
7use wcf\system\email\mime\MimePartFacade;
8use wcf\system\email\mime\PlainTextMimePart;
efee4fbd
TD
9
10/**
11 * Simplifies creating and sending a new Email.
a9229942
TD
12 *
13 * @author Tim Duesterhus
14 * @copyright 2001-2019 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16 * @package WoltLabSuite\Core\System\Email
17 * @since 3.0
efee4fbd 18 */
a9229942
TD
19class SimpleEmail
20{
21 /**
22 * the underlying email object
23 * @var Email
24 */
25 private $email;
26
27 /**
28 * the text/plain version of the message body
29 * @var PlainTextMimePart
30 */
31 private $textPlain;
32
33 /**
34 * the text/html version of the message body
35 * @var HtmlTextMimePart
36 */
37 private $textHtml;
38
39 /**
40 * Creates the underlying Email object.
41 */
42 public function __construct()
43 {
44 $this->email = new Email();
45 }
46
47 /**
48 * Sets the email's 'Subject'.
49 *
50 * @param string $subject
51 * @see Email::setSubject()
52 */
53 public function setSubject($subject)
54 {
55 $this->email->setSubject($subject);
56 }
57
58 /**
59 * Sets the recipient of this email.
60 * This method clears any previous recipient of
61 * the email.
62 *
63 * @param User $user
64 */
65 public function setRecipient(User $user)
66 {
67 if (!$user->userID) {
68 throw new \InvalidArgumentException('The $user must not be a guest');
69 }
70
71 $recipients = $this->email->getRecipients();
72 foreach ($recipients as $recipient) {
73 $this->email->removeRecipient($recipient['mailbox']);
74 }
75
76 $this->email->addRecipient(new UserMailbox($user));
77 }
78
79 /**
80 * Sets the text/plain version of this message.
81 * An empty string clears this version (not recommended!).
82 *
83 * @param string $message
84 * @see PlainTextMimePart
85 */
86 public function setMessage($message)
87 {
88 $this->textPlain = $message ? new PlainTextMimePart($message) : null;
89
90 $this->fixBody();
91 }
92
93 /**
94 * Sets the text/html version of this message.
95 * An empty string clears this version.
96 *
97 * @param string $message
98 * @see HtmlTextMimePart
99 */
100 public function setHtmlMessage($message)
101 {
102 $this->textHtml = $message ? new HtmlTextMimePart($message) : null;
103
104 $this->fixBody();
105 }
106
107 /**
108 * Sets the proper email body based on $textHtml and $textPlain.
109 */
110 private function fixBody()
111 {
112 $parts = [];
113 if ($this->textHtml) {
114 $parts[] = $this->textHtml;
115 }
116 if ($this->textPlain) {
117 $parts[] = $this->textPlain;
118 }
119
120 $this->email->setBody(new MimePartFacade($parts));
121 }
122
896fe68b
TD
123 /**
124 * Sets the part left of the at sign (@) in the email's 'Message-Id'.
125 *
126 * @see Email::setMessageID()
127 */
128 public function setMessageID(?string $messageId): void
129 {
130 $this->email->setMessageID($messageId);
131 }
132
a9229942
TD
133 /**
134 * Queues this email for delivery.
135 *
136 * @see Email::send()
137 */
138 public function send()
139 {
140 $this->email->send();
141 }
142
143 /**
144 * Returns the underlying email object
145 *
146 * @return Email
147 */
148 public function getEmail()
149 {
150 return $this->email;
151 }
efee4fbd 152}