xref: /plugin/smtp/classes/Message.php (revision 25673c6215d63918d8dce22c43e8f1afb732abfc)
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;
16*25673c62SAndreas Gohr    protected $rcpt;
170b7ac7c9SAndreas Gohr    protected $body;
180b7ac7c9SAndreas Gohr
190b7ac7c9SAndreas Gohr    /**
20*25673c62SAndreas Gohr     * @param string $from Sender Address
21*25673c62SAndreas Gohr     * @param string $rcpt all recipients (TO, CC, BCC)
22*25673c62SAndreas Gohr     * @param string $body the full message body including headers
230b7ac7c9SAndreas Gohr     */
24*25673c62SAndreas Gohr    public function __construct($from, $rcpt, $body) {
250b7ac7c9SAndreas Gohr        $this->from = $from;
26*25673c62SAndreas Gohr        $this->rcpt = $rcpt;
270b7ac7c9SAndreas Gohr        $this->body = $body;
280b7ac7c9SAndreas Gohr    }
290b7ac7c9SAndreas Gohr
300b7ac7c9SAndreas Gohr    /**
31*25673c62SAndreas Gohr     * Return the mail only part of the from address
32*25673c62SAndreas Gohr     *
330b7ac7c9SAndreas Gohr     * @return string
340b7ac7c9SAndreas Gohr     */
350b7ac7c9SAndreas Gohr    public function getFromEmail() {
36*25673c62SAndreas Gohr        if(preg_match('#(.*?)<(.*?)>#', $this->from, $matches)) {
37*25673c62SAndreas Gohr            return $matches[2];
38*25673c62SAndreas Gohr        }
39*25673c62SAndreas Gohr
400b7ac7c9SAndreas Gohr        return $this->from;
410b7ac7c9SAndreas Gohr    }
420b7ac7c9SAndreas Gohr
430b7ac7c9SAndreas Gohr    /**
44*25673c62SAndreas Gohr     * Get a list of all recipients (mail only part)
45*25673c62SAndreas Gohr     *
460b7ac7c9SAndreas Gohr     * @return array
470b7ac7c9SAndreas Gohr     */
480b7ac7c9SAndreas Gohr    public function getTo() {
49*25673c62SAndreas Gohr        $rcpt = array();
50*25673c62SAndreas Gohr
51*25673c62SAndreas Gohr        // We need the mail only part of all recipients
52*25673c62SAndreas Gohr        $addresses = explode(',', $this->rcpt);
53*25673c62SAndreas Gohr        foreach($addresses as $addr) {
54*25673c62SAndreas Gohr            // parse address
55*25673c62SAndreas Gohr            if(preg_match('#(.*?)<(.*?)>#', $addr, $matches)) {
56*25673c62SAndreas Gohr                $rcpt[] = $matches[2];
57*25673c62SAndreas Gohr            } else {
58*25673c62SAndreas Gohr                $rcpt[] = $addr;
59*25673c62SAndreas Gohr            }
60*25673c62SAndreas Gohr        }
61*25673c62SAndreas Gohr
620b7ac7c9SAndreas Gohr        $rcpt = array_filter($rcpt);
630b7ac7c9SAndreas Gohr        $rcpt = array_unique($rcpt);
640b7ac7c9SAndreas Gohr        return $rcpt;
650b7ac7c9SAndreas Gohr    }
660b7ac7c9SAndreas Gohr
670b7ac7c9SAndreas Gohr    /**
68*25673c62SAndreas Gohr     * Return the whole message body ready to be send by DATA
69*25673c62SAndreas Gohr     *
70*25673c62SAndreas Gohr     * Includes end of data signature and strips the BCC header
71*25673c62SAndreas Gohr     *
720b7ac7c9SAndreas Gohr     * @return string
730b7ac7c9SAndreas Gohr     */
740b7ac7c9SAndreas Gohr    public function toString() {
75*25673c62SAndreas Gohr        // we need to remove the BCC header here
76*25673c62SAndreas Gohr        $lines = explode("\n", $this->body);
77*25673c62SAndreas Gohr        $count = count($lines);
78*25673c62SAndreas Gohr        for($i=0; $i<$count; $i++) {
79*25673c62SAndreas Gohr            if(trim($lines[$i]) === '') break; // end of headers, we're done
80*25673c62SAndreas Gohr            if(substr($lines[$i],0, 4) == 'Bcc:') {
81*25673c62SAndreas Gohr                unset($lines[$i]); // we found the Bcc: header and remove it
82*25673c62SAndreas Gohr                while(substr($lines[++$i],0, 1) === ' ') {
83*25673c62SAndreas Gohr                    unset($lines[$i]); // indented lines are header continuiation
84*25673c62SAndreas Gohr                }
85*25673c62SAndreas Gohr                break; // header removed, we're done
86*25673c62SAndreas Gohr            }
87*25673c62SAndreas Gohr        }
88*25673c62SAndreas Gohr        $body = join("\n", $lines);
890b7ac7c9SAndreas Gohr
90*25673c62SAndreas Gohr        return $body . $this->CRLF . $this->CRLF . "." . $this->CRLF;
910b7ac7c9SAndreas Gohr    }
920b7ac7c9SAndreas Gohr
930b7ac7c9SAndreas Gohr}
94