. */ namespace Liuch\DmarcSrg\Mail; /** * The class is designed to easily create multipart/alternative message bodies. */ class MailBody { private $text = null; private $html = null; private $boundary = null; /** * Sets text content as a part of the message body * * @param array $text Text part of the message as an array of strings * * @return void */ public function setText(array &$text): void { $this->text = $text; } /** * Sets html content as a part of the message body * * @param array $html Html part of the message as and array of strings * * @return void */ public function setHtml(array $html): void { $this->html = $html; } /** * Return Content-Type header value for the whole message * * @return string */ public function contentType(): string { if ($this->boundary()) { $ctype = 'multipart/alternative; boundary="' . $this->boundary() . '"'; } else { if (!is_null($this->html)) { $ctype = 'text/html'; } else { $ctype = 'text/plain'; } $ctype .= '; charset=utf-8'; } return $ctype; } /** * Returns all the message parts with required headers as an array of strings * * @return array */ public function content(): array { $content = []; if ($this->text) { $this->addBodyPart('text', $this->text, $content); } if ($this->html) { $this->addBodyPart('html', $this->html, $content); } return $content; } /** * Generates a boundary string of the message. If the body has only one part of the content * it returns null * * @return string|null */ private function boundary() { if (!$this->boundary) { if ($this->text && $this->html) { $this->boundary = '==========' . sha1(uniqid()) . '====='; } } return $this->boundary; } /** * Adds the specified part of the content to the array passed as the third parameter * with the required headers. * * @param string $type Type of the content to add * @param array $part Part of the content to add * @param array $content Where the data with headers should be added * * @return void */ private function addBodyPart(string $type, array &$part, array &$content): void { if ($this->boundary()) { $content[] = '--' . $this->boundary(); switch ($type) { case 'text': $ctype = 'text/plain'; break; case 'html': $ctype = 'text/html'; break; } $content[] = 'Content-Type: ' . $ctype . '; charset=utf-8'; $content[] = 'Content-Transfer-Encoding: 7bit'; $content[] = ''; } foreach ($part as $row) { $content[] = $row; } unset($part); } }