xref: /dokuwiki/inc/Mailer.class.php (revision d868eb89f182718a31113373a6272670bd7f8012)
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 */
1124870174SAndreas Gohruse dokuwiki\Utf8\PhpString;
1224870174SAndreas Gohruse dokuwiki\Utf8\Clean;
13e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event;
14e1d9dcc8SAndreas Gohr
15a89c75afSAndreas Gohr/**
16a89c75afSAndreas Gohr * Mail Handling
17a89c75afSAndreas Gohr */
188c7c53b0SAndreas Gohrclass Mailer
198c7c53b0SAndreas Gohr{
20bb01c27cSAndreas Gohr
2124870174SAndreas Gohr    protected $headers   = [];
2224870174SAndreas Gohr    protected $attach    = [];
23f41c79d7SAndreas Gohr    protected $html      = '';
24f41c79d7SAndreas Gohr    protected $text      = '';
25bb01c27cSAndreas Gohr
26f41c79d7SAndreas Gohr    protected $boundary  = '';
27f41c79d7SAndreas Gohr    protected $partid    = '';
2824870174SAndreas Gohr    protected $sendparam;
29bb01c27cSAndreas Gohr
30f41c79d7SAndreas Gohr    protected $allowhtml = true;
311d045709SAndreas Gohr
3224870174SAndreas Gohr    protected $replacements = ['text'=> [], 'html' => []];
339ea45836SChristopher Smith
341d045709SAndreas Gohr    /**
351d045709SAndreas Gohr     * Constructor
361d045709SAndreas Gohr     *
379ea45836SChristopher Smith     * Initializes the boundary strings, part counters and token replacements
381d045709SAndreas Gohr     */
39*d868eb89SAndreas Gohr    public function __construct()
40*d868eb89SAndreas Gohr    {
419f3eca0bSAndreas Gohr        global $conf;
42585bf44eSChristopher Smith        /* @var Input $INPUT */
43585bf44eSChristopher Smith        global $INPUT;
449f3eca0bSAndreas Gohr
459f3eca0bSAndreas Gohr        $server = parse_url(DOKU_URL, PHP_URL_HOST);
46749c0023SAndreas Gohr        if(strpos($server, '.') === false) $server .= '.localhost';
471d045709SAndreas Gohr
4824870174SAndreas Gohr        $this->partid   = substr(md5(uniqid(random_int(0, mt_getrandmax()), true)), 0, 8).'@'.$server;
4924870174SAndreas Gohr        $this->boundary = '__________'.md5(uniqid(random_int(0, mt_getrandmax()), true));
509f3eca0bSAndreas Gohr
51749c0023SAndreas Gohr        $listid = implode('.', array_reverse(explode('/', DOKU_BASE))).$server;
529f3eca0bSAndreas Gohr        $listid = strtolower(trim($listid, '.'));
5324870174SAndreas Gohr
5424870174SAndreas Gohr        $messageid = uniqid(random_int(0, mt_getrandmax()), true) . "@$server";
559f3eca0bSAndreas Gohr
562398a2b5SAndreas Gohr        $this->allowhtml = (bool)$conf['htmlmail'];
572398a2b5SAndreas Gohr
589f3eca0bSAndreas Gohr        // add some default headers for mailfiltering FS#2247
595f43dcf4SLukas Rademacher        if(!empty($conf['mailreturnpath'])) {
605f43dcf4SLukas Rademacher            $this->setHeader('Return-Path', $conf['mailreturnpath']);
615f43dcf4SLukas Rademacher        }
626a1f928fSAndreas Gohr        $this->setHeader('X-Mailer', 'DokuWiki');
63585bf44eSChristopher Smith        $this->setHeader('X-DokuWiki-User', $INPUT->server->str('REMOTE_USER'));
649f3eca0bSAndreas Gohr        $this->setHeader('X-DokuWiki-Title', $conf['title']);
659f3eca0bSAndreas Gohr        $this->setHeader('X-DokuWiki-Server', $server);
669f3eca0bSAndreas Gohr        $this->setHeader('X-Auto-Response-Suppress', 'OOF');
679f3eca0bSAndreas Gohr        $this->setHeader('List-Id', $conf['title'].' <'.$listid.'>');
68d6e04b60SAndreas Gohr        $this->setHeader('Date', date('r'), false);
697c7659d2SPhilipp Specht        $this->setHeader('Message-Id', "<$messageid>");
709ea45836SChristopher Smith
719ea45836SChristopher Smith        $this->prepareTokenReplacements();
72bb01c27cSAndreas Gohr    }
73bb01c27cSAndreas Gohr
74bb01c27cSAndreas Gohr    /**
75bb01c27cSAndreas Gohr     * Attach a file
76bb01c27cSAndreas Gohr     *
77a89c75afSAndreas Gohr     * @param string $path  Path to the file to attach
78a89c75afSAndreas Gohr     * @param string $mime  Mimetype of the attached file
79a89c75afSAndreas Gohr     * @param string $name The filename to use
80a89c75afSAndreas Gohr     * @param string $embed Unique key to reference this file from the HTML part
81bb01c27cSAndreas Gohr     */
82*d868eb89SAndreas Gohr    public function attachFile($path, $mime, $name = '', $embed = '')
83*d868eb89SAndreas Gohr    {
84bb01c27cSAndreas Gohr        if(!$name) {
8524870174SAndreas Gohr            $name = PhpString::basename($path);
86bb01c27cSAndreas Gohr        }
87bb01c27cSAndreas Gohr
8824870174SAndreas Gohr        $this->attach[] = [
89bb01c27cSAndreas Gohr            'data'  => file_get_contents($path),
90bb01c27cSAndreas Gohr            'mime'  => $mime,
91bb01c27cSAndreas Gohr            'name'  => $name,
92bb01c27cSAndreas Gohr            'embed' => $embed
9324870174SAndreas 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     */
104*d868eb89SAndreas Gohr    public function attachContent($data, $mime, $name = '', $embed = '')
105*d868eb89SAndreas Gohr    {
106bb01c27cSAndreas Gohr        if(!$name) {
10724870174SAndreas Gohr            [, $ext] = explode('/', $mime);
108bb01c27cSAndreas Gohr            $name = count($this->attach).".$ext";
109bb01c27cSAndreas Gohr        }
110bb01c27cSAndreas Gohr
11124870174SAndreas Gohr        $this->attach[] = [
112bb01c27cSAndreas Gohr            'data'  => $data,
113bb01c27cSAndreas Gohr            'mime'  => $mime,
114bb01c27cSAndreas Gohr            'name'  => $name,
115bb01c27cSAndreas Gohr            'embed' => $embed
11624870174SAndreas Gohr        ];
117bb01c27cSAndreas Gohr    }
118bb01c27cSAndreas Gohr
119bb01c27cSAndreas Gohr    /**
120850dbf1fSAndreas Gohr     * Callback function to automatically embed images referenced in HTML templates
12142ea7f44SGerrit Uitslag     *
12242ea7f44SGerrit Uitslag     * @param array $matches
12342ea7f44SGerrit Uitslag     * @return string placeholder
124850dbf1fSAndreas Gohr     */
125*d868eb89SAndreas Gohr    protected function autoEmbedCallBack($matches)
126*d868eb89SAndreas Gohr    {
127850dbf1fSAndreas Gohr        static $embeds = 0;
128850dbf1fSAndreas Gohr        $embeds++;
129850dbf1fSAndreas Gohr
130850dbf1fSAndreas Gohr        // get file and mime type
131850dbf1fSAndreas Gohr        $media = cleanID($matches[1]);
13224870174SAndreas Gohr        [, $mime] = mimetype($media);
133850dbf1fSAndreas Gohr        $file = mediaFN($media);
134850dbf1fSAndreas Gohr        if(!file_exists($file)) return $matches[0]; //bad reference, keep as is
135850dbf1fSAndreas Gohr
136850dbf1fSAndreas Gohr        // attach it and set placeholder
137850dbf1fSAndreas Gohr        $this->attachFile($file, $mime, '', 'autoembed'.$embeds);
138850dbf1fSAndreas Gohr        return '%%autoembed'.$embeds.'%%';
139850dbf1fSAndreas Gohr    }
140850dbf1fSAndreas Gohr
141850dbf1fSAndreas Gohr    /**
1421d045709SAndreas Gohr     * Add an arbitrary header to the mail
1431d045709SAndreas Gohr     *
144a36fc348SAndreas Gohr     * If an empy value is passed, the header is removed
145a36fc348SAndreas Gohr     *
1461d045709SAndreas Gohr     * @param string $header the header name (no trailing colon!)
14759bc3b48SGerrit Uitslag     * @param string|string[] $value  the value of the header
1481d045709SAndreas Gohr     * @param bool   $clean  remove all non-ASCII chars and line feeds?
1491d045709SAndreas Gohr     */
150*d868eb89SAndreas Gohr    public function setHeader($header, $value, $clean = true)
151*d868eb89SAndreas Gohr    {
1529f3eca0bSAndreas Gohr        $header = str_replace(' ', '-', ucwords(strtolower(str_replace('-', ' ', $header)))); // streamline casing
1531d045709SAndreas Gohr        if($clean) {
154578b2c23SAndreas Gohr            $header = preg_replace('/[^a-zA-Z0-9_ \-\.\+\@]+/', '', $header);
155578b2c23SAndreas Gohr            $value  = preg_replace('/[^a-zA-Z0-9_ \-\.\+\@<>]+/', '', $value);
1561d045709SAndreas Gohr        }
157a36fc348SAndreas Gohr
158a36fc348SAndreas Gohr        // empty value deletes
159b6c97c70SAndreas Gohr        if(is_array($value)){
160b6c97c70SAndreas Gohr            $value = array_map('trim', $value);
161b6c97c70SAndreas Gohr            $value = array_filter($value);
162b6c97c70SAndreas Gohr            if(!$value) $value = '';
163b6c97c70SAndreas Gohr        }else{
164a36fc348SAndreas Gohr            $value = trim($value);
165b6c97c70SAndreas Gohr        }
166a36fc348SAndreas Gohr        if($value === '') {
167a36fc348SAndreas Gohr            if(isset($this->headers[$header])) unset($this->headers[$header]);
168a36fc348SAndreas Gohr        } else {
1691d045709SAndreas Gohr            $this->headers[$header] = $value;
1701d045709SAndreas Gohr        }
171a36fc348SAndreas Gohr    }
1721d045709SAndreas Gohr
1731d045709SAndreas Gohr    /**
1741d045709SAndreas Gohr     * Set additional parameters to be passed to sendmail
1751d045709SAndreas Gohr     *
1761d045709SAndreas Gohr     * Whatever is set here is directly passed to PHP's mail() command as last
1771d045709SAndreas Gohr     * parameter. Depending on the PHP setup this might break mailing alltogether
17842ea7f44SGerrit Uitslag     *
17942ea7f44SGerrit Uitslag     * @param string $param
1801d045709SAndreas Gohr     */
181*d868eb89SAndreas Gohr    public function setParameters($param)
182*d868eb89SAndreas Gohr    {
1831d045709SAndreas Gohr        $this->sendparam = $param;
1841d045709SAndreas Gohr    }
1851d045709SAndreas Gohr
1861d045709SAndreas Gohr    /**
187abbf0890SAndreas Gohr     * Set the text and HTML body and apply replacements
188abbf0890SAndreas Gohr     *
189abbf0890SAndreas Gohr     * This function applies a whole bunch of default replacements in addition
19004dcb5b2SChristopher Smith     * to the ones specified as parameters
191abbf0890SAndreas Gohr     *
192abbf0890SAndreas Gohr     * If you pass the HTML part or HTML replacements yourself you have to make
193abbf0890SAndreas Gohr     * sure you encode all HTML special chars correctly
194abbf0890SAndreas Gohr     *
195abbf0890SAndreas Gohr     * @param string $text     plain text body
196abbf0890SAndreas Gohr     * @param array  $textrep  replacements to apply on the text part
19764159a61SAndreas Gohr     * @param array  $htmlrep  replacements to apply on the HTML part, null to use $textrep (urls wrapped in <a> tags)
19859bc3b48SGerrit Uitslag     * @param string $html     the HTML body, leave null to create it from $text
199f08086ecSAndreas Gohr     * @param bool   $wrap     wrap the HTML in the default header/Footer
200abbf0890SAndreas Gohr     */
201*d868eb89SAndreas Gohr    public function setBody($text, $textrep = null, $htmlrep = null, $html = null, $wrap = true)
202*d868eb89SAndreas Gohr    {
203585bf44eSChristopher Smith
20476efd6d0SAndreas Gohr        $htmlrep = (array)$htmlrep;
20576efd6d0SAndreas Gohr        $textrep = (array)$textrep;
206abbf0890SAndreas Gohr
207abbf0890SAndreas Gohr        // create HTML from text if not given
208749c0023SAndreas Gohr        if($html === null) {
209ba9c057bSAndreas Gohr            $html = $text;
210ba9c057bSAndreas Gohr            $html = hsc($html);
211ba2c2f17Sfurun            $html = preg_replace('/^----+$/m', '<hr >', $html);
212ba9c057bSAndreas Gohr            $html = nl2br($html);
213abbf0890SAndreas Gohr        }
214f08086ecSAndreas Gohr        if($wrap) {
215749c0023SAndreas Gohr            $wrapper = rawLocale('mailwrap', 'html');
2163819cafdSfurun            $html = preg_replace('/\n-- <br \/>.*$/s', '', $html); //strip signature
2173819cafdSfurun            $html = str_replace('@EMAILSIGNATURE@', '', $html); //strip @EMAILSIGNATURE@
218749c0023SAndreas Gohr            $html = str_replace('@HTMLBODY@', $html, $wrapper);
219f08086ecSAndreas Gohr        }
220f08086ecSAndreas Gohr
2213819cafdSfurun        if(strpos($text, '@EMAILSIGNATURE@') === false) {
2229ea45836SChristopher Smith            $text .= '@EMAILSIGNATURE@';
2233819cafdSfurun        }
224ba2c2f17Sfurun
22576efd6d0SAndreas Gohr        // copy over all replacements missing for HTML (autolink URLs)
22676efd6d0SAndreas Gohr        foreach($textrep as $key => $value) {
22776efd6d0SAndreas Gohr            if(isset($htmlrep[$key])) continue;
2283e7e6067SKlap-in            if(media_isexternal($value)) {
22976efd6d0SAndreas Gohr                $htmlrep[$key] = '<a href="'.hsc($value).'">'.hsc($value).'</a>';
23076efd6d0SAndreas Gohr            } else {
23176efd6d0SAndreas Gohr                $htmlrep[$key] = hsc($value);
23276efd6d0SAndreas Gohr            }
233abbf0890SAndreas Gohr        }
234abbf0890SAndreas Gohr
235850dbf1fSAndreas Gohr        // embed media from templates
236a89c75afSAndreas Gohr        $html = preg_replace_callback(
237a89c75afSAndreas Gohr            '/@MEDIA\(([^\)]+)\)@/',
23824870174SAndreas Gohr            [$this, 'autoEmbedCallBack'], $html
239a89c75afSAndreas Gohr        );
240850dbf1fSAndreas Gohr
2419ea45836SChristopher Smith        // add default token replacements
24224870174SAndreas Gohr        $trep = array_merge($this->replacements['text'], $textrep);
24324870174SAndreas Gohr        $hrep = array_merge($this->replacements['html'], $htmlrep);
244abbf0890SAndreas Gohr
245abbf0890SAndreas Gohr        // Apply replacements
246abbf0890SAndreas Gohr        foreach($trep as $key => $substitution) {
247abbf0890SAndreas Gohr            $text = str_replace('@'.strtoupper($key).'@', $substitution, $text);
248abbf0890SAndreas Gohr        }
249abbf0890SAndreas Gohr        foreach($hrep as $key => $substitution) {
250abbf0890SAndreas Gohr            $html = str_replace('@'.strtoupper($key).'@', $substitution, $html);
251abbf0890SAndreas Gohr        }
252abbf0890SAndreas Gohr
253abbf0890SAndreas Gohr        $this->setHTML($html);
254abbf0890SAndreas Gohr        $this->setText($text);
255abbf0890SAndreas Gohr    }
256abbf0890SAndreas Gohr
257abbf0890SAndreas Gohr    /**
258bb01c27cSAndreas Gohr     * Set the HTML part of the mail
259bb01c27cSAndreas Gohr     *
260bb01c27cSAndreas Gohr     * Placeholders can be used to reference embedded attachments
261abbf0890SAndreas Gohr     *
262abbf0890SAndreas Gohr     * You probably want to use setBody() instead
26342ea7f44SGerrit Uitslag     *
26442ea7f44SGerrit Uitslag     * @param string $html
265bb01c27cSAndreas Gohr     */
266*d868eb89SAndreas Gohr    public function setHTML($html)
267*d868eb89SAndreas Gohr    {
268bb01c27cSAndreas Gohr        $this->html = $html;
269bb01c27cSAndreas Gohr    }
270bb01c27cSAndreas Gohr
271bb01c27cSAndreas Gohr    /**
272bb01c27cSAndreas Gohr     * Set the plain text part of the mail
273abbf0890SAndreas Gohr     *
274abbf0890SAndreas Gohr     * You probably want to use setBody() instead
27542ea7f44SGerrit Uitslag     *
27642ea7f44SGerrit Uitslag     * @param string $text
277bb01c27cSAndreas Gohr     */
278*d868eb89SAndreas Gohr    public function setText($text)
279*d868eb89SAndreas Gohr    {
280bb01c27cSAndreas Gohr        $this->text = $text;
281bb01c27cSAndreas Gohr    }
282bb01c27cSAndreas Gohr
283bb01c27cSAndreas Gohr    /**
284a36fc348SAndreas Gohr     * Add the To: recipients
285a36fc348SAndreas Gohr     *
2868c253612SGerrit Uitslag     * @see cleanAddress
28759bc3b48SGerrit Uitslag     * @param string|string[]  $address Multiple adresses separated by commas or as array
288a36fc348SAndreas Gohr     */
289*d868eb89SAndreas Gohr    public function to($address)
290*d868eb89SAndreas Gohr    {
291a36fc348SAndreas Gohr        $this->setHeader('To', $address, false);
292a36fc348SAndreas Gohr    }
293a36fc348SAndreas Gohr
294a36fc348SAndreas Gohr    /**
295a36fc348SAndreas Gohr     * Add the Cc: recipients
296a36fc348SAndreas Gohr     *
2978c253612SGerrit Uitslag     * @see cleanAddress
29859bc3b48SGerrit Uitslag     * @param string|string[]  $address Multiple adresses separated by commas or as array
299a36fc348SAndreas Gohr     */
300*d868eb89SAndreas Gohr    public function cc($address)
301*d868eb89SAndreas Gohr    {
302a36fc348SAndreas Gohr        $this->setHeader('Cc', $address, false);
303a36fc348SAndreas Gohr    }
304a36fc348SAndreas Gohr
305a36fc348SAndreas Gohr    /**
306a36fc348SAndreas Gohr     * Add the Bcc: recipients
307a36fc348SAndreas Gohr     *
3088c253612SGerrit Uitslag     * @see cleanAddress
30959bc3b48SGerrit Uitslag     * @param string|string[]  $address Multiple adresses separated by commas or as array
310a36fc348SAndreas Gohr     */
311*d868eb89SAndreas Gohr    public function bcc($address)
312*d868eb89SAndreas Gohr    {
313a36fc348SAndreas Gohr        $this->setHeader('Bcc', $address, false);
314a36fc348SAndreas Gohr    }
315a36fc348SAndreas Gohr
316a36fc348SAndreas Gohr    /**
317a36fc348SAndreas Gohr     * Add the From: address
318a36fc348SAndreas Gohr     *
319a36fc348SAndreas Gohr     * This is set to $conf['mailfrom'] when not specified so you shouldn't need
320a36fc348SAndreas Gohr     * to call this function
321a36fc348SAndreas Gohr     *
3228c253612SGerrit Uitslag     * @see cleanAddress
323a36fc348SAndreas Gohr     * @param string  $address from address
324a36fc348SAndreas Gohr     */
325*d868eb89SAndreas Gohr    public function from($address)
326*d868eb89SAndreas Gohr    {
327a36fc348SAndreas Gohr        $this->setHeader('From', $address, false);
328a36fc348SAndreas Gohr    }
329a36fc348SAndreas Gohr
330a36fc348SAndreas Gohr    /**
331a36fc348SAndreas Gohr     * Add the mail's Subject: header
332a36fc348SAndreas Gohr     *
333a36fc348SAndreas Gohr     * @param string $subject the mail subject
334a36fc348SAndreas Gohr     */
335*d868eb89SAndreas Gohr    public function subject($subject)
336*d868eb89SAndreas Gohr    {
337a36fc348SAndreas Gohr        $this->headers['Subject'] = $subject;
338a36fc348SAndreas Gohr    }
339a36fc348SAndreas Gohr
340a36fc348SAndreas Gohr    /**
341102cdbd7SLarsGit223     * Return a clean name which can be safely used in mail address
342102cdbd7SLarsGit223     * fields. That means the name will be enclosed in '"' if it includes
343102cdbd7SLarsGit223     * a '"' or a ','. Also a '"' will be escaped as '\"'.
344102cdbd7SLarsGit223     *
345102cdbd7SLarsGit223     * @param string $name the name to clean-up
346102cdbd7SLarsGit223     * @see cleanAddress
347102cdbd7SLarsGit223     */
348*d868eb89SAndreas Gohr    public function getCleanName($name)
349*d868eb89SAndreas Gohr    {
3502b58f049SAndreas Gohr        $name = trim($name, " \t\"");
351102cdbd7SLarsGit223        $name = str_replace('"', '\"', $name, $count);
352102cdbd7SLarsGit223        if ($count > 0 || strpos($name, ',') !== false) {
353102cdbd7SLarsGit223            $name = '"'.$name.'"';
354102cdbd7SLarsGit223        }
355102cdbd7SLarsGit223        return $name;
356102cdbd7SLarsGit223    }
357102cdbd7SLarsGit223
358102cdbd7SLarsGit223    /**
3591d045709SAndreas Gohr     * Sets an email address header with correct encoding
360bb01c27cSAndreas Gohr     *
361bb01c27cSAndreas Gohr     * Unicode characters will be deaccented and encoded base64
362bb01c27cSAndreas Gohr     * for headers. Addresses may not contain Non-ASCII data!
363bb01c27cSAndreas Gohr     *
364102cdbd7SLarsGit223     * If @$addresses is a string then it will be split into multiple
365102cdbd7SLarsGit223     * addresses. Addresses must be separated by a comma. If the display
366102cdbd7SLarsGit223     * name includes a comma then it MUST be properly enclosed by '"' to
367102cdbd7SLarsGit223     * prevent spliting at the wrong point.
368102cdbd7SLarsGit223     *
369bb01c27cSAndreas Gohr     * Example:
3708c253612SGerrit Uitslag     *   cc("föö <foo@bar.com>, me@somewhere.com","TBcc");
371102cdbd7SLarsGit223     *   to("foo, Dr." <foo@bar.com>, me@somewhere.com");
372bb01c27cSAndreas Gohr     *
37342ea7f44SGerrit Uitslag     * @param string|string[]  $addresses Multiple adresses separated by commas or as array
37442ea7f44SGerrit Uitslag     * @return false|string  the prepared header (can contain multiple lines)
375bb01c27cSAndreas Gohr     */
376*d868eb89SAndreas Gohr    public function cleanAddress($addresses)
377*d868eb89SAndreas Gohr    {
378bb01c27cSAndreas Gohr        $headers = '';
379b6c97c70SAndreas Gohr        if(!is_array($addresses)){
380d31a1599SLarsGit223            $count = preg_match_all('/\s*(?:("[^"]*"[^,]+),*)|([^,]+)\s*,*/', $addresses, $matches, PREG_SET_ORDER);
38124870174SAndreas Gohr            $addresses = [];
382743792d0SLarsGit223            if ($count !== false && is_array($matches)) {
383102cdbd7SLarsGit223                foreach ($matches as $match) {
38424870174SAndreas Gohr                    $addresses[] = rtrim($match[0], ',');
385102cdbd7SLarsGit223                }
386b6c97c70SAndreas Gohr            }
387b6c97c70SAndreas Gohr        }
388b6c97c70SAndreas Gohr
389b6c97c70SAndreas Gohr        foreach($addresses as $part) {
390b6c97c70SAndreas Gohr            $part = preg_replace('/[\r\n\0]+/', ' ', $part); // remove attack vectors
391bb01c27cSAndreas Gohr            $part = trim($part);
392bb01c27cSAndreas Gohr
393bb01c27cSAndreas Gohr            // parse address
394bb01c27cSAndreas Gohr            if(preg_match('#(.*?)<(.*?)>#', $part, $matches)) {
395bb01c27cSAndreas Gohr                $text = trim($matches[1]);
396bb01c27cSAndreas Gohr                $addr = $matches[2];
397bb01c27cSAndreas Gohr            } else {
39810da1f74SAndreas Gohr                $text = '';
399bb01c27cSAndreas Gohr                $addr = $part;
400bb01c27cSAndreas Gohr            }
401bb01c27cSAndreas Gohr            // skip empty ones
402bb01c27cSAndreas Gohr            if(empty($addr)) {
403bb01c27cSAndreas Gohr                continue;
404bb01c27cSAndreas Gohr            }
405bb01c27cSAndreas Gohr
406bb01c27cSAndreas Gohr            // FIXME: is there a way to encode the localpart of a emailaddress?
40724870174SAndreas Gohr            if(!Clean::isASCII($addr)) {
4084772cf38SAndreas Gohr                msg(hsc("E-Mail address <$addr> is not ASCII"), -1, __LINE__, __FILE__, MSG_ADMINS_ONLY);
409bb01c27cSAndreas Gohr                continue;
410bb01c27cSAndreas Gohr            }
411bb01c27cSAndreas Gohr
41264d23c16SAndreas Gohr            if(!mail_isvalid($addr)) {
4134772cf38SAndreas Gohr                msg(hsc("E-Mail address <$addr> is not valid"), -1, __LINE__, __FILE__, MSG_ADMINS_ONLY);
414bb01c27cSAndreas Gohr                continue;
415bb01c27cSAndreas Gohr            }
416bb01c27cSAndreas Gohr
417bb01c27cSAndreas Gohr            // text was given
41830085ef3SYurii K            if(!empty($text) && !isWindows()) { // No named recipients for To: in Windows (see FS#652)
419bb01c27cSAndreas Gohr                // add address quotes
420bb01c27cSAndreas Gohr                $addr = "<$addr>";
421bb01c27cSAndreas Gohr
422bb01c27cSAndreas Gohr                if(defined('MAILHEADER_ASCIIONLY')) {
42324870174SAndreas Gohr                    $text = Clean::deaccent($text);
42424870174SAndreas Gohr                    $text = Clean::strip($text);
425bb01c27cSAndreas Gohr                }
426bb01c27cSAndreas Gohr
42724870174SAndreas Gohr                if(strpos($text, ',') !== false || !Clean::isASCII($text)) {
428bb01c27cSAndreas Gohr                    $text = '=?UTF-8?B?'.base64_encode($text).'?=';
429bb01c27cSAndreas Gohr                }
430bb01c27cSAndreas Gohr            } else {
431bb01c27cSAndreas Gohr                $text = '';
432bb01c27cSAndreas Gohr            }
433bb01c27cSAndreas Gohr
434bb01c27cSAndreas Gohr            // add to header comma seperated
435bb01c27cSAndreas Gohr            if($headers != '') {
436bb01c27cSAndreas Gohr                $headers .= ', ';
437bb01c27cSAndreas Gohr            }
438bb01c27cSAndreas Gohr            $headers .= $text.' '.$addr;
439bb01c27cSAndreas Gohr        }
440bb01c27cSAndreas Gohr
441b6c97c70SAndreas Gohr        $headers = trim($headers);
442bb01c27cSAndreas Gohr        if(empty($headers)) return false;
443bb01c27cSAndreas Gohr
444bb01c27cSAndreas Gohr        return $headers;
445bb01c27cSAndreas Gohr    }
446bb01c27cSAndreas Gohr
447bb01c27cSAndreas Gohr
448bb01c27cSAndreas Gohr    /**
449bb01c27cSAndreas Gohr     * Prepare the mime multiparts for all attachments
450bb01c27cSAndreas Gohr     *
451bb01c27cSAndreas Gohr     * Replaces placeholders in the HTML with the correct CIDs
45242ea7f44SGerrit Uitslag     *
45342ea7f44SGerrit Uitslag     * @return string mime multiparts
454bb01c27cSAndreas Gohr     */
455*d868eb89SAndreas Gohr    protected function prepareAttachments()
456*d868eb89SAndreas Gohr    {
457bb01c27cSAndreas Gohr        $mime = '';
458bb01c27cSAndreas Gohr        $part = 1;
459bb01c27cSAndreas Gohr        // embedded attachments
460bb01c27cSAndreas Gohr        foreach($this->attach as $media) {
461ce9d2cc8SAndreas Gohr            $media['name'] = str_replace(':', '_', cleanID($media['name'], true));
462ce9d2cc8SAndreas Gohr
463bb01c27cSAndreas Gohr            // create content id
464bb01c27cSAndreas Gohr            $cid = 'part'.$part.'.'.$this->partid;
465bb01c27cSAndreas Gohr
466bb01c27cSAndreas Gohr            // replace wildcards
467bb01c27cSAndreas Gohr            if($media['embed']) {
468bb01c27cSAndreas Gohr                $this->html = str_replace('%%'.$media['embed'].'%%', 'cid:'.$cid, $this->html);
469bb01c27cSAndreas Gohr            }
470bb01c27cSAndreas Gohr
471bb01c27cSAndreas Gohr            $mime .= '--'.$this->boundary.MAILHEADER_EOL;
4721d8036c2SAndreas Gohr            $mime .= $this->wrappedHeaderLine('Content-Type', $media['mime'].'; id="'.$cid.'"');
4731d8036c2SAndreas Gohr            $mime .= $this->wrappedHeaderLine('Content-Transfer-Encoding', 'base64');
4741d8036c2SAndreas Gohr            $mime .= $this->wrappedHeaderLine('Content-ID', "<$cid>");
475bb01c27cSAndreas Gohr            if($media['embed']) {
4761d8036c2SAndreas Gohr                $mime .= $this->wrappedHeaderLine('Content-Disposition', 'inline; filename='.$media['name']);
477bb01c27cSAndreas Gohr            } else {
4781d8036c2SAndreas Gohr                $mime .= $this->wrappedHeaderLine('Content-Disposition', 'attachment; filename='.$media['name']);
479bb01c27cSAndreas Gohr            }
480bb01c27cSAndreas Gohr            $mime .= MAILHEADER_EOL; //end of headers
481bb01c27cSAndreas Gohr            $mime .= chunk_split(base64_encode($media['data']), 74, MAILHEADER_EOL);
482bb01c27cSAndreas Gohr
483bb01c27cSAndreas Gohr            $part++;
484bb01c27cSAndreas Gohr        }
485bb01c27cSAndreas Gohr        return $mime;
486bb01c27cSAndreas Gohr    }
487bb01c27cSAndreas Gohr
4881d045709SAndreas Gohr    /**
4891d045709SAndreas Gohr     * Build the body and handles multi part mails
4901d045709SAndreas Gohr     *
4911d045709SAndreas Gohr     * Needs to be called before prepareHeaders!
4921d045709SAndreas Gohr     *
4931d045709SAndreas Gohr     * @return string the prepared mail body, false on errors
4941d045709SAndreas Gohr     */
495*d868eb89SAndreas Gohr    protected function prepareBody()
496*d868eb89SAndreas Gohr    {
4971d045709SAndreas Gohr
4982398a2b5SAndreas Gohr        // no HTML mails allowed? remove HTML body
4992398a2b5SAndreas Gohr        if(!$this->allowhtml) {
5002398a2b5SAndreas Gohr            $this->html = '';
5012398a2b5SAndreas Gohr        }
5022398a2b5SAndreas Gohr
503bb01c27cSAndreas Gohr        // check for body
504bb01c27cSAndreas Gohr        if(!$this->text && !$this->html) {
505bb01c27cSAndreas Gohr            return false;
506bb01c27cSAndreas Gohr        }
507bb01c27cSAndreas Gohr
508bb01c27cSAndreas Gohr        // add general headers
509bb01c27cSAndreas Gohr        $this->headers['MIME-Version'] = '1.0';
510bb01c27cSAndreas Gohr
5111d045709SAndreas Gohr        $body = '';
5121d045709SAndreas Gohr
513bb01c27cSAndreas Gohr        if(!$this->html && !count($this->attach)) { // we can send a simple single part message
514bb01c27cSAndreas Gohr            $this->headers['Content-Type']              = 'text/plain; charset=UTF-8';
515bb01c27cSAndreas Gohr            $this->headers['Content-Transfer-Encoding'] = 'base64';
516be3cc6abSAndreas Gohr            $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL);
517bb01c27cSAndreas Gohr        } else { // multi part it is
5181d045709SAndreas Gohr            $body .= "This is a multi-part message in MIME format.".MAILHEADER_EOL;
519bb01c27cSAndreas Gohr
520bb01c27cSAndreas Gohr            // prepare the attachments
521bb01c27cSAndreas Gohr            $attachments = $this->prepareAttachments();
522bb01c27cSAndreas Gohr
523bb01c27cSAndreas Gohr            // do we have alternative text content?
524bb01c27cSAndreas Gohr            if($this->text && $this->html) {
525a36fc348SAndreas Gohr                $this->headers['Content-Type'] = 'multipart/alternative;'.MAILHEADER_EOL.
526a36fc348SAndreas Gohr                    '  boundary="'.$this->boundary.'XX"';
527bb01c27cSAndreas Gohr                $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL;
5281d045709SAndreas Gohr                $body .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL;
5291d045709SAndreas Gohr                $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL;
530bb01c27cSAndreas Gohr                $body .= MAILHEADER_EOL;
531be3cc6abSAndreas Gohr                $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL);
532bb01c27cSAndreas Gohr                $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL;
533a36fc348SAndreas Gohr                $body .= 'Content-Type: multipart/related;'.MAILHEADER_EOL.
534d6e04b60SAndreas Gohr                    '  boundary="'.$this->boundary.'";'.MAILHEADER_EOL.
535d6e04b60SAndreas Gohr                    '  type="text/html"'.MAILHEADER_EOL;
536bb01c27cSAndreas Gohr                $body .= MAILHEADER_EOL;
537bb01c27cSAndreas Gohr            }
538bb01c27cSAndreas Gohr
5391d045709SAndreas Gohr            $body .= '--'.$this->boundary.MAILHEADER_EOL;
5401d045709SAndreas Gohr            $body .= 'Content-Type: text/html; charset=UTF-8'.MAILHEADER_EOL;
5411d045709SAndreas Gohr            $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL;
542bb01c27cSAndreas Gohr            $body .= MAILHEADER_EOL;
543be3cc6abSAndreas Gohr            $body .= chunk_split(base64_encode($this->html), 72, MAILHEADER_EOL);
544bb01c27cSAndreas Gohr            $body .= MAILHEADER_EOL;
545bb01c27cSAndreas Gohr            $body .= $attachments;
546bb01c27cSAndreas Gohr            $body .= '--'.$this->boundary.'--'.MAILHEADER_EOL;
547bb01c27cSAndreas Gohr
548bb01c27cSAndreas Gohr            // close open multipart/alternative boundary
549bb01c27cSAndreas Gohr            if($this->text && $this->html) {
550bb01c27cSAndreas Gohr                $body .= '--'.$this->boundary.'XX--'.MAILHEADER_EOL;
551bb01c27cSAndreas Gohr            }
552bb01c27cSAndreas Gohr        }
553bb01c27cSAndreas Gohr
554bb01c27cSAndreas Gohr        return $body;
555bb01c27cSAndreas Gohr    }
556bb01c27cSAndreas Gohr
557bb01c27cSAndreas Gohr    /**
558a36fc348SAndreas Gohr     * Cleanup and encode the headers array
559a36fc348SAndreas Gohr     */
560*d868eb89SAndreas Gohr    protected function cleanHeaders()
561*d868eb89SAndreas Gohr    {
562a36fc348SAndreas Gohr        global $conf;
563a36fc348SAndreas Gohr
564a36fc348SAndreas Gohr        // clean up addresses
565a36fc348SAndreas Gohr        if(empty($this->headers['From'])) $this->from($conf['mailfrom']);
56624870174SAndreas Gohr        $addrs = ['To', 'From', 'Cc', 'Bcc', 'Reply-To', 'Sender'];
567a36fc348SAndreas Gohr        foreach($addrs as $addr) {
568a36fc348SAndreas Gohr            if(isset($this->headers[$addr])) {
569a36fc348SAndreas Gohr                $this->headers[$addr] = $this->cleanAddress($this->headers[$addr]);
570a36fc348SAndreas Gohr            }
571a36fc348SAndreas Gohr        }
572a36fc348SAndreas Gohr
57345992a63SAndreas Gohr        if(isset($this->headers['Subject'])) {
574a36fc348SAndreas Gohr            // add prefix to subject
57554f30755SAndreas Gohr            if(empty($conf['mailprefix'])) {
57624870174SAndreas Gohr                if(PhpString::strlen($conf['title']) < 20) {
57754f30755SAndreas Gohr                    $prefix = '['.$conf['title'].']';
57854f30755SAndreas Gohr                } else {
57924870174SAndreas Gohr                    $prefix = '['.PhpString::substr($conf['title'], 0, 20).'...]';
5808a215f09SAndreas Gohr                }
5818a215f09SAndreas Gohr            } else {
582a36fc348SAndreas Gohr                $prefix = '['.$conf['mailprefix'].']';
58354f30755SAndreas Gohr            }
584a36fc348SAndreas Gohr            $len = strlen($prefix);
58524870174SAndreas Gohr            if(substr($this->headers['Subject'], 0, $len) !== $prefix) {
58645992a63SAndreas Gohr                $this->headers['Subject'] = $prefix.' '.$this->headers['Subject'];
587a36fc348SAndreas Gohr            }
588a36fc348SAndreas Gohr
589a36fc348SAndreas Gohr            // encode subject
590a36fc348SAndreas Gohr            if(defined('MAILHEADER_ASCIIONLY')) {
59124870174SAndreas Gohr                $this->headers['Subject'] = Clean::deaccent($this->headers['Subject']);
59224870174SAndreas Gohr                $this->headers['Subject'] = Clean::strip($this->headers['Subject']);
593a36fc348SAndreas Gohr            }
59424870174SAndreas Gohr            if(!Clean::isASCII($this->headers['Subject'])) {
59545992a63SAndreas Gohr                $this->headers['Subject'] = '=?UTF-8?B?'.base64_encode($this->headers['Subject']).'?=';
596a36fc348SAndreas Gohr            }
597a36fc348SAndreas Gohr        }
598a36fc348SAndreas Gohr
599a36fc348SAndreas Gohr    }
6001d8036c2SAndreas Gohr
6011d8036c2SAndreas Gohr    /**
6021d8036c2SAndreas Gohr     * Returns a complete, EOL terminated header line, wraps it if necessary
6031d8036c2SAndreas Gohr     *
60442ea7f44SGerrit Uitslag     * @param string $key
60542ea7f44SGerrit Uitslag     * @param string $val
60642ea7f44SGerrit Uitslag     * @return string line
6071d8036c2SAndreas Gohr     */
608*d868eb89SAndreas Gohr    protected function wrappedHeaderLine($key, $val)
609*d868eb89SAndreas Gohr    {
6101d8036c2SAndreas Gohr        return wordwrap("$key: $val", 78, MAILHEADER_EOL.'  ').MAILHEADER_EOL;
611a36fc348SAndreas Gohr    }
612a36fc348SAndreas Gohr
613a36fc348SAndreas Gohr    /**
614bb01c27cSAndreas Gohr     * Create a string from the headers array
6151d045709SAndreas Gohr     *
6161d045709SAndreas Gohr     * @returns string the headers
617bb01c27cSAndreas Gohr     */
618*d868eb89SAndreas Gohr    protected function prepareHeaders()
619*d868eb89SAndreas Gohr    {
620bb01c27cSAndreas Gohr        $headers = '';
621bb01c27cSAndreas Gohr        foreach($this->headers as $key => $val) {
622749c0023SAndreas Gohr            if ($val === '' || $val === null) continue;
6231d8036c2SAndreas Gohr            $headers .= $this->wrappedHeaderLine($key, $val);
624bb01c27cSAndreas Gohr        }
625bb01c27cSAndreas Gohr        return $headers;
626bb01c27cSAndreas Gohr    }
627bb01c27cSAndreas Gohr
628bb01c27cSAndreas Gohr    /**
629bb01c27cSAndreas Gohr     * return a full email with all headers
630bb01c27cSAndreas Gohr     *
6311d045709SAndreas Gohr     * This is mainly intended for debugging and testing but could also be
6321d045709SAndreas Gohr     * used for MHT exports
6331d045709SAndreas Gohr     *
6341d045709SAndreas Gohr     * @return string the mail, false on errors
635bb01c27cSAndreas Gohr     */
636*d868eb89SAndreas Gohr    public function dump()
637*d868eb89SAndreas Gohr    {
638a36fc348SAndreas Gohr        $this->cleanHeaders();
639bb01c27cSAndreas Gohr        $body = $this->prepareBody();
6404d18e936SAndreas Gohr        if($body === false) return false;
6411d045709SAndreas Gohr        $headers = $this->prepareHeaders();
642bb01c27cSAndreas Gohr
643bb01c27cSAndreas Gohr        return $headers.MAILHEADER_EOL.$body;
644bb01c27cSAndreas Gohr    }
6451d045709SAndreas Gohr
6461d045709SAndreas Gohr    /**
6479ea45836SChristopher Smith     * Prepare default token replacement strings
6489ea45836SChristopher Smith     *
6499ea45836SChristopher Smith     * Populates the '$replacements' property.
6509ea45836SChristopher Smith     * Should be called by the class constructor
6519ea45836SChristopher Smith     */
652*d868eb89SAndreas Gohr    protected function prepareTokenReplacements()
653*d868eb89SAndreas Gohr    {
6549ea45836SChristopher Smith        global $INFO;
6559ea45836SChristopher Smith        global $conf;
6569ea45836SChristopher Smith        /* @var Input $INPUT */
6579ea45836SChristopher Smith        global $INPUT;
6589ea45836SChristopher Smith        global $lang;
6599ea45836SChristopher Smith
6609ea45836SChristopher Smith        $ip   = clientIP();
6619ea45836SChristopher Smith        $cip  = gethostsbyaddrs($ip);
6628fa268b3SAndreas Gohr        $name = $INFO['userinfo']['name'] ?? '';
6638fa268b3SAndreas Gohr        $mail = $INFO['userinfo']['mail'] ?? '';
6649ea45836SChristopher Smith
66524870174SAndreas Gohr        $this->replacements['text'] = [
6669ea45836SChristopher Smith            'DATE' => dformat(),
6679ea45836SChristopher Smith            'BROWSER' => $INPUT->server->str('HTTP_USER_AGENT'),
6689ea45836SChristopher Smith            'IPADDRESS' => $ip,
6699ea45836SChristopher Smith            'HOSTNAME' => $cip,
6709ea45836SChristopher Smith            'TITLE' => $conf['title'],
6719ea45836SChristopher Smith            'DOKUWIKIURL' => DOKU_URL,
6729ea45836SChristopher Smith            'USER' => $INPUT->server->str('REMOTE_USER'),
67368491db9SPhy            'NAME' => $name,
67468491db9SPhy            'MAIL' => $mail
67524870174SAndreas Gohr        ];
67624870174SAndreas Gohr
67764159a61SAndreas Gohr        $signature = str_replace(
67864159a61SAndreas Gohr            '@DOKUWIKIURL@',
67964159a61SAndreas Gohr            $this->replacements['text']['DOKUWIKIURL'],
68064159a61SAndreas Gohr            $lang['email_signature_text']
68164159a61SAndreas Gohr        );
682774514c9SGerrit Uitslag        $this->replacements['text']['EMAILSIGNATURE'] = "\n-- \n" . $signature . "\n";
6839ea45836SChristopher Smith
68424870174SAndreas Gohr        $this->replacements['html'] = [
6859ea45836SChristopher Smith            'DATE' => '<i>' . hsc(dformat()) . '</i>',
6869ea45836SChristopher Smith            'BROWSER' => hsc($INPUT->server->str('HTTP_USER_AGENT')),
6879ea45836SChristopher Smith            'IPADDRESS' => '<code>' . hsc($ip) . '</code>',
6889ea45836SChristopher Smith            'HOSTNAME' => '<code>' . hsc($cip) . '</code>',
6899ea45836SChristopher Smith            'TITLE' => hsc($conf['title']),
6909ea45836SChristopher Smith            'DOKUWIKIURL' => '<a href="' . DOKU_URL . '">' . DOKU_URL . '</a>',
6919ea45836SChristopher Smith            'USER' => hsc($INPUT->server->str('REMOTE_USER')),
69268491db9SPhy            'NAME' => hsc($name),
69324870174SAndreas Gohr            'MAIL' => '<a href="mailto:"' . hsc($mail) . '">' . hsc($mail) . '</a>'
69424870174SAndreas Gohr        ];
695774514c9SGerrit Uitslag        $signature = $lang['email_signature_text'];
696774514c9SGerrit Uitslag        if(!empty($lang['email_signature_html'])) {
697774514c9SGerrit Uitslag            $signature = $lang['email_signature_html'];
698774514c9SGerrit Uitslag        }
699774514c9SGerrit Uitslag        $signature = str_replace(
70024870174SAndreas Gohr            ['@DOKUWIKIURL@', "\n"],
70124870174SAndreas Gohr            [$this->replacements['html']['DOKUWIKIURL'], '<br />'],
702774514c9SGerrit Uitslag            $signature
703774514c9SGerrit Uitslag        );
704774514c9SGerrit Uitslag        $this->replacements['html']['EMAILSIGNATURE'] = $signature;
7059ea45836SChristopher Smith    }
7069ea45836SChristopher Smith
7079ea45836SChristopher Smith    /**
7081d045709SAndreas Gohr     * Send the mail
7091d045709SAndreas Gohr     *
7101d045709SAndreas Gohr     * Call this after all data was set
7111d045709SAndreas Gohr     *
71228d2ad80SAndreas Gohr     * @triggers MAIL_MESSAGE_SEND
7131d045709SAndreas Gohr     * @return bool true if the mail was successfully passed to the MTA
7141d045709SAndreas Gohr     */
715*d868eb89SAndreas Gohr    public function send()
716*d868eb89SAndreas Gohr    {
7173f6872b1SMyron Turner        global $lang;
71828d2ad80SAndreas Gohr        $success = false;
719a36fc348SAndreas Gohr
72028d2ad80SAndreas Gohr        // prepare hook data
72124870174SAndreas Gohr        $data = [
72228d2ad80SAndreas Gohr            // pass the whole mail class to plugin
72328d2ad80SAndreas Gohr            'mail'    => $this,
72428d2ad80SAndreas Gohr            // pass references for backward compatibility
72528d2ad80SAndreas Gohr            'to'      => &$this->headers['To'],
72628d2ad80SAndreas Gohr            'cc'      => &$this->headers['Cc'],
72728d2ad80SAndreas Gohr            'bcc'     => &$this->headers['Bcc'],
72828d2ad80SAndreas Gohr            'from'    => &$this->headers['From'],
72928d2ad80SAndreas Gohr            'subject' => &$this->headers['Subject'],
73028d2ad80SAndreas Gohr            'body'    => &$this->text,
731a89c75afSAndreas Gohr            'params'  => &$this->sendparam,
73228d2ad80SAndreas Gohr            'headers' => '', // plugins shouldn't use this
73328d2ad80SAndreas Gohr            // signal if we mailed successfully to AFTER event
73428d2ad80SAndreas Gohr            'success' => &$success,
73524870174SAndreas Gohr        ];
73628d2ad80SAndreas Gohr
73728d2ad80SAndreas Gohr        // do our thing if BEFORE hook approves
738e1d9dcc8SAndreas Gohr        $evt = new Event('MAIL_MESSAGE_SEND', $data);
73928d2ad80SAndreas Gohr        if($evt->advise_before(true)) {
74028d2ad80SAndreas Gohr            // clean up before using the headers
741a36fc348SAndreas Gohr            $this->cleanHeaders();
742a36fc348SAndreas Gohr
7431d045709SAndreas Gohr            // any recipients?
7441d045709SAndreas Gohr            if(trim($this->headers['To']) === '' &&
7451d045709SAndreas Gohr                trim($this->headers['Cc']) === '' &&
746a89c75afSAndreas Gohr                trim($this->headers['Bcc']) === ''
747a89c75afSAndreas Gohr            ) return false;
7481d045709SAndreas Gohr
7491d045709SAndreas Gohr            // The To: header is special
7506be717dbSMichael Hamann            if(array_key_exists('To', $this->headers)) {
7516be717dbSMichael Hamann                $to = (string)$this->headers['To'];
7521d045709SAndreas Gohr                unset($this->headers['To']);
7531d045709SAndreas Gohr            } else {
7541d045709SAndreas Gohr                $to = '';
7551d045709SAndreas Gohr            }
7561d045709SAndreas Gohr
7571d045709SAndreas Gohr            // so is the subject
7586be717dbSMichael Hamann            if(array_key_exists('Subject', $this->headers)) {
7596be717dbSMichael Hamann                $subject = (string)$this->headers['Subject'];
7601d045709SAndreas Gohr                unset($this->headers['Subject']);
7611d045709SAndreas Gohr            } else {
7621d045709SAndreas Gohr                $subject = '';
7631d045709SAndreas Gohr            }
7641d045709SAndreas Gohr
7651d045709SAndreas Gohr            // make the body
7661d045709SAndreas Gohr            $body = $this->prepareBody();
7674c89a7f6SAndreas Gohr            if($body === false) return false;
7681d045709SAndreas Gohr
7691d045709SAndreas Gohr            // cook the headers
7701d045709SAndreas Gohr            $headers = $this->prepareHeaders();
77128d2ad80SAndreas Gohr            // add any headers set by legacy plugins
77228d2ad80SAndreas Gohr            if(trim($data['headers'])) {
77328d2ad80SAndreas Gohr                $headers .= MAILHEADER_EOL.trim($data['headers']);
77428d2ad80SAndreas Gohr            }
7751d045709SAndreas Gohr
7763f6872b1SMyron Turner            if(!function_exists('mail')){
7773f6872b1SMyron Turner                $emsg = $lang['email_fail'] . $subject;
7783f6872b1SMyron Turner                error_log($emsg);
7793f6872b1SMyron Turner                msg(hsc($emsg), -1, __LINE__, __FILE__, MSG_MANAGERS_ONLY);
7803f6872b1SMyron Turner                $evt->advise_after();
7813f6872b1SMyron Turner                return false;
7823f6872b1SMyron Turner            }
7833f6872b1SMyron Turner
7841d045709SAndreas Gohr            // send the thing
785bfa6d256SAndreas Gohr            if($to === '') $to = '(undisclosed-recipients)'; // #1422
786749c0023SAndreas Gohr            if($this->sendparam === null) {
78728d2ad80SAndreas Gohr                $success = @mail($to, $subject, $body, $headers);
7881d045709SAndreas Gohr            } else {
78928d2ad80SAndreas Gohr                $success = @mail($to, $subject, $body, $headers, $this->sendparam);
7901d045709SAndreas Gohr            }
7911d045709SAndreas Gohr        }
79228d2ad80SAndreas Gohr        // any AFTER actions?
79328d2ad80SAndreas Gohr        $evt->advise_after();
79428d2ad80SAndreas Gohr        return $success;
79528d2ad80SAndreas Gohr    }
796bb01c27cSAndreas Gohr}
797