| Total Complexity | 32 |
| Total Lines | 211 |
| Duplicated Lines | 0 % |
| Coverage | 46.88% |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 14 | class org_openpsa_mail_message |
||
| 15 | { |
||
| 16 | private $_to; |
||
| 17 | |||
| 18 | private $_encoding; |
||
| 19 | |||
| 20 | private $_headers; |
||
| 21 | |||
| 22 | private $_body; |
||
| 23 | private $_html_body; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * |
||
| 27 | * @var Swift_Message |
||
| 28 | */ |
||
| 29 | private $_message; |
||
| 30 | |||
| 31 | 4 | public function __construct($to, array $headers, $encoding) |
|
| 32 | { |
||
| 33 | 4 | $this->_to = $this->_encode_address_field($to); |
|
| 34 | 4 | $this->_headers = $headers; |
|
| 35 | 4 | $this->_encoding = $encoding; |
|
| 36 | |||
| 37 | 4 | $this->_message = new Swift_Message(''); |
|
| 38 | 4 | } |
|
| 39 | |||
| 40 | 4 | public function get_recipients() |
|
| 43 | } |
||
| 44 | |||
| 45 | public function get_message() : Swift_Message |
||
| 46 | { |
||
| 47 | // set headers |
||
| 48 | $headers_setter_map = [ |
||
| 49 | "content-type" => "setContentType", |
||
| 50 | "content-description" => "setDescription", |
||
| 51 | "from" => "setFrom", |
||
| 52 | "to" => "setTo", |
||
| 53 | "cc" => "setCc", |
||
| 54 | "bcc" => "setBcc", |
||
| 55 | "reply-to" => "setReplyTo", |
||
| 56 | "subject" => "setSubject", |
||
| 57 | "date" => "setDate", |
||
| 58 | "return-path" => "setReturnPath" |
||
| 59 | ]; |
||
| 60 | |||
| 61 | // map headers we got to swift setter methods |
||
| 62 | $msg_headers = $this->_message->getHeaders(); |
||
| 63 | $headers = $this->get_headers(); |
||
| 64 | foreach ($headers as $name => $value) { |
||
| 65 | if (array_key_exists(strtolower($name), $headers_setter_map)) { |
||
| 66 | $setter = $headers_setter_map[strtolower($name)]; |
||
| 67 | $this->_message->$setter($value); |
||
| 68 | } elseif ($msg_headers->has($name)) { |
||
| 69 | // header already exists => just set a new value |
||
| 70 | $msg_headers->get($name)->setValue($value); |
||
|
|
|||
| 71 | } else { |
||
| 72 | $msg_headers->addTextHeader($name, $value); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | // somehow we need to set the body after the headers... |
||
| 77 | if (!empty($this->_html_body)) { |
||
| 78 | $this->_message->setBody($this->_html_body, 'text/html'); |
||
| 79 | $this->_message->addPart($this->_body, 'text/plain'); |
||
| 80 | } else { |
||
| 81 | $this->_message->setBody($this->_body, 'text/plain'); |
||
| 82 | } |
||
| 83 | |||
| 84 | return $this->_message; |
||
| 85 | } |
||
| 86 | |||
| 87 | public function set_header_field($name, $value) |
||
| 88 | { |
||
| 89 | $this->_headers[$name] = $value; |
||
| 90 | } |
||
| 91 | |||
| 92 | 4 | public function get_headers() : array |
|
| 93 | { |
||
| 94 | 4 | if (empty($this->_headers['Content-Type'])) { |
|
| 95 | 4 | $this->_headers['Content-Type'] = "text/plain; charset={$this->_encoding}"; |
|
| 96 | } |
||
| 97 | |||
| 98 | 4 | reset($this->_headers); |
|
| 99 | 4 | foreach ($this->_headers as $header => $value) { |
|
| 100 | 4 | if (is_string($value)) { |
|
| 101 | 4 | $this->_headers[$header] = trim($value); |
|
| 102 | } |
||
| 103 | 4 | if ( strtolower($header) == 'from' |
|
| 104 | 4 | || strtolower($header) == 'reply-to' |
|
| 105 | 4 | || strtolower($header) == 'to') { |
|
| 106 | 4 | $this->_headers[$header] = $this->_encode_address_field($value); |
|
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | 4 | return $this->_headers; |
|
| 111 | } |
||
| 112 | |||
| 113 | 4 | public function get_body() |
|
| 116 | } |
||
| 117 | |||
| 118 | 3 | public function set_body($body) |
|
| 119 | { |
||
| 122 | 3 | } |
|
| 123 | |||
| 124 | /** |
||
| 125 | * |
||
| 126 | * @param string $body the html body |
||
| 127 | * @param string $altBody the alternative (text) body |
||
| 128 | * @param array $attachments |
||
| 129 | * @param boolean $do_image_embedding |
||
| 130 | */ |
||
| 131 | 1 | public function set_html_body($body, $altBody, $attachments, $do_image_embedding) |
|
| 143 | 1 | } |
|
| 144 | |||
| 145 | private function _embed_images() |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | 1 | private function _process_attachments($attachments) |
|
| 181 | { |
||
| 182 | 1 | foreach ($attachments as $att) { |
|
| 183 | if (empty($att['mimetype'])) { |
||
| 184 | $att['mimetype'] = "application/octet-stream"; |
||
| 185 | } |
||
| 186 | |||
| 187 | $swift_att = false; |
||
| 188 | // we got a file path |
||
| 189 | if (!empty($att['file'])) { |
||
| 190 | $swift_att = Swift_Attachment::fromPath($att['file'], $att['mimetype']); |
||
| 191 | } |
||
| 192 | // we got the contents (bytes) |
||
| 193 | elseif (!empty($att['content'])) { |
||
| 194 | $swift_att = new Swift_Attachment($att['content'], $att['name'], $att['mimetype']); |
||
| 195 | } |
||
| 196 | |||
| 197 | if ($swift_att) { |
||
| 198 | $this->_message->attach($swift_att); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | 1 | } |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Helper function that provides backwards compatibility |
||
| 205 | * to addresses specified in a "Name <[email protected]>" format |
||
| 206 | * |
||
| 207 | * @param string $value The value to encode |
||
| 208 | * @return mixed the encoded value |
||
| 209 | */ |
||
| 210 | 4 | private function _encode_address_field($value) |
|
| 225 | } |
||
| 226 | } |
||
| 227 |