xref: /plugin/smtp/classes/Message.php (revision 52d89ff35464923c06b17b4743d35ffe01173e0e)
10b7ac7c9SAndreas Gohr<?php
20b7ac7c9SAndreas Gohr
30b7ac7c9SAndreas Gohrnamespace splitbrain\dokuwiki\plugin\smtp;
40b7ac7c9SAndreas Gohr
50b7ac7c9SAndreas Gohr/**
60b7ac7c9SAndreas Gohr * Class Message
70b7ac7c9SAndreas Gohr *
80b7ac7c9SAndreas Gohr * Overrides the Message class with what we need to reuse the SMTP mailer without using
90b7ac7c9SAndreas Gohr * their message composer
100b7ac7c9SAndreas Gohr *
110b7ac7c9SAndreas Gohr * @package splitbrain\dokuwiki\plugin\smtp
120b7ac7c9SAndreas Gohr */
130b7ac7c9SAndreas Gohrclass Message extends \Tx\Mailer\Message {
140b7ac7c9SAndreas Gohr
150b7ac7c9SAndreas Gohr    protected $from;
1625673c62SAndreas Gohr    protected $rcpt;
170b7ac7c9SAndreas Gohr    protected $body;
180b7ac7c9SAndreas Gohr
190b7ac7c9SAndreas Gohr    /**
2025673c62SAndreas Gohr     * @param string $from Sender Address
2125673c62SAndreas Gohr     * @param string $rcpt all recipients (TO, CC, BCC)
2225673c62SAndreas Gohr     * @param string $body the full message body including headers
230b7ac7c9SAndreas Gohr     */
2425673c62SAndreas Gohr    public function __construct($from, $rcpt, $body) {
250b7ac7c9SAndreas Gohr        $this->from = $from;
2625673c62SAndreas Gohr        $this->rcpt = $rcpt;
270b7ac7c9SAndreas Gohr        $this->body = $body;
280b7ac7c9SAndreas Gohr    }
290b7ac7c9SAndreas Gohr
300b7ac7c9SAndreas Gohr    /**
3125673c62SAndreas Gohr     * Return the mail only part of the from address
3225673c62SAndreas Gohr     *
330b7ac7c9SAndreas Gohr     * @return string
340b7ac7c9SAndreas Gohr     */
350b7ac7c9SAndreas Gohr    public function getFromEmail() {
3625673c62SAndreas Gohr        if(preg_match('#(.*?)<(.*?)>#', $this->from, $matches)) {
3725673c62SAndreas Gohr            return $matches[2];
3825673c62SAndreas Gohr        }
3925673c62SAndreas Gohr
400b7ac7c9SAndreas Gohr        return $this->from;
410b7ac7c9SAndreas Gohr    }
420b7ac7c9SAndreas Gohr
430b7ac7c9SAndreas Gohr    /**
4425673c62SAndreas Gohr     * Get a list of all recipients (mail only part)
4525673c62SAndreas Gohr     *
460b7ac7c9SAndreas Gohr     * @return array
470b7ac7c9SAndreas Gohr     */
480b7ac7c9SAndreas Gohr    public function getTo() {
4925673c62SAndreas Gohr        $rcpt = array();
5025673c62SAndreas Gohr
5125673c62SAndreas Gohr        // We need the mail only part of all recipients
5225673c62SAndreas Gohr        $addresses = explode(',', $this->rcpt);
5325673c62SAndreas Gohr        foreach($addresses as $addr) {
5425673c62SAndreas Gohr            // parse address
5525673c62SAndreas Gohr            if(preg_match('#(.*?)<(.*?)>#', $addr, $matches)) {
56*52d89ff3SSébastien Mennetrier                $rcpt[] = trim($matches[2]);
5725673c62SAndreas Gohr            } else {
58*52d89ff3SSébastien Mennetrier                $rcpt[] = trim($addr);
5925673c62SAndreas Gohr            }
6025673c62SAndreas Gohr        }
6125673c62SAndreas Gohr
620b7ac7c9SAndreas Gohr        $rcpt = array_filter($rcpt);
630b7ac7c9SAndreas Gohr        $rcpt = array_unique($rcpt);
640b7ac7c9SAndreas Gohr        return $rcpt;
650b7ac7c9SAndreas Gohr    }
660b7ac7c9SAndreas Gohr
670b7ac7c9SAndreas Gohr    /**
6825673c62SAndreas Gohr     * Return the whole message body ready to be send by DATA
6925673c62SAndreas Gohr     *
7025673c62SAndreas Gohr     * Includes end of data signature and strips the BCC header
7125673c62SAndreas Gohr     *
720b7ac7c9SAndreas Gohr     * @return string
730b7ac7c9SAndreas Gohr     */
740b7ac7c9SAndreas Gohr    public function toString() {
7525673c62SAndreas Gohr        // we need to remove the BCC header here
7625673c62SAndreas Gohr        $lines = explode("\n", $this->body);
7725673c62SAndreas Gohr        $count = count($lines);
7825673c62SAndreas Gohr        for($i=0; $i<$count; $i++) {
7925673c62SAndreas Gohr            if(trim($lines[$i]) === '') break; // end of headers, we're done
8025673c62SAndreas Gohr            if(substr($lines[$i],0, 4) == 'Bcc:') {
8125673c62SAndreas Gohr                unset($lines[$i]); // we found the Bcc: header and remove it
8225673c62SAndreas Gohr                while(substr($lines[++$i],0, 1) === ' ') {
8325673c62SAndreas Gohr                    unset($lines[$i]); // indented lines are header continuiation
8425673c62SAndreas Gohr                }
8525673c62SAndreas Gohr                break; // header removed, we're done
8625673c62SAndreas Gohr            }
8725673c62SAndreas Gohr        }
8825673c62SAndreas Gohr        $body = join("\n", $lines);
890b7ac7c9SAndreas Gohr
9025673c62SAndreas Gohr        return $body . $this->CRLF . $this->CRLF . "." . $this->CRLF;
910b7ac7c9SAndreas Gohr    }
920b7ac7c9SAndreas Gohr
930b7ac7c9SAndreas Gohr}
94