| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports. |
| 5 | * Copyright (C) 2022-2024 Aleksey Andreev (liuch) |
| 6 | * |
| 7 | * Available at: |
| 8 | * https://github.com/liuch/dmarc-srg |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the Free |
| 12 | * Software Foundation, either version 3 of the License. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 17 | * more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along with |
| 20 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | */ |
| 22 | |
| 23 | namespace Liuch\DmarcSrg\Mail; |
| 24 | |
| 25 | /** |
| 26 | * The class is designed to easily create multipart/alternative message bodies. |
| 27 | */ |
| 28 | class MailBody |
| 29 | { |
| 30 | private $boundary = null; |
| 31 | |
| 32 | /** |
| 33 | * Plain text message body as an array of strings |
| 34 | * |
| 35 | * @var null|array |
| 36 | */ |
| 37 | public $text = null; |
| 38 | |
| 39 | /** |
| 40 | * HTML text message body as an array of strings |
| 41 | * |
| 42 | * @var null|array |
| 43 | */ |
| 44 | public $html = null; |
| 45 | |
| 46 | /** |
| 47 | * Return Content-Type header value for the whole message |
| 48 | * |
| 49 | * @return string |
| 50 | */ |
| 51 | public function contentType(): string |
| 52 | { |
| 53 | if ($this->boundary()) { |
| 54 | $ctype = 'multipart/alternative; boundary="' . $this->boundary() . '"'; |
| 55 | } else { |
| 56 | if (!is_null($this->html)) { |
| 57 | $ctype = 'text/html'; |
| 58 | } else { |
| 59 | $ctype = 'text/plain'; |
| 60 | } |
| 61 | $ctype .= '; charset=utf-8'; |
| 62 | } |
| 63 | return $ctype; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns all the message parts with required headers as an array of strings |
| 68 | * |
| 69 | * @return array |
| 70 | */ |
| 71 | public function content(): array |
| 72 | { |
| 73 | $content = []; |
| 74 | if ($this->text) { |
| 75 | $this->addBodyPart('text', $this->text, $content); |
| 76 | } |
| 77 | if ($this->html) { |
| 78 | $this->addBodyPart('html', $this->html, $content); |
| 79 | } |
| 80 | return $content; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Generates a boundary string of the message. If the body has only one part of the content |
| 85 | * it returns null |
| 86 | * |
| 87 | * @return string|null |
| 88 | */ |
| 89 | private function boundary() |
| 90 | { |
| 91 | if (!$this->boundary) { |
| 92 | if ($this->text && $this->html) { |
| 93 | $this->boundary = '==========' . sha1(uniqid()) . '====='; |
| 94 | } |
| 95 | } |
| 96 | return $this->boundary; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Adds the specified part of the content to the array passed as the third parameter |
| 101 | * with the required headers. |
| 102 | * |
| 103 | * @param string $type Type of the content to add |
| 104 | * @param array $part Part of the content to add |
| 105 | * @param array $content Where the data with headers should be added |
| 106 | * |
| 107 | * @return void |
| 108 | */ |
| 109 | private function addBodyPart(string $type, array &$part, array &$content): void |
| 110 | { |
| 111 | if ($this->boundary()) { |
| 112 | $content[] = '--' . $this->boundary(); |
| 113 | switch ($type) { |
| 114 | case 'text': |
| 115 | default: |
| 116 | $ctype = 'text/plain'; |
| 117 | break; |
| 118 | case 'html': |
| 119 | $ctype = 'text/html'; |
| 120 | break; |
| 121 | } |
| 122 | $content[] = 'Content-Type: ' . $ctype . '; charset=utf-8'; |
| 123 | $content[] = 'Content-Transfer-Encoding: 7bit'; |
| 124 | $content[] = ''; |
| 125 | } |
| 126 | foreach ($part as $row) { |
| 127 | $content[] = $row; |
| 128 | } |
| 129 | unset($part); |
| 130 | } |
| 131 | } |