1<?php 2 3namespace splitbrain\dokuwiki\plugin\smtp; 4 5/** 6 * Class Message 7 * 8 * Overrides the Message class with what we need to reuse the SMTP mailer without using 9 * their message composer 10 * 11 * @package splitbrain\dokuwiki\plugin\smtp 12 */ 13class Message extends \Tx\Mailer\Message 14{ 15 protected $from; 16 protected $rcpt; 17 protected $body; 18 19 /** 20 * @param string $from Sender Address 21 * @param string $rcpt all recipients (TO, CC, BCC) 22 * @param string $body the full message body including headers 23 */ 24 public function __construct($from, $rcpt, $body) 25 { 26 $this->from = $from; 27 $this->rcpt = $rcpt; 28 $this->body = $body; 29 } 30 31 /** 32 * Return the mail only part of the from address 33 * 34 * @return string 35 */ 36 public function getFromEmail() 37 { 38 if (preg_match('#(.*?)<(.*?)>#', $this->from, $matches)) { 39 return $matches[2]; 40 } 41 42 return $this->from; 43 } 44 45 /** 46 * Get a list of all recipients (mail only part) 47 * 48 * The Mailer expects recipients as an array keyed by the email address 49 * (the value being the display name), so we return them in that format. 50 * 51 * @return array 52 */ 53 public function getTo() 54 { 55 $rcpt = []; 56 57 // We need the mail only part of all recipients 58 $addresses = explode(',', $this->rcpt); 59 foreach ($addresses as $addr) { 60 // parse address 61 if (preg_match('#(.*?)<(.*?)>#', $addr, $matches)) { 62 $mail = trim($matches[2]); 63 } else { 64 $mail = trim($addr); 65 } 66 if ($mail === '') continue; 67 $rcpt[$mail] = $mail; 68 } 69 70 return $rcpt; 71 } 72 73 /** 74 * Return the whole message body ready to be send by DATA 75 * 76 * Includes end of data signature and strips the BCC header 77 * 78 * @return string 79 */ 80 public function toString() 81 { 82 // we need to remove the BCC header here 83 $lines = preg_split('/\r?\n/', $this->body); 84 $count = count($lines); 85 for ($i = 0; $i < $count; $i++) { 86 if (trim($lines[$i]) === '') break; // end of headers, we're done 87 if (str_starts_with($lines[$i], 'Bcc:')) { 88 unset($lines[$i]); // we found the Bcc: header and remove it 89 while ($i + 1 < $count && str_starts_with($lines[$i + 1], ' ')) { 90 unset($lines[++$i]); // indented lines are header continuation 91 } 92 break; // header removed, we're done 93 } 94 } 95 $body = implode($this->CRLF, $lines); 96 97 return $body . $this->CRLF . $this->CRLF . "." . $this->CRLF; 98 } 99} 100