xref: /dokuwiki/inc/Mailer.class.php (revision 8fa268b3d54a68c7f30864fbf666776420c35634)
1bb01c27cSAndreas Gohr<?php
2bb01c27cSAndreas Gohr/**
31d045709SAndreas Gohr * A class to build and send multi part mails (with HTML content and embedded
41d045709SAndreas Gohr * attachments). All mails are assumed to be in UTF-8 encoding.
51d045709SAndreas Gohr *
61d045709SAndreas Gohr * Attachments are handled in memory so this shouldn't be used to send huge
71d045709SAndreas Gohr * files, but then again mail shouldn't be used to send huge files either.
81d045709SAndreas Gohr *
9bb01c27cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
10bb01c27cSAndreas Gohr */
11bb01c27cSAndreas Gohr
12e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event;
13e1d9dcc8SAndreas Gohr
14bb01c27cSAndreas Gohr// end of line for mail lines - RFC822 says CRLF but postfix (and other MTAs?)
15bb01c27cSAndreas Gohr// think different
16bb01c27cSAndreas Gohrif(!defined('MAILHEADER_EOL')) define('MAILHEADER_EOL', "\n");
17bb01c27cSAndreas Gohr#define('MAILHEADER_ASCIIONLY',1);
18bb01c27cSAndreas Gohr
19a89c75afSAndreas Gohr/**
20a89c75afSAndreas Gohr * Mail Handling
21a89c75afSAndreas Gohr */
22bb01c27cSAndreas Gohrclass Mailer {
23bb01c27cSAndreas Gohr
24f41c79d7SAndreas Gohr    protected $headers   = array();
25f41c79d7SAndreas Gohr    protected $attach    = array();
26f41c79d7SAndreas Gohr    protected $html      = '';
27f41c79d7SAndreas Gohr    protected $text      = '';
28bb01c27cSAndreas Gohr
29f41c79d7SAndreas Gohr    protected $boundary  = '';
30f41c79d7SAndreas Gohr    protected $partid    = '';
31f41c79d7SAndreas Gohr    protected $sendparam = null;
32bb01c27cSAndreas Gohr
33f41c79d7SAndreas Gohr    protected $allowhtml = true;
341d045709SAndreas Gohr
359ea45836SChristopher Smith    protected $replacements = array('text'=> array(), 'html' => array());
369ea45836SChristopher Smith
371d045709SAndreas Gohr    /**
381d045709SAndreas Gohr     * Constructor
391d045709SAndreas Gohr     *
409ea45836SChristopher Smith     * Initializes the boundary strings, part counters and token replacements
411d045709SAndreas Gohr     */
421d045709SAndreas Gohr    public function __construct() {
439f3eca0bSAndreas Gohr        global $conf;
44585bf44eSChristopher Smith        /* @var Input $INPUT */
45585bf44eSChristopher Smith        global $INPUT;
469f3eca0bSAndreas Gohr
479f3eca0bSAndreas Gohr        $server = parse_url(DOKU_URL, PHP_URL_HOST);
48749c0023SAndreas Gohr        if(strpos($server,'.') === false) $server .= '.localhost';
491d045709SAndreas Gohr
50749c0023SAndreas Gohr        $this->partid   = substr(md5(uniqid(mt_rand(), true)),0, 8).'@'.$server;
51749c0023SAndreas Gohr        $this->boundary = '__________'.md5(uniqid(mt_rand(), true));
529f3eca0bSAndreas Gohr
53749c0023SAndreas Gohr        $listid = implode('.', array_reverse(explode('/', DOKU_BASE))).$server;
549f3eca0bSAndreas Gohr        $listid = strtolower(trim($listid, '.'));
557c7659d2SPhilipp Specht        $messageid = uniqid(mt_rand(), true) . "@$server";
569f3eca0bSAndreas Gohr
572398a2b5SAndreas Gohr        $this->allowhtml = (bool)$conf['htmlmail'];
582398a2b5SAndreas Gohr
599f3eca0bSAndreas Gohr        // add some default headers for mailfiltering FS#2247
605f43dcf4SLukas Rademacher        if(!empty($conf['mailreturnpath'])) {
615f43dcf4SLukas Rademacher            $this->setHeader('Return-Path', $conf['mailreturnpath']);
625f43dcf4SLukas Rademacher        }
636a1f928fSAndreas Gohr        $this->setHeader('X-Mailer', 'DokuWiki');
64585bf44eSChristopher Smith        $this->setHeader('X-DokuWiki-User', $INPUT->server->str('REMOTE_USER'));
659f3eca0bSAndreas Gohr        $this->setHeader('X-DokuWiki-Title', $conf['title']);
669f3eca0bSAndreas Gohr        $this->setHeader('X-DokuWiki-Server', $server);
679f3eca0bSAndreas Gohr        $this->setHeader('X-Auto-Response-Suppress', 'OOF');
689f3eca0bSAndreas Gohr        $this->setHeader('List-Id', $conf['title'].' <'.$listid.'>');
69d6e04b60SAndreas Gohr        $this->setHeader('Date', date('r'), false);
707c7659d2SPhilipp Specht        $this->setHeader('Message-Id', "<$messageid>");
719ea45836SChristopher Smith
729ea45836SChristopher Smith        $this->prepareTokenReplacements();
73bb01c27cSAndreas Gohr    }
74bb01c27cSAndreas Gohr
75bb01c27cSAndreas Gohr    /**
76bb01c27cSAndreas Gohr     * Attach a file
77bb01c27cSAndreas Gohr     *
78a89c75afSAndreas Gohr     * @param string $path  Path to the file to attach
79a89c75afSAndreas Gohr     * @param string $mime  Mimetype of the attached file
80a89c75afSAndreas Gohr     * @param string $name The filename to use
81a89c75afSAndreas Gohr     * @param string $embed Unique key to reference this file from the HTML part
82bb01c27cSAndreas Gohr     */
83bb01c27cSAndreas Gohr    public function attachFile($path, $mime, $name = '', $embed = '') {
84bb01c27cSAndreas Gohr        if(!$name) {
858cbc5ee8SAndreas Gohr            $name = \dokuwiki\Utf8\PhpString::basename($path);
86bb01c27cSAndreas Gohr        }
87bb01c27cSAndreas Gohr
88bb01c27cSAndreas Gohr        $this->attach[] = array(
89bb01c27cSAndreas Gohr            'data'  => file_get_contents($path),
90bb01c27cSAndreas Gohr            'mime'  => $mime,
91bb01c27cSAndreas Gohr            'name'  => $name,
92bb01c27cSAndreas Gohr            'embed' => $embed
93bb01c27cSAndreas Gohr        );
94bb01c27cSAndreas Gohr    }
95bb01c27cSAndreas Gohr
96bb01c27cSAndreas Gohr    /**
97bb01c27cSAndreas Gohr     * Attach a file
98bb01c27cSAndreas Gohr     *
99a89c75afSAndreas Gohr     * @param string $data  The file contents to attach
100a89c75afSAndreas Gohr     * @param string $mime  Mimetype of the attached file
101a89c75afSAndreas Gohr     * @param string $name  The filename to use
102a89c75afSAndreas Gohr     * @param string $embed Unique key to reference this file from the HTML part
103bb01c27cSAndreas Gohr     */
104bb01c27cSAndreas Gohr    public function attachContent($data, $mime, $name = '', $embed = '') {
105bb01c27cSAndreas Gohr        if(!$name) {
106a89c75afSAndreas Gohr            list(, $ext) = explode('/', $mime);
107bb01c27cSAndreas Gohr            $name = count($this->attach).".$ext";
108bb01c27cSAndreas Gohr        }
109bb01c27cSAndreas Gohr
110bb01c27cSAndreas Gohr        $this->attach[] = array(
111bb01c27cSAndreas Gohr            'data'  => $data,
112bb01c27cSAndreas Gohr            'mime'  => $mime,
113bb01c27cSAndreas Gohr            'name'  => $name,
114bb01c27cSAndreas Gohr            'embed' => $embed
115bb01c27cSAndreas Gohr        );
116bb01c27cSAndreas Gohr    }
117bb01c27cSAndreas Gohr
118bb01c27cSAndreas Gohr    /**
119850dbf1fSAndreas Gohr     * Callback function to automatically embed images referenced in HTML templates
12042ea7f44SGerrit Uitslag     *
12142ea7f44SGerrit Uitslag     * @param array $matches
12242ea7f44SGerrit Uitslag     * @return string placeholder
123850dbf1fSAndreas Gohr     */
124749c0023SAndreas Gohr    protected function autoEmbedCallBack($matches) {
125850dbf1fSAndreas Gohr        static $embeds = 0;
126850dbf1fSAndreas Gohr        $embeds++;
127850dbf1fSAndreas Gohr
128850dbf1fSAndreas Gohr        // get file and mime type
129850dbf1fSAndreas Gohr        $media = cleanID($matches[1]);
130a89c75afSAndreas Gohr        list(, $mime) = mimetype($media);
131850dbf1fSAndreas Gohr        $file = mediaFN($media);
132850dbf1fSAndreas Gohr        if(!file_exists($file)) return $matches[0]; //bad reference, keep as is
133850dbf1fSAndreas Gohr
134850dbf1fSAndreas Gohr        // attach it and set placeholder
135850dbf1fSAndreas Gohr        $this->attachFile($file, $mime, '', 'autoembed'.$embeds);
136850dbf1fSAndreas Gohr        return '%%autoembed'.$embeds.'%%';
137850dbf1fSAndreas Gohr    }
138850dbf1fSAndreas Gohr
139850dbf1fSAndreas Gohr    /**
1401d045709SAndreas Gohr     * Add an arbitrary header to the mail
1411d045709SAndreas Gohr     *
142a36fc348SAndreas Gohr     * If an empy value is passed, the header is removed
143a36fc348SAndreas Gohr     *
1441d045709SAndreas Gohr     * @param string $header the header name (no trailing colon!)
14559bc3b48SGerrit Uitslag     * @param string|string[] $value  the value of the header
1461d045709SAndreas Gohr     * @param bool   $clean  remove all non-ASCII chars and line feeds?
1471d045709SAndreas Gohr     */
1481d045709SAndreas Gohr    public function setHeader($header, $value, $clean = true) {
1499f3eca0bSAndreas Gohr        $header = str_replace(' ', '-', ucwords(strtolower(str_replace('-', ' ', $header)))); // streamline casing
1501d045709SAndreas Gohr        if($clean) {
151578b2c23SAndreas Gohr            $header = preg_replace('/[^a-zA-Z0-9_ \-\.\+\@]+/', '', $header);
152578b2c23SAndreas Gohr            $value  = preg_replace('/[^a-zA-Z0-9_ \-\.\+\@<>]+/', '', $value);
1531d045709SAndreas Gohr        }
154a36fc348SAndreas Gohr
155a36fc348SAndreas Gohr        // empty value deletes
156b6c97c70SAndreas Gohr        if(is_array($value)){
157b6c97c70SAndreas Gohr            $value = array_map('trim', $value);
158b6c97c70SAndreas Gohr            $value = array_filter($value);
159b6c97c70SAndreas Gohr            if(!$value) $value = '';
160b6c97c70SAndreas Gohr        }else{
161a36fc348SAndreas Gohr            $value = trim($value);
162b6c97c70SAndreas Gohr        }
163a36fc348SAndreas Gohr        if($value === '') {
164a36fc348SAndreas Gohr            if(isset($this->headers[$header])) unset($this->headers[$header]);
165a36fc348SAndreas Gohr        } else {
1661d045709SAndreas Gohr            $this->headers[$header] = $value;
1671d045709SAndreas Gohr        }
168a36fc348SAndreas Gohr    }
1691d045709SAndreas Gohr
1701d045709SAndreas Gohr    /**
1711d045709SAndreas Gohr     * Set additional parameters to be passed to sendmail
1721d045709SAndreas Gohr     *
1731d045709SAndreas Gohr     * Whatever is set here is directly passed to PHP's mail() command as last
1741d045709SAndreas Gohr     * parameter. Depending on the PHP setup this might break mailing alltogether
17542ea7f44SGerrit Uitslag     *
17642ea7f44SGerrit Uitslag     * @param string $param
1771d045709SAndreas Gohr     */
1781d045709SAndreas Gohr    public function setParameters($param) {
1791d045709SAndreas Gohr        $this->sendparam = $param;
1801d045709SAndreas Gohr    }
1811d045709SAndreas Gohr
1821d045709SAndreas Gohr    /**
183abbf0890SAndreas Gohr     * Set the text and HTML body and apply replacements
184abbf0890SAndreas Gohr     *
185abbf0890SAndreas Gohr     * This function applies a whole bunch of default replacements in addition
18604dcb5b2SChristopher Smith     * to the ones specified as parameters
187abbf0890SAndreas Gohr     *
188abbf0890SAndreas Gohr     * If you pass the HTML part or HTML replacements yourself you have to make
189abbf0890SAndreas Gohr     * sure you encode all HTML special chars correctly
190abbf0890SAndreas Gohr     *
191abbf0890SAndreas Gohr     * @param string $text     plain text body
192abbf0890SAndreas Gohr     * @param array  $textrep  replacements to apply on the text part
19364159a61SAndreas Gohr     * @param array  $htmlrep  replacements to apply on the HTML part, null to use $textrep (urls wrapped in <a> tags)
19459bc3b48SGerrit Uitslag     * @param string $html     the HTML body, leave null to create it from $text
195f08086ecSAndreas Gohr     * @param bool   $wrap     wrap the HTML in the default header/Footer
196abbf0890SAndreas Gohr     */
197f08086ecSAndreas Gohr    public function setBody($text, $textrep = null, $htmlrep = null, $html = null, $wrap = true) {
198585bf44eSChristopher Smith
19976efd6d0SAndreas Gohr        $htmlrep = (array)$htmlrep;
20076efd6d0SAndreas Gohr        $textrep = (array)$textrep;
201abbf0890SAndreas Gohr
202abbf0890SAndreas Gohr        // create HTML from text if not given
203749c0023SAndreas Gohr        if($html === null) {
204ba9c057bSAndreas Gohr            $html = $text;
205ba9c057bSAndreas Gohr            $html = hsc($html);
206ba2c2f17Sfurun            $html = preg_replace('/^----+$/m', '<hr >', $html);
207ba9c057bSAndreas Gohr            $html = nl2br($html);
208abbf0890SAndreas Gohr        }
209f08086ecSAndreas Gohr        if($wrap) {
210749c0023SAndreas Gohr            $wrapper = rawLocale('mailwrap', 'html');
2113819cafdSfurun            $html = preg_replace('/\n-- <br \/>.*$/s', '', $html); //strip signature
2123819cafdSfurun            $html = str_replace('@EMAILSIGNATURE@', '', $html); //strip @EMAILSIGNATURE@
213749c0023SAndreas Gohr            $html = str_replace('@HTMLBODY@', $html, $wrapper);
214f08086ecSAndreas Gohr        }
215f08086ecSAndreas Gohr
2163819cafdSfurun        if(strpos($text, '@EMAILSIGNATURE@') === false) {
2179ea45836SChristopher Smith            $text .= '@EMAILSIGNATURE@';
2183819cafdSfurun        }
219ba2c2f17Sfurun
22076efd6d0SAndreas Gohr        // copy over all replacements missing for HTML (autolink URLs)
22176efd6d0SAndreas Gohr        foreach($textrep as $key => $value) {
22276efd6d0SAndreas Gohr            if(isset($htmlrep[$key])) continue;
2233e7e6067SKlap-in            if(media_isexternal($value)) {
22476efd6d0SAndreas Gohr                $htmlrep[$key] = '<a href="'.hsc($value).'">'.hsc($value).'</a>';
22576efd6d0SAndreas Gohr            } else {
22676efd6d0SAndreas Gohr                $htmlrep[$key] = hsc($value);
22776efd6d0SAndreas Gohr            }
228abbf0890SAndreas Gohr        }
229abbf0890SAndreas Gohr
230850dbf1fSAndreas Gohr        // embed media from templates
231a89c75afSAndreas Gohr        $html = preg_replace_callback(
232a89c75afSAndreas Gohr            '/@MEDIA\(([^\)]+)\)@/',
233749c0023SAndreas Gohr            array($this, 'autoEmbedCallBack'), $html
234a89c75afSAndreas Gohr        );
235850dbf1fSAndreas Gohr
2369ea45836SChristopher Smith        // add default token replacements
2379ea45836SChristopher Smith        $trep = array_merge($this->replacements['text'], (array)$textrep);
2389ea45836SChristopher Smith        $hrep = array_merge($this->replacements['html'], (array)$htmlrep);
239abbf0890SAndreas Gohr
240abbf0890SAndreas Gohr        // Apply replacements
241abbf0890SAndreas Gohr        foreach($trep as $key => $substitution) {
242abbf0890SAndreas Gohr            $text = str_replace('@'.strtoupper($key).'@', $substitution, $text);
243abbf0890SAndreas Gohr        }
244abbf0890SAndreas Gohr        foreach($hrep as $key => $substitution) {
245abbf0890SAndreas Gohr            $html = str_replace('@'.strtoupper($key).'@', $substitution, $html);
246abbf0890SAndreas Gohr        }
247abbf0890SAndreas Gohr
248abbf0890SAndreas Gohr        $this->setHTML($html);
249abbf0890SAndreas Gohr        $this->setText($text);
250abbf0890SAndreas Gohr    }
251abbf0890SAndreas Gohr
252abbf0890SAndreas Gohr    /**
253bb01c27cSAndreas Gohr     * Set the HTML part of the mail
254bb01c27cSAndreas Gohr     *
255bb01c27cSAndreas Gohr     * Placeholders can be used to reference embedded attachments
256abbf0890SAndreas Gohr     *
257abbf0890SAndreas Gohr     * You probably want to use setBody() instead
25842ea7f44SGerrit Uitslag     *
25942ea7f44SGerrit Uitslag     * @param string $html
260bb01c27cSAndreas Gohr     */
2611d045709SAndreas Gohr    public function setHTML($html) {
262bb01c27cSAndreas Gohr        $this->html = $html;
263bb01c27cSAndreas Gohr    }
264bb01c27cSAndreas Gohr
265bb01c27cSAndreas Gohr    /**
266bb01c27cSAndreas Gohr     * Set the plain text part of the mail
267abbf0890SAndreas Gohr     *
268abbf0890SAndreas Gohr     * You probably want to use setBody() instead
26942ea7f44SGerrit Uitslag     *
27042ea7f44SGerrit Uitslag     * @param string $text
271bb01c27cSAndreas Gohr     */
2721d045709SAndreas Gohr    public function setText($text) {
273bb01c27cSAndreas Gohr        $this->text = $text;
274bb01c27cSAndreas Gohr    }
275bb01c27cSAndreas Gohr
276bb01c27cSAndreas Gohr    /**
277a36fc348SAndreas Gohr     * Add the To: recipients
278a36fc348SAndreas Gohr     *
2798c253612SGerrit Uitslag     * @see cleanAddress
28059bc3b48SGerrit Uitslag     * @param string|string[]  $address Multiple adresses separated by commas or as array
281a36fc348SAndreas Gohr     */
282a36fc348SAndreas Gohr    public function to($address) {
283a36fc348SAndreas Gohr        $this->setHeader('To', $address, false);
284a36fc348SAndreas Gohr    }
285a36fc348SAndreas Gohr
286a36fc348SAndreas Gohr    /**
287a36fc348SAndreas Gohr     * Add the Cc: recipients
288a36fc348SAndreas Gohr     *
2898c253612SGerrit Uitslag     * @see cleanAddress
29059bc3b48SGerrit Uitslag     * @param string|string[]  $address Multiple adresses separated by commas or as array
291a36fc348SAndreas Gohr     */
292a36fc348SAndreas Gohr    public function cc($address) {
293a36fc348SAndreas Gohr        $this->setHeader('Cc', $address, false);
294a36fc348SAndreas Gohr    }
295a36fc348SAndreas Gohr
296a36fc348SAndreas Gohr    /**
297a36fc348SAndreas Gohr     * Add the Bcc: recipients
298a36fc348SAndreas Gohr     *
2998c253612SGerrit Uitslag     * @see cleanAddress
30059bc3b48SGerrit Uitslag     * @param string|string[]  $address Multiple adresses separated by commas or as array
301a36fc348SAndreas Gohr     */
302a36fc348SAndreas Gohr    public function bcc($address) {
303a36fc348SAndreas Gohr        $this->setHeader('Bcc', $address, false);
304a36fc348SAndreas Gohr    }
305a36fc348SAndreas Gohr
306a36fc348SAndreas Gohr    /**
307a36fc348SAndreas Gohr     * Add the From: address
308a36fc348SAndreas Gohr     *
309a36fc348SAndreas Gohr     * This is set to $conf['mailfrom'] when not specified so you shouldn't need
310a36fc348SAndreas Gohr     * to call this function
311a36fc348SAndreas Gohr     *
3128c253612SGerrit Uitslag     * @see cleanAddress
313a36fc348SAndreas Gohr     * @param string  $address from address
314a36fc348SAndreas Gohr     */
315a36fc348SAndreas Gohr    public function from($address) {
316a36fc348SAndreas Gohr        $this->setHeader('From', $address, false);
317a36fc348SAndreas Gohr    }
318a36fc348SAndreas Gohr
319a36fc348SAndreas Gohr    /**
320a36fc348SAndreas Gohr     * Add the mail's Subject: header
321a36fc348SAndreas Gohr     *
322a36fc348SAndreas Gohr     * @param string $subject the mail subject
323a36fc348SAndreas Gohr     */
324a36fc348SAndreas Gohr    public function subject($subject) {
325a36fc348SAndreas Gohr        $this->headers['Subject'] = $subject;
326a36fc348SAndreas Gohr    }
327a36fc348SAndreas Gohr
328a36fc348SAndreas Gohr    /**
329102cdbd7SLarsGit223     * Return a clean name which can be safely used in mail address
330102cdbd7SLarsGit223     * fields. That means the name will be enclosed in '"' if it includes
331102cdbd7SLarsGit223     * a '"' or a ','. Also a '"' will be escaped as '\"'.
332102cdbd7SLarsGit223     *
333102cdbd7SLarsGit223     * @param string $name the name to clean-up
334102cdbd7SLarsGit223     * @see cleanAddress
335102cdbd7SLarsGit223     */
336102cdbd7SLarsGit223    public function getCleanName($name) {
337102cdbd7SLarsGit223        $name = trim($name, ' \t"');
338102cdbd7SLarsGit223        $name = str_replace('"', '\"', $name, $count);
339102cdbd7SLarsGit223        if ($count > 0 || strpos($name, ',') !== false) {
340102cdbd7SLarsGit223            $name = '"'.$name.'"';
341102cdbd7SLarsGit223        }
342102cdbd7SLarsGit223        return $name;
343102cdbd7SLarsGit223    }
344102cdbd7SLarsGit223
345102cdbd7SLarsGit223    /**
3461d045709SAndreas Gohr     * Sets an email address header with correct encoding
347bb01c27cSAndreas Gohr     *
348bb01c27cSAndreas Gohr     * Unicode characters will be deaccented and encoded base64
349bb01c27cSAndreas Gohr     * for headers. Addresses may not contain Non-ASCII data!
350bb01c27cSAndreas Gohr     *
351102cdbd7SLarsGit223     * If @$addresses is a string then it will be split into multiple
352102cdbd7SLarsGit223     * addresses. Addresses must be separated by a comma. If the display
353102cdbd7SLarsGit223     * name includes a comma then it MUST be properly enclosed by '"' to
354102cdbd7SLarsGit223     * prevent spliting at the wrong point.
355102cdbd7SLarsGit223     *
356bb01c27cSAndreas Gohr     * Example:
3578c253612SGerrit Uitslag     *   cc("föö <foo@bar.com>, me@somewhere.com","TBcc");
358102cdbd7SLarsGit223     *   to("foo, Dr." <foo@bar.com>, me@somewhere.com");
359bb01c27cSAndreas Gohr     *
36042ea7f44SGerrit Uitslag     * @param string|string[]  $addresses Multiple adresses separated by commas or as array
36142ea7f44SGerrit Uitslag     * @return false|string  the prepared header (can contain multiple lines)
362bb01c27cSAndreas Gohr     */
363b6c97c70SAndreas Gohr    public function cleanAddress($addresses) {
364bb01c27cSAndreas Gohr        $headers = '';
365b6c97c70SAndreas Gohr        if(!is_array($addresses)){
366d31a1599SLarsGit223            $count = preg_match_all('/\s*(?:("[^"]*"[^,]+),*)|([^,]+)\s*,*/', $addresses, $matches, PREG_SET_ORDER);
367102cdbd7SLarsGit223            $addresses = array();
368743792d0SLarsGit223            if ($count !== false && is_array($matches)) {
369102cdbd7SLarsGit223                foreach ($matches as $match) {
37010da1f74SAndreas Gohr                    array_push($addresses, rtrim($match[0], ','));
371102cdbd7SLarsGit223                }
372b6c97c70SAndreas Gohr            }
373b6c97c70SAndreas Gohr        }
374b6c97c70SAndreas Gohr
375b6c97c70SAndreas Gohr        foreach($addresses as $part) {
376b6c97c70SAndreas Gohr            $part = preg_replace('/[\r\n\0]+/', ' ', $part); // remove attack vectors
377bb01c27cSAndreas Gohr            $part = trim($part);
378bb01c27cSAndreas Gohr
379bb01c27cSAndreas Gohr            // parse address
380bb01c27cSAndreas Gohr            if(preg_match('#(.*?)<(.*?)>#', $part, $matches)) {
381bb01c27cSAndreas Gohr                $text = trim($matches[1]);
382bb01c27cSAndreas Gohr                $addr = $matches[2];
383bb01c27cSAndreas Gohr            } else {
38410da1f74SAndreas Gohr                $text = '';
385bb01c27cSAndreas Gohr                $addr = $part;
386bb01c27cSAndreas Gohr            }
387bb01c27cSAndreas Gohr            // skip empty ones
388bb01c27cSAndreas Gohr            if(empty($addr)) {
389bb01c27cSAndreas Gohr                continue;
390bb01c27cSAndreas Gohr            }
391bb01c27cSAndreas Gohr
392bb01c27cSAndreas Gohr            // FIXME: is there a way to encode the localpart of a emailaddress?
3938cbc5ee8SAndreas Gohr            if(!\dokuwiki\Utf8\Clean::isASCII($addr)) {
3944772cf38SAndreas Gohr                msg(hsc("E-Mail address <$addr> is not ASCII"), -1, __LINE__, __FILE__, MSG_ADMINS_ONLY);
395bb01c27cSAndreas Gohr                continue;
396bb01c27cSAndreas Gohr            }
397bb01c27cSAndreas Gohr
39864d23c16SAndreas Gohr            if(!mail_isvalid($addr)) {
3994772cf38SAndreas Gohr                msg(hsc("E-Mail address <$addr> is not valid"), -1, __LINE__, __FILE__, MSG_ADMINS_ONLY);
400bb01c27cSAndreas Gohr                continue;
401bb01c27cSAndreas Gohr            }
402bb01c27cSAndreas Gohr
403bb01c27cSAndreas Gohr            // text was given
40430085ef3SYurii K            if(!empty($text) && !isWindows()) { // No named recipients for To: in Windows (see FS#652)
405bb01c27cSAndreas Gohr                // add address quotes
406bb01c27cSAndreas Gohr                $addr = "<$addr>";
407bb01c27cSAndreas Gohr
408bb01c27cSAndreas Gohr                if(defined('MAILHEADER_ASCIIONLY')) {
4098cbc5ee8SAndreas Gohr                    $text = \dokuwiki\Utf8\Clean::deaccent($text);
4108cbc5ee8SAndreas Gohr                    $text = \dokuwiki\Utf8\Clean::strip($text);
411bb01c27cSAndreas Gohr                }
412bb01c27cSAndreas Gohr
4138cbc5ee8SAndreas Gohr                if(strpos($text, ',') !== false || !\dokuwiki\Utf8\Clean::isASCII($text)) {
414bb01c27cSAndreas Gohr                    $text = '=?UTF-8?B?'.base64_encode($text).'?=';
415bb01c27cSAndreas Gohr                }
416bb01c27cSAndreas Gohr            } else {
417bb01c27cSAndreas Gohr                $text = '';
418bb01c27cSAndreas Gohr            }
419bb01c27cSAndreas Gohr
420bb01c27cSAndreas Gohr            // add to header comma seperated
421bb01c27cSAndreas Gohr            if($headers != '') {
422bb01c27cSAndreas Gohr                $headers .= ', ';
423bb01c27cSAndreas Gohr            }
424bb01c27cSAndreas Gohr            $headers .= $text.' '.$addr;
425bb01c27cSAndreas Gohr        }
426bb01c27cSAndreas Gohr
427b6c97c70SAndreas Gohr        $headers = trim($headers);
428bb01c27cSAndreas Gohr        if(empty($headers)) return false;
429bb01c27cSAndreas Gohr
430bb01c27cSAndreas Gohr        return $headers;
431bb01c27cSAndreas Gohr    }
432bb01c27cSAndreas Gohr
433bb01c27cSAndreas Gohr
434bb01c27cSAndreas Gohr    /**
435bb01c27cSAndreas Gohr     * Prepare the mime multiparts for all attachments
436bb01c27cSAndreas Gohr     *
437bb01c27cSAndreas Gohr     * Replaces placeholders in the HTML with the correct CIDs
43842ea7f44SGerrit Uitslag     *
43942ea7f44SGerrit Uitslag     * @return string mime multiparts
440bb01c27cSAndreas Gohr     */
441bb01c27cSAndreas Gohr    protected function prepareAttachments() {
442bb01c27cSAndreas Gohr        $mime = '';
443bb01c27cSAndreas Gohr        $part = 1;
444bb01c27cSAndreas Gohr        // embedded attachments
445bb01c27cSAndreas Gohr        foreach($this->attach as $media) {
446ce9d2cc8SAndreas Gohr            $media['name'] = str_replace(':', '_', cleanID($media['name'], true));
447ce9d2cc8SAndreas Gohr
448bb01c27cSAndreas Gohr            // create content id
449bb01c27cSAndreas Gohr            $cid = 'part'.$part.'.'.$this->partid;
450bb01c27cSAndreas Gohr
451bb01c27cSAndreas Gohr            // replace wildcards
452bb01c27cSAndreas Gohr            if($media['embed']) {
453bb01c27cSAndreas Gohr                $this->html = str_replace('%%'.$media['embed'].'%%', 'cid:'.$cid, $this->html);
454bb01c27cSAndreas Gohr            }
455bb01c27cSAndreas Gohr
456bb01c27cSAndreas Gohr            $mime .= '--'.$this->boundary.MAILHEADER_EOL;
4571d8036c2SAndreas Gohr            $mime .= $this->wrappedHeaderLine('Content-Type', $media['mime'].'; id="'.$cid.'"');
4581d8036c2SAndreas Gohr            $mime .= $this->wrappedHeaderLine('Content-Transfer-Encoding', 'base64');
4591d8036c2SAndreas Gohr            $mime .= $this->wrappedHeaderLine('Content-ID',"<$cid>");
460bb01c27cSAndreas Gohr            if($media['embed']) {
4611d8036c2SAndreas Gohr                $mime .= $this->wrappedHeaderLine('Content-Disposition', 'inline; filename='.$media['name']);
462bb01c27cSAndreas Gohr            } else {
4631d8036c2SAndreas Gohr                $mime .= $this->wrappedHeaderLine('Content-Disposition', 'attachment; filename='.$media['name']);
464bb01c27cSAndreas Gohr            }
465bb01c27cSAndreas Gohr            $mime .= MAILHEADER_EOL; //end of headers
466bb01c27cSAndreas Gohr            $mime .= chunk_split(base64_encode($media['data']), 74, MAILHEADER_EOL);
467bb01c27cSAndreas Gohr
468bb01c27cSAndreas Gohr            $part++;
469bb01c27cSAndreas Gohr        }
470bb01c27cSAndreas Gohr        return $mime;
471bb01c27cSAndreas Gohr    }
472bb01c27cSAndreas Gohr
4731d045709SAndreas Gohr    /**
4741d045709SAndreas Gohr     * Build the body and handles multi part mails
4751d045709SAndreas Gohr     *
4761d045709SAndreas Gohr     * Needs to be called before prepareHeaders!
4771d045709SAndreas Gohr     *
4781d045709SAndreas Gohr     * @return string the prepared mail body, false on errors
4791d045709SAndreas Gohr     */
4801d045709SAndreas Gohr    protected function prepareBody() {
4811d045709SAndreas Gohr
4822398a2b5SAndreas Gohr        // no HTML mails allowed? remove HTML body
4832398a2b5SAndreas Gohr        if(!$this->allowhtml) {
4842398a2b5SAndreas Gohr            $this->html = '';
4852398a2b5SAndreas Gohr        }
4862398a2b5SAndreas Gohr
487bb01c27cSAndreas Gohr        // check for body
488bb01c27cSAndreas Gohr        if(!$this->text && !$this->html) {
489bb01c27cSAndreas Gohr            return false;
490bb01c27cSAndreas Gohr        }
491bb01c27cSAndreas Gohr
492bb01c27cSAndreas Gohr        // add general headers
493bb01c27cSAndreas Gohr        $this->headers['MIME-Version'] = '1.0';
494bb01c27cSAndreas Gohr
4951d045709SAndreas Gohr        $body = '';
4961d045709SAndreas Gohr
497bb01c27cSAndreas Gohr        if(!$this->html && !count($this->attach)) { // we can send a simple single part message
498bb01c27cSAndreas Gohr            $this->headers['Content-Type']              = 'text/plain; charset=UTF-8';
499bb01c27cSAndreas Gohr            $this->headers['Content-Transfer-Encoding'] = 'base64';
500be3cc6abSAndreas Gohr            $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL);
501bb01c27cSAndreas Gohr        } else { // multi part it is
5021d045709SAndreas Gohr            $body .= "This is a multi-part message in MIME format.".MAILHEADER_EOL;
503bb01c27cSAndreas Gohr
504bb01c27cSAndreas Gohr            // prepare the attachments
505bb01c27cSAndreas Gohr            $attachments = $this->prepareAttachments();
506bb01c27cSAndreas Gohr
507bb01c27cSAndreas Gohr            // do we have alternative text content?
508bb01c27cSAndreas Gohr            if($this->text && $this->html) {
509a36fc348SAndreas Gohr                $this->headers['Content-Type'] = 'multipart/alternative;'.MAILHEADER_EOL.
510a36fc348SAndreas Gohr                    '  boundary="'.$this->boundary.'XX"';
511bb01c27cSAndreas Gohr                $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL;
5121d045709SAndreas Gohr                $body .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL;
5131d045709SAndreas Gohr                $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL;
514bb01c27cSAndreas Gohr                $body .= MAILHEADER_EOL;
515be3cc6abSAndreas Gohr                $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL);
516bb01c27cSAndreas Gohr                $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL;
517a36fc348SAndreas Gohr                $body .= 'Content-Type: multipart/related;'.MAILHEADER_EOL.
518d6e04b60SAndreas Gohr                    '  boundary="'.$this->boundary.'";'.MAILHEADER_EOL.
519d6e04b60SAndreas Gohr                    '  type="text/html"'.MAILHEADER_EOL;
520bb01c27cSAndreas Gohr                $body .= MAILHEADER_EOL;
521bb01c27cSAndreas Gohr            }
522bb01c27cSAndreas Gohr
5231d045709SAndreas Gohr            $body .= '--'.$this->boundary.MAILHEADER_EOL;
5241d045709SAndreas Gohr            $body .= 'Content-Type: text/html; charset=UTF-8'.MAILHEADER_EOL;
5251d045709SAndreas Gohr            $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL;
526bb01c27cSAndreas Gohr            $body .= MAILHEADER_EOL;
527be3cc6abSAndreas Gohr            $body .= chunk_split(base64_encode($this->html), 72, MAILHEADER_EOL);
528bb01c27cSAndreas Gohr            $body .= MAILHEADER_EOL;
529bb01c27cSAndreas Gohr            $body .= $attachments;
530bb01c27cSAndreas Gohr            $body .= '--'.$this->boundary.'--'.MAILHEADER_EOL;
531bb01c27cSAndreas Gohr
532bb01c27cSAndreas Gohr            // close open multipart/alternative boundary
533bb01c27cSAndreas Gohr            if($this->text && $this->html) {
534bb01c27cSAndreas Gohr                $body .= '--'.$this->boundary.'XX--'.MAILHEADER_EOL;
535bb01c27cSAndreas Gohr            }
536bb01c27cSAndreas Gohr        }
537bb01c27cSAndreas Gohr
538bb01c27cSAndreas Gohr        return $body;
539bb01c27cSAndreas Gohr    }
540bb01c27cSAndreas Gohr
541bb01c27cSAndreas Gohr    /**
542a36fc348SAndreas Gohr     * Cleanup and encode the headers array
543a36fc348SAndreas Gohr     */
544a36fc348SAndreas Gohr    protected function cleanHeaders() {
545a36fc348SAndreas Gohr        global $conf;
546a36fc348SAndreas Gohr
547a36fc348SAndreas Gohr        // clean up addresses
548a36fc348SAndreas Gohr        if(empty($this->headers['From'])) $this->from($conf['mailfrom']);
549acbf061cSGerrit Uitslag        $addrs = array('To', 'From', 'Cc', 'Bcc', 'Reply-To', 'Sender');
550a36fc348SAndreas Gohr        foreach($addrs as $addr) {
551a36fc348SAndreas Gohr            if(isset($this->headers[$addr])) {
552a36fc348SAndreas Gohr                $this->headers[$addr] = $this->cleanAddress($this->headers[$addr]);
553a36fc348SAndreas Gohr            }
554a36fc348SAndreas Gohr        }
555a36fc348SAndreas Gohr
55645992a63SAndreas Gohr        if(isset($this->headers['Subject'])) {
557a36fc348SAndreas Gohr            // add prefix to subject
55854f30755SAndreas Gohr            if(empty($conf['mailprefix'])) {
5598cbc5ee8SAndreas Gohr                if(\dokuwiki\Utf8\PhpString::strlen($conf['title']) < 20) {
56054f30755SAndreas Gohr                    $prefix = '['.$conf['title'].']';
56154f30755SAndreas Gohr                } else {
5628cbc5ee8SAndreas Gohr                    $prefix = '['.\dokuwiki\Utf8\PhpString::substr($conf['title'], 0, 20).'...]';
5638a215f09SAndreas Gohr                }
5648a215f09SAndreas Gohr            } else {
565a36fc348SAndreas Gohr                $prefix = '['.$conf['mailprefix'].']';
56654f30755SAndreas Gohr            }
567a36fc348SAndreas Gohr            $len = strlen($prefix);
56845992a63SAndreas Gohr            if(substr($this->headers['Subject'], 0, $len) != $prefix) {
56945992a63SAndreas Gohr                $this->headers['Subject'] = $prefix.' '.$this->headers['Subject'];
570a36fc348SAndreas Gohr            }
571a36fc348SAndreas Gohr
572a36fc348SAndreas Gohr            // encode subject
573a36fc348SAndreas Gohr            if(defined('MAILHEADER_ASCIIONLY')) {
5748cbc5ee8SAndreas Gohr                $this->headers['Subject'] = \dokuwiki\Utf8\Clean::deaccent($this->headers['Subject']);
5758cbc5ee8SAndreas Gohr                $this->headers['Subject'] = \dokuwiki\Utf8\Clean::strip($this->headers['Subject']);
576a36fc348SAndreas Gohr            }
5778cbc5ee8SAndreas Gohr            if(!\dokuwiki\Utf8\Clean::isASCII($this->headers['Subject'])) {
57845992a63SAndreas Gohr                $this->headers['Subject'] = '=?UTF-8?B?'.base64_encode($this->headers['Subject']).'?=';
579a36fc348SAndreas Gohr            }
580a36fc348SAndreas Gohr        }
581a36fc348SAndreas Gohr
582a36fc348SAndreas Gohr    }
5831d8036c2SAndreas Gohr
5841d8036c2SAndreas Gohr    /**
5851d8036c2SAndreas Gohr     * Returns a complete, EOL terminated header line, wraps it if necessary
5861d8036c2SAndreas Gohr     *
58742ea7f44SGerrit Uitslag     * @param string $key
58842ea7f44SGerrit Uitslag     * @param string $val
58942ea7f44SGerrit Uitslag     * @return string line
5901d8036c2SAndreas Gohr     */
5911d8036c2SAndreas Gohr    protected function wrappedHeaderLine($key, $val){
5921d8036c2SAndreas Gohr        return wordwrap("$key: $val", 78, MAILHEADER_EOL.'  ').MAILHEADER_EOL;
593a36fc348SAndreas Gohr    }
594a36fc348SAndreas Gohr
595a36fc348SAndreas Gohr    /**
596bb01c27cSAndreas Gohr     * Create a string from the headers array
5971d045709SAndreas Gohr     *
5981d045709SAndreas Gohr     * @returns string the headers
599bb01c27cSAndreas Gohr     */
600bb01c27cSAndreas Gohr    protected function prepareHeaders() {
601bb01c27cSAndreas Gohr        $headers = '';
602bb01c27cSAndreas Gohr        foreach($this->headers as $key => $val) {
603749c0023SAndreas Gohr            if ($val === '' || $val === null) continue;
6041d8036c2SAndreas Gohr            $headers .= $this->wrappedHeaderLine($key, $val);
605bb01c27cSAndreas Gohr        }
606bb01c27cSAndreas Gohr        return $headers;
607bb01c27cSAndreas Gohr    }
608bb01c27cSAndreas Gohr
609bb01c27cSAndreas Gohr    /**
610bb01c27cSAndreas Gohr     * return a full email with all headers
611bb01c27cSAndreas Gohr     *
6121d045709SAndreas Gohr     * This is mainly intended for debugging and testing but could also be
6131d045709SAndreas Gohr     * used for MHT exports
6141d045709SAndreas Gohr     *
6151d045709SAndreas Gohr     * @return string the mail, false on errors
616bb01c27cSAndreas Gohr     */
617bb01c27cSAndreas Gohr    public function dump() {
618a36fc348SAndreas Gohr        $this->cleanHeaders();
619bb01c27cSAndreas Gohr        $body = $this->prepareBody();
6204d18e936SAndreas Gohr        if($body === false) return false;
6211d045709SAndreas Gohr        $headers = $this->prepareHeaders();
622bb01c27cSAndreas Gohr
623bb01c27cSAndreas Gohr        return $headers.MAILHEADER_EOL.$body;
624bb01c27cSAndreas Gohr    }
6251d045709SAndreas Gohr
6261d045709SAndreas Gohr    /**
6279ea45836SChristopher Smith     * Prepare default token replacement strings
6289ea45836SChristopher Smith     *
6299ea45836SChristopher Smith     * Populates the '$replacements' property.
6309ea45836SChristopher Smith     * Should be called by the class constructor
6319ea45836SChristopher Smith     */
6329ea45836SChristopher Smith    protected function prepareTokenReplacements() {
6339ea45836SChristopher Smith        global $INFO;
6349ea45836SChristopher Smith        global $conf;
6359ea45836SChristopher Smith        /* @var Input $INPUT */
6369ea45836SChristopher Smith        global $INPUT;
6379ea45836SChristopher Smith        global $lang;
6389ea45836SChristopher Smith
6399ea45836SChristopher Smith        $ip   = clientIP();
6409ea45836SChristopher Smith        $cip  = gethostsbyaddrs($ip);
641*8fa268b3SAndreas Gohr        $name = $INFO['userinfo']['name'] ?? '';
642*8fa268b3SAndreas Gohr        $mail = $INFO['userinfo']['mail'] ?? '';
6439ea45836SChristopher Smith
6449ea45836SChristopher Smith        $this->replacements['text'] = array(
6459ea45836SChristopher Smith            'DATE' => dformat(),
6469ea45836SChristopher Smith            'BROWSER' => $INPUT->server->str('HTTP_USER_AGENT'),
6479ea45836SChristopher Smith            'IPADDRESS' => $ip,
6489ea45836SChristopher Smith            'HOSTNAME' => $cip,
6499ea45836SChristopher Smith            'TITLE' => $conf['title'],
6509ea45836SChristopher Smith            'DOKUWIKIURL' => DOKU_URL,
6519ea45836SChristopher Smith            'USER' => $INPUT->server->str('REMOTE_USER'),
65268491db9SPhy            'NAME' => $name,
65368491db9SPhy            'MAIL' => $mail
6549ea45836SChristopher Smith        );
65564159a61SAndreas Gohr        $signature = str_replace(
65664159a61SAndreas Gohr            '@DOKUWIKIURL@',
65764159a61SAndreas Gohr            $this->replacements['text']['DOKUWIKIURL'],
65864159a61SAndreas Gohr            $lang['email_signature_text']
65964159a61SAndreas Gohr        );
660774514c9SGerrit Uitslag        $this->replacements['text']['EMAILSIGNATURE'] = "\n-- \n" . $signature . "\n";
6619ea45836SChristopher Smith
6629ea45836SChristopher Smith        $this->replacements['html'] = array(
6639ea45836SChristopher Smith            'DATE' => '<i>' . hsc(dformat()) . '</i>',
6649ea45836SChristopher Smith            'BROWSER' => hsc($INPUT->server->str('HTTP_USER_AGENT')),
6659ea45836SChristopher Smith            'IPADDRESS' => '<code>' . hsc($ip) . '</code>',
6669ea45836SChristopher Smith            'HOSTNAME' => '<code>' . hsc($cip) . '</code>',
6679ea45836SChristopher Smith            'TITLE' => hsc($conf['title']),
6689ea45836SChristopher Smith            'DOKUWIKIURL' => '<a href="' . DOKU_URL . '">' . DOKU_URL . '</a>',
6699ea45836SChristopher Smith            'USER' => hsc($INPUT->server->str('REMOTE_USER')),
67068491db9SPhy            'NAME' => hsc($name),
67168491db9SPhy            'MAIL' => '<a href="mailto:"' . hsc($mail) . '">' .
67268491db9SPhy                hsc($mail) . '</a>'
6739ea45836SChristopher Smith        );
674774514c9SGerrit Uitslag        $signature = $lang['email_signature_text'];
675774514c9SGerrit Uitslag        if(!empty($lang['email_signature_html'])) {
676774514c9SGerrit Uitslag            $signature = $lang['email_signature_html'];
677774514c9SGerrit Uitslag        }
678774514c9SGerrit Uitslag        $signature = str_replace(
679774514c9SGerrit Uitslag            array(
680774514c9SGerrit Uitslag                '@DOKUWIKIURL@',
681774514c9SGerrit Uitslag                "\n"
682774514c9SGerrit Uitslag            ),
683774514c9SGerrit Uitslag            array(
684774514c9SGerrit Uitslag                $this->replacements['html']['DOKUWIKIURL'],
685774514c9SGerrit Uitslag                '<br />'
686774514c9SGerrit Uitslag            ),
687774514c9SGerrit Uitslag            $signature
688774514c9SGerrit Uitslag        );
689774514c9SGerrit Uitslag        $this->replacements['html']['EMAILSIGNATURE'] = $signature;
6909ea45836SChristopher Smith    }
6919ea45836SChristopher Smith
6929ea45836SChristopher Smith    /**
6931d045709SAndreas Gohr     * Send the mail
6941d045709SAndreas Gohr     *
6951d045709SAndreas Gohr     * Call this after all data was set
6961d045709SAndreas Gohr     *
69728d2ad80SAndreas Gohr     * @triggers MAIL_MESSAGE_SEND
6981d045709SAndreas Gohr     * @return bool true if the mail was successfully passed to the MTA
6991d045709SAndreas Gohr     */
7001d045709SAndreas Gohr    public function send() {
7013f6872b1SMyron Turner        global $lang;
70228d2ad80SAndreas Gohr        $success = false;
703a36fc348SAndreas Gohr
70428d2ad80SAndreas Gohr        // prepare hook data
70528d2ad80SAndreas Gohr        $data = array(
70628d2ad80SAndreas Gohr            // pass the whole mail class to plugin
70728d2ad80SAndreas Gohr            'mail'    => $this,
70828d2ad80SAndreas Gohr            // pass references for backward compatibility
70928d2ad80SAndreas Gohr            'to'      => &$this->headers['To'],
71028d2ad80SAndreas Gohr            'cc'      => &$this->headers['Cc'],
71128d2ad80SAndreas Gohr            'bcc'     => &$this->headers['Bcc'],
71228d2ad80SAndreas Gohr            'from'    => &$this->headers['From'],
71328d2ad80SAndreas Gohr            'subject' => &$this->headers['Subject'],
71428d2ad80SAndreas Gohr            'body'    => &$this->text,
715a89c75afSAndreas Gohr            'params'  => &$this->sendparam,
71628d2ad80SAndreas Gohr            'headers' => '', // plugins shouldn't use this
71728d2ad80SAndreas Gohr            // signal if we mailed successfully to AFTER event
71828d2ad80SAndreas Gohr            'success' => &$success,
71928d2ad80SAndreas Gohr        );
72028d2ad80SAndreas Gohr
72128d2ad80SAndreas Gohr        // do our thing if BEFORE hook approves
722e1d9dcc8SAndreas Gohr        $evt = new Event('MAIL_MESSAGE_SEND', $data);
72328d2ad80SAndreas Gohr        if($evt->advise_before(true)) {
72428d2ad80SAndreas Gohr            // clean up before using the headers
725a36fc348SAndreas Gohr            $this->cleanHeaders();
726a36fc348SAndreas Gohr
7271d045709SAndreas Gohr            // any recipients?
7281d045709SAndreas Gohr            if(trim($this->headers['To']) === '' &&
7291d045709SAndreas Gohr                trim($this->headers['Cc']) === '' &&
730a89c75afSAndreas Gohr                trim($this->headers['Bcc']) === ''
731a89c75afSAndreas Gohr            ) return false;
7321d045709SAndreas Gohr
7331d045709SAndreas Gohr            // The To: header is special
7346be717dbSMichael Hamann            if(array_key_exists('To', $this->headers)) {
7356be717dbSMichael Hamann                $to = (string)$this->headers['To'];
7361d045709SAndreas Gohr                unset($this->headers['To']);
7371d045709SAndreas Gohr            } else {
7381d045709SAndreas Gohr                $to = '';
7391d045709SAndreas Gohr            }
7401d045709SAndreas Gohr
7411d045709SAndreas Gohr            // so is the subject
7426be717dbSMichael Hamann            if(array_key_exists('Subject', $this->headers)) {
7436be717dbSMichael Hamann                $subject = (string)$this->headers['Subject'];
7441d045709SAndreas Gohr                unset($this->headers['Subject']);
7451d045709SAndreas Gohr            } else {
7461d045709SAndreas Gohr                $subject = '';
7471d045709SAndreas Gohr            }
7481d045709SAndreas Gohr
7491d045709SAndreas Gohr            // make the body
7501d045709SAndreas Gohr            $body = $this->prepareBody();
7514c89a7f6SAndreas Gohr            if($body === false) return false;
7521d045709SAndreas Gohr
7531d045709SAndreas Gohr            // cook the headers
7541d045709SAndreas Gohr            $headers = $this->prepareHeaders();
75528d2ad80SAndreas Gohr            // add any headers set by legacy plugins
75628d2ad80SAndreas Gohr            if(trim($data['headers'])) {
75728d2ad80SAndreas Gohr                $headers .= MAILHEADER_EOL.trim($data['headers']);
75828d2ad80SAndreas Gohr            }
7591d045709SAndreas Gohr
7603f6872b1SMyron Turner            if(!function_exists('mail')){
7613f6872b1SMyron Turner                $emsg = $lang['email_fail'] . $subject;
7623f6872b1SMyron Turner                error_log($emsg);
7633f6872b1SMyron Turner                msg(hsc($emsg), -1, __LINE__, __FILE__, MSG_MANAGERS_ONLY);
7643f6872b1SMyron Turner                $evt->advise_after();
7653f6872b1SMyron Turner                return false;
7663f6872b1SMyron Turner            }
7673f6872b1SMyron Turner
7681d045709SAndreas Gohr            // send the thing
769749c0023SAndreas Gohr            if($this->sendparam === null) {
77028d2ad80SAndreas Gohr                $success = @mail($to, $subject, $body, $headers);
7711d045709SAndreas Gohr            } else {
77228d2ad80SAndreas Gohr                $success = @mail($to, $subject, $body, $headers, $this->sendparam);
7731d045709SAndreas Gohr            }
7741d045709SAndreas Gohr        }
77528d2ad80SAndreas Gohr        // any AFTER actions?
77628d2ad80SAndreas Gohr        $evt->advise_after();
77728d2ad80SAndreas Gohr        return $success;
77828d2ad80SAndreas Gohr    }
779bb01c27cSAndreas Gohr}
780