xref: /dokuwiki/inc/Mailer.class.php (revision d4f83172d9533c4d84f450fe22ef630816b21d75)
1bb01c27cSAndreas Gohr<?php
2*d4f83172SAndreas Gohr
3bb01c27cSAndreas Gohr/**
41d045709SAndreas Gohr * A class to build and send multi part mails (with HTML content and embedded
51d045709SAndreas Gohr * attachments). All mails are assumed to be in UTF-8 encoding.
61d045709SAndreas Gohr *
71d045709SAndreas Gohr * Attachments are handled in memory so this shouldn't be used to send huge
81d045709SAndreas Gohr * files, but then again mail shouldn't be used to send huge files either.
91d045709SAndreas Gohr *
10bb01c27cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
11bb01c27cSAndreas Gohr */
12*d4f83172SAndreas Gohr
1324870174SAndreas Gohruse dokuwiki\Utf8\PhpString;
1424870174SAndreas Gohruse dokuwiki\Utf8\Clean;
15e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event;
16e1d9dcc8SAndreas Gohr
17a89c75afSAndreas Gohr/**
18a89c75afSAndreas Gohr * Mail Handling
19a89c75afSAndreas Gohr */
208c7c53b0SAndreas Gohrclass Mailer
218c7c53b0SAndreas Gohr{
2224870174SAndreas Gohr    protected $headers   = [];
2324870174SAndreas Gohr    protected $attach    = [];
24f41c79d7SAndreas Gohr    protected $html      = '';
25f41c79d7SAndreas Gohr    protected $text      = '';
26bb01c27cSAndreas Gohr
27f41c79d7SAndreas Gohr    protected $boundary  = '';
28f41c79d7SAndreas Gohr    protected $partid    = '';
2924870174SAndreas Gohr    protected $sendparam;
30bb01c27cSAndreas Gohr
31f41c79d7SAndreas Gohr    protected $allowhtml = true;
321d045709SAndreas Gohr
3324870174SAndreas Gohr    protected $replacements = ['text' => [], 'html' => []];
349ea45836SChristopher Smith
351d045709SAndreas Gohr    /**
361d045709SAndreas Gohr     * Constructor
371d045709SAndreas Gohr     *
389ea45836SChristopher Smith     * Initializes the boundary strings, part counters and token replacements
391d045709SAndreas Gohr     */
40d868eb89SAndreas Gohr    public function __construct()
41d868eb89SAndreas Gohr    {
429f3eca0bSAndreas Gohr        global $conf;
43585bf44eSChristopher Smith        /* @var Input $INPUT */
44585bf44eSChristopher Smith        global $INPUT;
459f3eca0bSAndreas Gohr
469f3eca0bSAndreas Gohr        $server = parse_url(DOKU_URL, PHP_URL_HOST);
47749c0023SAndreas Gohr        if (strpos($server, '.') === false) $server .= '.localhost';
481d045709SAndreas Gohr
4924870174SAndreas Gohr        $this->partid   = substr(md5(uniqid(random_int(0, mt_getrandmax()), true)), 0, 8) . '@' . $server;
5024870174SAndreas Gohr        $this->boundary = '__________' . md5(uniqid(random_int(0, mt_getrandmax()), true));
519f3eca0bSAndreas Gohr
52749c0023SAndreas Gohr        $listid = implode('.', array_reverse(explode('/', DOKU_BASE))) . $server;
539f3eca0bSAndreas Gohr        $listid = strtolower(trim($listid, '.'));
5424870174SAndreas Gohr
5524870174SAndreas Gohr        $messageid = uniqid(random_int(0, mt_getrandmax()), 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     */
83d868eb89SAndreas Gohr    public function attachFile($path, $mime, $name = '', $embed = '')
84d868eb89SAndreas Gohr    {
85bb01c27cSAndreas Gohr        if (!$name) {
8624870174SAndreas Gohr            $name = PhpString::basename($path);
87bb01c27cSAndreas Gohr        }
88bb01c27cSAndreas Gohr
8924870174SAndreas Gohr        $this->attach[] = [
90bb01c27cSAndreas Gohr            'data'  => file_get_contents($path),
91bb01c27cSAndreas Gohr            'mime'  => $mime,
92bb01c27cSAndreas Gohr            'name'  => $name,
93bb01c27cSAndreas Gohr            'embed' => $embed
9424870174SAndreas Gohr        ];
95bb01c27cSAndreas Gohr    }
96bb01c27cSAndreas Gohr
97bb01c27cSAndreas Gohr    /**
98bb01c27cSAndreas Gohr     * Attach a file
99bb01c27cSAndreas Gohr     *
100a89c75afSAndreas Gohr     * @param string $data  The file contents to attach
101a89c75afSAndreas Gohr     * @param string $mime  Mimetype of the attached file
102a89c75afSAndreas Gohr     * @param string $name  The filename to use
103a89c75afSAndreas Gohr     * @param string $embed Unique key to reference this file from the HTML part
104bb01c27cSAndreas Gohr     */
105d868eb89SAndreas Gohr    public function attachContent($data, $mime, $name = '', $embed = '')
106d868eb89SAndreas Gohr    {
107bb01c27cSAndreas Gohr        if (!$name) {
10824870174SAndreas Gohr            [, $ext] = explode('/', $mime);
109bb01c27cSAndreas Gohr            $name = count($this->attach) . ".$ext";
110bb01c27cSAndreas Gohr        }
111bb01c27cSAndreas Gohr
11224870174SAndreas Gohr        $this->attach[] = [
113bb01c27cSAndreas Gohr            'data'  => $data,
114bb01c27cSAndreas Gohr            'mime'  => $mime,
115bb01c27cSAndreas Gohr            'name'  => $name,
116bb01c27cSAndreas Gohr            'embed' => $embed
11724870174SAndreas Gohr        ];
118bb01c27cSAndreas Gohr    }
119bb01c27cSAndreas Gohr
120bb01c27cSAndreas Gohr    /**
121850dbf1fSAndreas Gohr     * Callback function to automatically embed images referenced in HTML templates
12242ea7f44SGerrit Uitslag     *
12342ea7f44SGerrit Uitslag     * @param array $matches
12442ea7f44SGerrit Uitslag     * @return string placeholder
125850dbf1fSAndreas Gohr     */
126d868eb89SAndreas Gohr    protected function autoEmbedCallBack($matches)
127d868eb89SAndreas Gohr    {
128850dbf1fSAndreas Gohr        static $embeds = 0;
129850dbf1fSAndreas Gohr        $embeds++;
130850dbf1fSAndreas Gohr
131850dbf1fSAndreas Gohr        // get file and mime type
132850dbf1fSAndreas Gohr        $media = cleanID($matches[1]);
13324870174SAndreas Gohr        [, $mime] = mimetype($media);
134850dbf1fSAndreas Gohr        $file = mediaFN($media);
135850dbf1fSAndreas Gohr        if (!file_exists($file)) return $matches[0]; //bad reference, keep as is
136850dbf1fSAndreas Gohr
137850dbf1fSAndreas Gohr        // attach it and set placeholder
138850dbf1fSAndreas Gohr        $this->attachFile($file, $mime, '', 'autoembed' . $embeds);
139850dbf1fSAndreas Gohr        return '%%autoembed' . $embeds . '%%';
140850dbf1fSAndreas Gohr    }
141850dbf1fSAndreas Gohr
142850dbf1fSAndreas Gohr    /**
1431d045709SAndreas Gohr     * Add an arbitrary header to the mail
1441d045709SAndreas Gohr     *
145a36fc348SAndreas Gohr     * If an empy value is passed, the header is removed
146a36fc348SAndreas Gohr     *
1471d045709SAndreas Gohr     * @param string $header the header name (no trailing colon!)
14859bc3b48SGerrit Uitslag     * @param string|string[] $value  the value of the header
1491d045709SAndreas Gohr     * @param bool   $clean  remove all non-ASCII chars and line feeds?
1501d045709SAndreas Gohr     */
151d868eb89SAndreas Gohr    public function setHeader($header, $value, $clean = true)
152d868eb89SAndreas Gohr    {
1539f3eca0bSAndreas Gohr        $header = str_replace(' ', '-', ucwords(strtolower(str_replace('-', ' ', $header)))); // streamline casing
1541d045709SAndreas Gohr        if ($clean) {
155578b2c23SAndreas Gohr            $header = preg_replace('/[^a-zA-Z0-9_ \-\.\+\@]+/', '', $header);
156578b2c23SAndreas Gohr            $value  = preg_replace('/[^a-zA-Z0-9_ \-\.\+\@<>]+/', '', $value);
1571d045709SAndreas Gohr        }
158a36fc348SAndreas Gohr
159a36fc348SAndreas Gohr        // empty value deletes
160b6c97c70SAndreas Gohr        if (is_array($value)) {
161b6c97c70SAndreas Gohr            $value = array_map('trim', $value);
162b6c97c70SAndreas Gohr            $value = array_filter($value);
163b6c97c70SAndreas Gohr            if (!$value) $value = '';
164b6c97c70SAndreas Gohr        } else {
165a36fc348SAndreas Gohr            $value = trim($value);
166b6c97c70SAndreas Gohr        }
167a36fc348SAndreas Gohr        if ($value === '') {
168a36fc348SAndreas Gohr            if (isset($this->headers[$header])) unset($this->headers[$header]);
169a36fc348SAndreas Gohr        } else {
1701d045709SAndreas Gohr            $this->headers[$header] = $value;
1711d045709SAndreas Gohr        }
172a36fc348SAndreas Gohr    }
1731d045709SAndreas Gohr
1741d045709SAndreas Gohr    /**
1751d045709SAndreas Gohr     * Set additional parameters to be passed to sendmail
1761d045709SAndreas Gohr     *
1771d045709SAndreas Gohr     * Whatever is set here is directly passed to PHP's mail() command as last
1781d045709SAndreas Gohr     * parameter. Depending on the PHP setup this might break mailing alltogether
17942ea7f44SGerrit Uitslag     *
18042ea7f44SGerrit Uitslag     * @param string $param
1811d045709SAndreas Gohr     */
182d868eb89SAndreas Gohr    public function setParameters($param)
183d868eb89SAndreas Gohr    {
1841d045709SAndreas Gohr        $this->sendparam = $param;
1851d045709SAndreas Gohr    }
1861d045709SAndreas Gohr
1871d045709SAndreas Gohr    /**
188abbf0890SAndreas Gohr     * Set the text and HTML body and apply replacements
189abbf0890SAndreas Gohr     *
190abbf0890SAndreas Gohr     * This function applies a whole bunch of default replacements in addition
19104dcb5b2SChristopher Smith     * to the ones specified as parameters
192abbf0890SAndreas Gohr     *
193abbf0890SAndreas Gohr     * If you pass the HTML part or HTML replacements yourself you have to make
194abbf0890SAndreas Gohr     * sure you encode all HTML special chars correctly
195abbf0890SAndreas Gohr     *
196abbf0890SAndreas Gohr     * @param string $text     plain text body
197abbf0890SAndreas Gohr     * @param array  $textrep  replacements to apply on the text part
19864159a61SAndreas Gohr     * @param array  $htmlrep  replacements to apply on the HTML part, null to use $textrep (urls wrapped in <a> tags)
19959bc3b48SGerrit Uitslag     * @param string $html     the HTML body, leave null to create it from $text
200f08086ecSAndreas Gohr     * @param bool   $wrap     wrap the HTML in the default header/Footer
201abbf0890SAndreas Gohr     */
202d868eb89SAndreas Gohr    public function setBody($text, $textrep = null, $htmlrep = null, $html = null, $wrap = true)
203d868eb89SAndreas Gohr    {
204585bf44eSChristopher Smith
20576efd6d0SAndreas Gohr        $htmlrep = (array)$htmlrep;
20676efd6d0SAndreas Gohr        $textrep = (array)$textrep;
207abbf0890SAndreas Gohr
208abbf0890SAndreas Gohr        // create HTML from text if not given
209749c0023SAndreas Gohr        if ($html === null) {
210ba9c057bSAndreas Gohr            $html = $text;
211ba9c057bSAndreas Gohr            $html = hsc($html);
212ba2c2f17Sfurun            $html = preg_replace('/^----+$/m', '<hr >', $html);
213ba9c057bSAndreas Gohr            $html = nl2br($html);
214abbf0890SAndreas Gohr        }
215f08086ecSAndreas Gohr        if ($wrap) {
216749c0023SAndreas Gohr            $wrapper = rawLocale('mailwrap', 'html');
2173819cafdSfurun            $html = preg_replace('/\n-- <br \/>.*$/s', '', $html); //strip signature
2183819cafdSfurun            $html = str_replace('@EMAILSIGNATURE@', '', $html); //strip @EMAILSIGNATURE@
219749c0023SAndreas Gohr            $html = str_replace('@HTMLBODY@', $html, $wrapper);
220f08086ecSAndreas Gohr        }
221f08086ecSAndreas Gohr
2223819cafdSfurun        if (strpos($text, '@EMAILSIGNATURE@') === false) {
2239ea45836SChristopher Smith            $text .= '@EMAILSIGNATURE@';
2243819cafdSfurun        }
225ba2c2f17Sfurun
22676efd6d0SAndreas Gohr        // copy over all replacements missing for HTML (autolink URLs)
22776efd6d0SAndreas Gohr        foreach ($textrep as $key => $value) {
22876efd6d0SAndreas Gohr            if (isset($htmlrep[$key])) continue;
2293e7e6067SKlap-in            if (media_isexternal($value)) {
23076efd6d0SAndreas Gohr                $htmlrep[$key] = '<a href="' . hsc($value) . '">' . hsc($value) . '</a>';
23176efd6d0SAndreas Gohr            } else {
23276efd6d0SAndreas Gohr                $htmlrep[$key] = hsc($value);
23376efd6d0SAndreas Gohr            }
234abbf0890SAndreas Gohr        }
235abbf0890SAndreas Gohr
236850dbf1fSAndreas Gohr        // embed media from templates
237a89c75afSAndreas Gohr        $html = preg_replace_callback(
238a89c75afSAndreas Gohr            '/@MEDIA\(([^\)]+)\)@/',
239dccd6b2bSAndreas Gohr            [$this, 'autoEmbedCallBack'],
240dccd6b2bSAndreas Gohr            $html
241a89c75afSAndreas Gohr        );
242850dbf1fSAndreas Gohr
2439ea45836SChristopher Smith        // add default token replacements
24424870174SAndreas Gohr        $trep = array_merge($this->replacements['text'], $textrep);
24524870174SAndreas Gohr        $hrep = array_merge($this->replacements['html'], $htmlrep);
246abbf0890SAndreas Gohr
247abbf0890SAndreas Gohr        // Apply replacements
248abbf0890SAndreas Gohr        foreach ($trep as $key => $substitution) {
249abbf0890SAndreas Gohr            $text = str_replace('@' . strtoupper($key) . '@', $substitution, $text);
250abbf0890SAndreas Gohr        }
251abbf0890SAndreas Gohr        foreach ($hrep as $key => $substitution) {
252abbf0890SAndreas Gohr            $html = str_replace('@' . strtoupper($key) . '@', $substitution, $html);
253abbf0890SAndreas Gohr        }
254abbf0890SAndreas Gohr
255abbf0890SAndreas Gohr        $this->setHTML($html);
256abbf0890SAndreas Gohr        $this->setText($text);
257abbf0890SAndreas Gohr    }
258abbf0890SAndreas Gohr
259abbf0890SAndreas Gohr    /**
260bb01c27cSAndreas Gohr     * Set the HTML part of the mail
261bb01c27cSAndreas Gohr     *
262bb01c27cSAndreas Gohr     * Placeholders can be used to reference embedded attachments
263abbf0890SAndreas Gohr     *
264abbf0890SAndreas Gohr     * You probably want to use setBody() instead
26542ea7f44SGerrit Uitslag     *
26642ea7f44SGerrit Uitslag     * @param string $html
267bb01c27cSAndreas Gohr     */
268d868eb89SAndreas Gohr    public function setHTML($html)
269d868eb89SAndreas Gohr    {
270bb01c27cSAndreas Gohr        $this->html = $html;
271bb01c27cSAndreas Gohr    }
272bb01c27cSAndreas Gohr
273bb01c27cSAndreas Gohr    /**
274bb01c27cSAndreas Gohr     * Set the plain text part of the mail
275abbf0890SAndreas Gohr     *
276abbf0890SAndreas Gohr     * You probably want to use setBody() instead
27742ea7f44SGerrit Uitslag     *
27842ea7f44SGerrit Uitslag     * @param string $text
279bb01c27cSAndreas Gohr     */
280d868eb89SAndreas Gohr    public function setText($text)
281d868eb89SAndreas Gohr    {
282bb01c27cSAndreas Gohr        $this->text = $text;
283bb01c27cSAndreas Gohr    }
284bb01c27cSAndreas Gohr
285bb01c27cSAndreas Gohr    /**
286a36fc348SAndreas Gohr     * Add the To: recipients
287a36fc348SAndreas Gohr     *
2888c253612SGerrit Uitslag     * @see cleanAddress
28959bc3b48SGerrit Uitslag     * @param string|string[]  $address Multiple adresses separated by commas or as array
290a36fc348SAndreas Gohr     */
291d868eb89SAndreas Gohr    public function to($address)
292d868eb89SAndreas Gohr    {
293a36fc348SAndreas Gohr        $this->setHeader('To', $address, false);
294a36fc348SAndreas Gohr    }
295a36fc348SAndreas Gohr
296a36fc348SAndreas Gohr    /**
297a36fc348SAndreas Gohr     * Add the Cc: recipients
298a36fc348SAndreas Gohr     *
2998c253612SGerrit Uitslag     * @see cleanAddress
30059bc3b48SGerrit Uitslag     * @param string|string[]  $address Multiple adresses separated by commas or as array
301a36fc348SAndreas Gohr     */
302d868eb89SAndreas Gohr    public function cc($address)
303d868eb89SAndreas Gohr    {
304a36fc348SAndreas Gohr        $this->setHeader('Cc', $address, false);
305a36fc348SAndreas Gohr    }
306a36fc348SAndreas Gohr
307a36fc348SAndreas Gohr    /**
308a36fc348SAndreas Gohr     * Add the Bcc: recipients
309a36fc348SAndreas Gohr     *
3108c253612SGerrit Uitslag     * @see cleanAddress
31159bc3b48SGerrit Uitslag     * @param string|string[]  $address Multiple adresses separated by commas or as array
312a36fc348SAndreas Gohr     */
313d868eb89SAndreas Gohr    public function bcc($address)
314d868eb89SAndreas Gohr    {
315a36fc348SAndreas Gohr        $this->setHeader('Bcc', $address, false);
316a36fc348SAndreas Gohr    }
317a36fc348SAndreas Gohr
318a36fc348SAndreas Gohr    /**
319a36fc348SAndreas Gohr     * Add the From: address
320a36fc348SAndreas Gohr     *
321a36fc348SAndreas Gohr     * This is set to $conf['mailfrom'] when not specified so you shouldn't need
322a36fc348SAndreas Gohr     * to call this function
323a36fc348SAndreas Gohr     *
3248c253612SGerrit Uitslag     * @see cleanAddress
325a36fc348SAndreas Gohr     * @param string  $address from address
326a36fc348SAndreas Gohr     */
327d868eb89SAndreas Gohr    public function from($address)
328d868eb89SAndreas Gohr    {
329a36fc348SAndreas Gohr        $this->setHeader('From', $address, false);
330a36fc348SAndreas Gohr    }
331a36fc348SAndreas Gohr
332a36fc348SAndreas Gohr    /**
333a36fc348SAndreas Gohr     * Add the mail's Subject: header
334a36fc348SAndreas Gohr     *
335a36fc348SAndreas Gohr     * @param string $subject the mail subject
336a36fc348SAndreas Gohr     */
337d868eb89SAndreas Gohr    public function subject($subject)
338d868eb89SAndreas Gohr    {
339a36fc348SAndreas Gohr        $this->headers['Subject'] = $subject;
340a36fc348SAndreas Gohr    }
341a36fc348SAndreas Gohr
342a36fc348SAndreas Gohr    /**
343102cdbd7SLarsGit223     * Return a clean name which can be safely used in mail address
344102cdbd7SLarsGit223     * fields. That means the name will be enclosed in '"' if it includes
345102cdbd7SLarsGit223     * a '"' or a ','. Also a '"' will be escaped as '\"'.
346102cdbd7SLarsGit223     *
347102cdbd7SLarsGit223     * @param string $name the name to clean-up
348102cdbd7SLarsGit223     * @see cleanAddress
349102cdbd7SLarsGit223     */
350d868eb89SAndreas Gohr    public function getCleanName($name)
351d868eb89SAndreas Gohr    {
3522b58f049SAndreas Gohr        $name = trim($name, " \t\"");
353102cdbd7SLarsGit223        $name = str_replace('"', '\"', $name, $count);
354102cdbd7SLarsGit223        if ($count > 0 || strpos($name, ',') !== false) {
355102cdbd7SLarsGit223            $name = '"' . $name . '"';
356102cdbd7SLarsGit223        }
357102cdbd7SLarsGit223        return $name;
358102cdbd7SLarsGit223    }
359102cdbd7SLarsGit223
360102cdbd7SLarsGit223    /**
3611d045709SAndreas Gohr     * Sets an email address header with correct encoding
362bb01c27cSAndreas Gohr     *
363bb01c27cSAndreas Gohr     * Unicode characters will be deaccented and encoded base64
364bb01c27cSAndreas Gohr     * for headers. Addresses may not contain Non-ASCII data!
365bb01c27cSAndreas Gohr     *
366102cdbd7SLarsGit223     * If @$addresses is a string then it will be split into multiple
367102cdbd7SLarsGit223     * addresses. Addresses must be separated by a comma. If the display
368102cdbd7SLarsGit223     * name includes a comma then it MUST be properly enclosed by '"' to
369102cdbd7SLarsGit223     * prevent spliting at the wrong point.
370102cdbd7SLarsGit223     *
371bb01c27cSAndreas Gohr     * Example:
3728c253612SGerrit Uitslag     *   cc("föö <foo@bar.com>, me@somewhere.com","TBcc");
373102cdbd7SLarsGit223     *   to("foo, Dr." <foo@bar.com>, me@somewhere.com");
374bb01c27cSAndreas Gohr     *
37542ea7f44SGerrit Uitslag     * @param string|string[]  $addresses Multiple adresses separated by commas or as array
37642ea7f44SGerrit Uitslag     * @return false|string  the prepared header (can contain multiple lines)
377bb01c27cSAndreas Gohr     */
378d868eb89SAndreas Gohr    public function cleanAddress($addresses)
379d868eb89SAndreas Gohr    {
380bb01c27cSAndreas Gohr        $headers = '';
381b6c97c70SAndreas Gohr        if (!is_array($addresses)) {
382d31a1599SLarsGit223            $count = preg_match_all('/\s*(?:("[^"]*"[^,]+),*)|([^,]+)\s*,*/', $addresses, $matches, PREG_SET_ORDER);
38324870174SAndreas Gohr            $addresses = [];
384743792d0SLarsGit223            if ($count !== false && is_array($matches)) {
385102cdbd7SLarsGit223                foreach ($matches as $match) {
38624870174SAndreas Gohr                    $addresses[] = rtrim($match[0], ',');
387102cdbd7SLarsGit223                }
388b6c97c70SAndreas Gohr            }
389b6c97c70SAndreas Gohr        }
390b6c97c70SAndreas Gohr
391b6c97c70SAndreas Gohr        foreach ($addresses as $part) {
392b6c97c70SAndreas Gohr            $part = preg_replace('/[\r\n\0]+/', ' ', $part); // remove attack vectors
393bb01c27cSAndreas Gohr            $part = trim($part);
394bb01c27cSAndreas Gohr
395bb01c27cSAndreas Gohr            // parse address
396bb01c27cSAndreas Gohr            if (preg_match('#(.*?)<(.*?)>#', $part, $matches)) {
397bb01c27cSAndreas Gohr                $text = trim($matches[1]);
398bb01c27cSAndreas Gohr                $addr = $matches[2];
399bb01c27cSAndreas Gohr            } else {
40010da1f74SAndreas Gohr                $text = '';
401bb01c27cSAndreas Gohr                $addr = $part;
402bb01c27cSAndreas Gohr            }
403bb01c27cSAndreas Gohr            // skip empty ones
404bb01c27cSAndreas Gohr            if (empty($addr)) {
405bb01c27cSAndreas Gohr                continue;
406bb01c27cSAndreas Gohr            }
407bb01c27cSAndreas Gohr
408bb01c27cSAndreas Gohr            // FIXME: is there a way to encode the localpart of a emailaddress?
40924870174SAndreas Gohr            if (!Clean::isASCII($addr)) {
4104772cf38SAndreas Gohr                msg(hsc("E-Mail address <$addr> is not ASCII"), -1, __LINE__, __FILE__, MSG_ADMINS_ONLY);
411bb01c27cSAndreas Gohr                continue;
412bb01c27cSAndreas Gohr            }
413bb01c27cSAndreas Gohr
41464d23c16SAndreas Gohr            if (!mail_isvalid($addr)) {
4154772cf38SAndreas Gohr                msg(hsc("E-Mail address <$addr> is not valid"), -1, __LINE__, __FILE__, MSG_ADMINS_ONLY);
416bb01c27cSAndreas Gohr                continue;
417bb01c27cSAndreas Gohr            }
418bb01c27cSAndreas Gohr
419bb01c27cSAndreas Gohr            // text was given
42030085ef3SYurii K            if (!empty($text) && !isWindows()) { // No named recipients for To: in Windows (see FS#652)
421bb01c27cSAndreas Gohr                // add address quotes
422bb01c27cSAndreas Gohr                $addr = "<$addr>";
423bb01c27cSAndreas Gohr
424bb01c27cSAndreas Gohr                if (defined('MAILHEADER_ASCIIONLY')) {
42524870174SAndreas Gohr                    $text = Clean::deaccent($text);
42624870174SAndreas Gohr                    $text = Clean::strip($text);
427bb01c27cSAndreas Gohr                }
428bb01c27cSAndreas Gohr
42924870174SAndreas Gohr                if (strpos($text, ',') !== false || !Clean::isASCII($text)) {
430bb01c27cSAndreas Gohr                    $text = '=?UTF-8?B?' . base64_encode($text) . '?=';
431bb01c27cSAndreas Gohr                }
432bb01c27cSAndreas Gohr            } else {
433bb01c27cSAndreas Gohr                $text = '';
434bb01c27cSAndreas Gohr            }
435bb01c27cSAndreas Gohr
436bb01c27cSAndreas Gohr            // add to header comma seperated
437bb01c27cSAndreas Gohr            if ($headers != '') {
438bb01c27cSAndreas Gohr                $headers .= ', ';
439bb01c27cSAndreas Gohr            }
440bb01c27cSAndreas Gohr            $headers .= $text . ' ' . $addr;
441bb01c27cSAndreas Gohr        }
442bb01c27cSAndreas Gohr
443b6c97c70SAndreas Gohr        $headers = trim($headers);
444bb01c27cSAndreas Gohr        if (empty($headers)) return false;
445bb01c27cSAndreas Gohr
446bb01c27cSAndreas Gohr        return $headers;
447bb01c27cSAndreas Gohr    }
448bb01c27cSAndreas Gohr
449bb01c27cSAndreas Gohr
450bb01c27cSAndreas Gohr    /**
451bb01c27cSAndreas Gohr     * Prepare the mime multiparts for all attachments
452bb01c27cSAndreas Gohr     *
453bb01c27cSAndreas Gohr     * Replaces placeholders in the HTML with the correct CIDs
45442ea7f44SGerrit Uitslag     *
45542ea7f44SGerrit Uitslag     * @return string mime multiparts
456bb01c27cSAndreas Gohr     */
457d868eb89SAndreas Gohr    protected function prepareAttachments()
458d868eb89SAndreas Gohr    {
459bb01c27cSAndreas Gohr        $mime = '';
460bb01c27cSAndreas Gohr        $part = 1;
461bb01c27cSAndreas Gohr        // embedded attachments
462bb01c27cSAndreas Gohr        foreach ($this->attach as $media) {
463ce9d2cc8SAndreas Gohr            $media['name'] = str_replace(':', '_', cleanID($media['name'], true));
464ce9d2cc8SAndreas Gohr
465bb01c27cSAndreas Gohr            // create content id
466bb01c27cSAndreas Gohr            $cid = 'part' . $part . '.' . $this->partid;
467bb01c27cSAndreas Gohr
468bb01c27cSAndreas Gohr            // replace wildcards
469bb01c27cSAndreas Gohr            if ($media['embed']) {
470bb01c27cSAndreas Gohr                $this->html = str_replace('%%' . $media['embed'] . '%%', 'cid:' . $cid, $this->html);
471bb01c27cSAndreas Gohr            }
472bb01c27cSAndreas Gohr
473bb01c27cSAndreas Gohr            $mime .= '--' . $this->boundary . MAILHEADER_EOL;
4741d8036c2SAndreas Gohr            $mime .= $this->wrappedHeaderLine('Content-Type', $media['mime'] . '; id="' . $cid . '"');
4751d8036c2SAndreas Gohr            $mime .= $this->wrappedHeaderLine('Content-Transfer-Encoding', 'base64');
4761d8036c2SAndreas Gohr            $mime .= $this->wrappedHeaderLine('Content-ID', "<$cid>");
477bb01c27cSAndreas Gohr            if ($media['embed']) {
4781d8036c2SAndreas Gohr                $mime .= $this->wrappedHeaderLine('Content-Disposition', 'inline; filename=' . $media['name']);
479bb01c27cSAndreas Gohr            } else {
4801d8036c2SAndreas Gohr                $mime .= $this->wrappedHeaderLine('Content-Disposition', 'attachment; filename=' . $media['name']);
481bb01c27cSAndreas Gohr            }
482bb01c27cSAndreas Gohr            $mime .= MAILHEADER_EOL; //end of headers
483bb01c27cSAndreas Gohr            $mime .= chunk_split(base64_encode($media['data']), 74, MAILHEADER_EOL);
484bb01c27cSAndreas Gohr
485bb01c27cSAndreas Gohr            $part++;
486bb01c27cSAndreas Gohr        }
487bb01c27cSAndreas Gohr        return $mime;
488bb01c27cSAndreas Gohr    }
489bb01c27cSAndreas Gohr
4901d045709SAndreas Gohr    /**
4911d045709SAndreas Gohr     * Build the body and handles multi part mails
4921d045709SAndreas Gohr     *
4931d045709SAndreas Gohr     * Needs to be called before prepareHeaders!
4941d045709SAndreas Gohr     *
4951d045709SAndreas Gohr     * @return string the prepared mail body, false on errors
4961d045709SAndreas Gohr     */
497d868eb89SAndreas Gohr    protected function prepareBody()
498d868eb89SAndreas Gohr    {
4991d045709SAndreas Gohr
5002398a2b5SAndreas Gohr        // no HTML mails allowed? remove HTML body
5012398a2b5SAndreas Gohr        if (!$this->allowhtml) {
5022398a2b5SAndreas Gohr            $this->html = '';
5032398a2b5SAndreas Gohr        }
5042398a2b5SAndreas Gohr
505bb01c27cSAndreas Gohr        // check for body
506bb01c27cSAndreas Gohr        if (!$this->text && !$this->html) {
507bb01c27cSAndreas Gohr            return false;
508bb01c27cSAndreas Gohr        }
509bb01c27cSAndreas Gohr
510bb01c27cSAndreas Gohr        // add general headers
511bb01c27cSAndreas Gohr        $this->headers['MIME-Version'] = '1.0';
512bb01c27cSAndreas Gohr
5131d045709SAndreas Gohr        $body = '';
5141d045709SAndreas Gohr
515bb01c27cSAndreas Gohr        if (!$this->html && !count($this->attach)) { // we can send a simple single part message
516bb01c27cSAndreas Gohr            $this->headers['Content-Type']              = 'text/plain; charset=UTF-8';
517bb01c27cSAndreas Gohr            $this->headers['Content-Transfer-Encoding'] = 'base64';
518be3cc6abSAndreas Gohr            $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL);
519bb01c27cSAndreas Gohr        } else { // multi part it is
5201d045709SAndreas Gohr            $body .= "This is a multi-part message in MIME format." . MAILHEADER_EOL;
521bb01c27cSAndreas Gohr
522bb01c27cSAndreas Gohr            // prepare the attachments
523bb01c27cSAndreas Gohr            $attachments = $this->prepareAttachments();
524bb01c27cSAndreas Gohr
525bb01c27cSAndreas Gohr            // do we have alternative text content?
526bb01c27cSAndreas Gohr            if ($this->text && $this->html) {
527a36fc348SAndreas Gohr                $this->headers['Content-Type'] = 'multipart/alternative;' . MAILHEADER_EOL .
528a36fc348SAndreas Gohr                    '  boundary="' . $this->boundary . 'XX"';
529bb01c27cSAndreas Gohr                $body .= '--' . $this->boundary . 'XX' . MAILHEADER_EOL;
5301d045709SAndreas Gohr                $body .= 'Content-Type: text/plain; charset=UTF-8' . MAILHEADER_EOL;
5311d045709SAndreas Gohr                $body .= 'Content-Transfer-Encoding: base64' . MAILHEADER_EOL;
532bb01c27cSAndreas Gohr                $body .= MAILHEADER_EOL;
533be3cc6abSAndreas Gohr                $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL);
534bb01c27cSAndreas Gohr                $body .= '--' . $this->boundary . 'XX' . MAILHEADER_EOL;
535a36fc348SAndreas Gohr                $body .= 'Content-Type: multipart/related;' . MAILHEADER_EOL .
536d6e04b60SAndreas Gohr                    '  boundary="' . $this->boundary . '";' . MAILHEADER_EOL .
537d6e04b60SAndreas Gohr                    '  type="text/html"' . MAILHEADER_EOL;
538bb01c27cSAndreas Gohr                $body .= MAILHEADER_EOL;
539bb01c27cSAndreas Gohr            }
540bb01c27cSAndreas Gohr
5411d045709SAndreas Gohr            $body .= '--' . $this->boundary . MAILHEADER_EOL;
5421d045709SAndreas Gohr            $body .= 'Content-Type: text/html; charset=UTF-8' . MAILHEADER_EOL;
5431d045709SAndreas Gohr            $body .= 'Content-Transfer-Encoding: base64' . MAILHEADER_EOL;
544bb01c27cSAndreas Gohr            $body .= MAILHEADER_EOL;
545be3cc6abSAndreas Gohr            $body .= chunk_split(base64_encode($this->html), 72, MAILHEADER_EOL);
546bb01c27cSAndreas Gohr            $body .= MAILHEADER_EOL;
547bb01c27cSAndreas Gohr            $body .= $attachments;
548bb01c27cSAndreas Gohr            $body .= '--' . $this->boundary . '--' . MAILHEADER_EOL;
549bb01c27cSAndreas Gohr
550bb01c27cSAndreas Gohr            // close open multipart/alternative boundary
551bb01c27cSAndreas Gohr            if ($this->text && $this->html) {
552bb01c27cSAndreas Gohr                $body .= '--' . $this->boundary . 'XX--' . MAILHEADER_EOL;
553bb01c27cSAndreas Gohr            }
554bb01c27cSAndreas Gohr        }
555bb01c27cSAndreas Gohr
556bb01c27cSAndreas Gohr        return $body;
557bb01c27cSAndreas Gohr    }
558bb01c27cSAndreas Gohr
559bb01c27cSAndreas Gohr    /**
560a36fc348SAndreas Gohr     * Cleanup and encode the headers array
561a36fc348SAndreas Gohr     */
562d868eb89SAndreas Gohr    protected function cleanHeaders()
563d868eb89SAndreas Gohr    {
564a36fc348SAndreas Gohr        global $conf;
565a36fc348SAndreas Gohr
566a36fc348SAndreas Gohr        // clean up addresses
567a36fc348SAndreas Gohr        if (empty($this->headers['From'])) $this->from($conf['mailfrom']);
56824870174SAndreas Gohr        $addrs = ['To', 'From', 'Cc', 'Bcc', 'Reply-To', 'Sender'];
569a36fc348SAndreas Gohr        foreach ($addrs as $addr) {
570a36fc348SAndreas Gohr            if (isset($this->headers[$addr])) {
571a36fc348SAndreas Gohr                $this->headers[$addr] = $this->cleanAddress($this->headers[$addr]);
572a36fc348SAndreas Gohr            }
573a36fc348SAndreas Gohr        }
574a36fc348SAndreas Gohr
57545992a63SAndreas Gohr        if (isset($this->headers['Subject'])) {
576a36fc348SAndreas Gohr            // add prefix to subject
57754f30755SAndreas Gohr            if (empty($conf['mailprefix'])) {
57824870174SAndreas Gohr                if (PhpString::strlen($conf['title']) < 20) {
57954f30755SAndreas Gohr                    $prefix = '[' . $conf['title'] . ']';
58054f30755SAndreas Gohr                } else {
58124870174SAndreas Gohr                    $prefix = '[' . PhpString::substr($conf['title'], 0, 20) . '...]';
5828a215f09SAndreas Gohr                }
5838a215f09SAndreas Gohr            } else {
584a36fc348SAndreas Gohr                $prefix = '[' . $conf['mailprefix'] . ']';
58554f30755SAndreas Gohr            }
586a36fc348SAndreas Gohr            $len = strlen($prefix);
58724870174SAndreas Gohr            if (substr($this->headers['Subject'], 0, $len) !== $prefix) {
58845992a63SAndreas Gohr                $this->headers['Subject'] = $prefix . ' ' . $this->headers['Subject'];
589a36fc348SAndreas Gohr            }
590a36fc348SAndreas Gohr
591a36fc348SAndreas Gohr            // encode subject
592a36fc348SAndreas Gohr            if (defined('MAILHEADER_ASCIIONLY')) {
59324870174SAndreas Gohr                $this->headers['Subject'] = Clean::deaccent($this->headers['Subject']);
59424870174SAndreas Gohr                $this->headers['Subject'] = Clean::strip($this->headers['Subject']);
595a36fc348SAndreas Gohr            }
59624870174SAndreas Gohr            if (!Clean::isASCII($this->headers['Subject'])) {
59745992a63SAndreas Gohr                $this->headers['Subject'] = '=?UTF-8?B?' . base64_encode($this->headers['Subject']) . '?=';
598a36fc348SAndreas Gohr            }
599a36fc348SAndreas Gohr        }
600a36fc348SAndreas Gohr    }
6011d8036c2SAndreas Gohr
6021d8036c2SAndreas Gohr    /**
6031d8036c2SAndreas Gohr     * Returns a complete, EOL terminated header line, wraps it if necessary
6041d8036c2SAndreas Gohr     *
60542ea7f44SGerrit Uitslag     * @param string $key
60642ea7f44SGerrit Uitslag     * @param string $val
60742ea7f44SGerrit Uitslag     * @return string line
6081d8036c2SAndreas Gohr     */
609d868eb89SAndreas Gohr    protected function wrappedHeaderLine($key, $val)
610d868eb89SAndreas Gohr    {
6111d8036c2SAndreas Gohr        return wordwrap("$key: $val", 78, MAILHEADER_EOL . '  ') . MAILHEADER_EOL;
612a36fc348SAndreas Gohr    }
613a36fc348SAndreas Gohr
614a36fc348SAndreas Gohr    /**
615bb01c27cSAndreas Gohr     * Create a string from the headers array
6161d045709SAndreas Gohr     *
6171d045709SAndreas Gohr     * @returns string the headers
618bb01c27cSAndreas Gohr     */
619d868eb89SAndreas Gohr    protected function prepareHeaders()
620d868eb89SAndreas Gohr    {
621bb01c27cSAndreas Gohr        $headers = '';
622bb01c27cSAndreas Gohr        foreach ($this->headers as $key => $val) {
623749c0023SAndreas Gohr            if ($val === '' || $val === null) continue;
6241d8036c2SAndreas Gohr            $headers .= $this->wrappedHeaderLine($key, $val);
625bb01c27cSAndreas Gohr        }
626bb01c27cSAndreas Gohr        return $headers;
627bb01c27cSAndreas Gohr    }
628bb01c27cSAndreas Gohr
629bb01c27cSAndreas Gohr    /**
630bb01c27cSAndreas Gohr     * return a full email with all headers
631bb01c27cSAndreas Gohr     *
6321d045709SAndreas Gohr     * This is mainly intended for debugging and testing but could also be
6331d045709SAndreas Gohr     * used for MHT exports
6341d045709SAndreas Gohr     *
6351d045709SAndreas Gohr     * @return string the mail, false on errors
636bb01c27cSAndreas Gohr     */
637d868eb89SAndreas Gohr    public function dump()
638d868eb89SAndreas Gohr    {
639a36fc348SAndreas Gohr        $this->cleanHeaders();
640bb01c27cSAndreas Gohr        $body = $this->prepareBody();
6414d18e936SAndreas Gohr        if ($body === false) return false;
6421d045709SAndreas Gohr        $headers = $this->prepareHeaders();
643bb01c27cSAndreas Gohr
644bb01c27cSAndreas Gohr        return $headers . MAILHEADER_EOL . $body;
645bb01c27cSAndreas Gohr    }
6461d045709SAndreas Gohr
6471d045709SAndreas Gohr    /**
6489ea45836SChristopher Smith     * Prepare default token replacement strings
6499ea45836SChristopher Smith     *
6509ea45836SChristopher Smith     * Populates the '$replacements' property.
6519ea45836SChristopher Smith     * Should be called by the class constructor
6529ea45836SChristopher Smith     */
653d868eb89SAndreas Gohr    protected function prepareTokenReplacements()
654d868eb89SAndreas Gohr    {
6559ea45836SChristopher Smith        global $INFO;
6569ea45836SChristopher Smith        global $conf;
6579ea45836SChristopher Smith        /* @var Input $INPUT */
6589ea45836SChristopher Smith        global $INPUT;
6599ea45836SChristopher Smith        global $lang;
6609ea45836SChristopher Smith
6619ea45836SChristopher Smith        $ip   = clientIP();
6629ea45836SChristopher Smith        $cip  = gethostsbyaddrs($ip);
6638fa268b3SAndreas Gohr        $name = $INFO['userinfo']['name'] ?? '';
6648fa268b3SAndreas Gohr        $mail = $INFO['userinfo']['mail'] ?? '';
6659ea45836SChristopher Smith
66624870174SAndreas Gohr        $this->replacements['text'] = [
6679ea45836SChristopher Smith            'DATE' => dformat(),
6689ea45836SChristopher Smith            'BROWSER' => $INPUT->server->str('HTTP_USER_AGENT'),
6699ea45836SChristopher Smith            'IPADDRESS' => $ip,
6709ea45836SChristopher Smith            'HOSTNAME' => $cip,
6719ea45836SChristopher Smith            'TITLE' => $conf['title'],
6729ea45836SChristopher Smith            'DOKUWIKIURL' => DOKU_URL,
6739ea45836SChristopher Smith            'USER' => $INPUT->server->str('REMOTE_USER'),
67468491db9SPhy            'NAME' => $name,
67568491db9SPhy            'MAIL' => $mail
67624870174SAndreas Gohr        ];
67724870174SAndreas Gohr
67864159a61SAndreas Gohr        $signature = str_replace(
67964159a61SAndreas Gohr            '@DOKUWIKIURL@',
68064159a61SAndreas Gohr            $this->replacements['text']['DOKUWIKIURL'],
68164159a61SAndreas Gohr            $lang['email_signature_text']
68264159a61SAndreas Gohr        );
683774514c9SGerrit Uitslag        $this->replacements['text']['EMAILSIGNATURE'] = "\n-- \n" . $signature . "\n";
6849ea45836SChristopher Smith
68524870174SAndreas Gohr        $this->replacements['html'] = [
6869ea45836SChristopher Smith            'DATE' => '<i>' . hsc(dformat()) . '</i>',
6879ea45836SChristopher Smith            'BROWSER' => hsc($INPUT->server->str('HTTP_USER_AGENT')),
6889ea45836SChristopher Smith            'IPADDRESS' => '<code>' . hsc($ip) . '</code>',
6899ea45836SChristopher Smith            'HOSTNAME' => '<code>' . hsc($cip) . '</code>',
6909ea45836SChristopher Smith            'TITLE' => hsc($conf['title']),
6919ea45836SChristopher Smith            'DOKUWIKIURL' => '<a href="' . DOKU_URL . '">' . DOKU_URL . '</a>',
6929ea45836SChristopher Smith            'USER' => hsc($INPUT->server->str('REMOTE_USER')),
69368491db9SPhy            'NAME' => hsc($name),
69424870174SAndreas Gohr            'MAIL' => '<a href="mailto:"' . hsc($mail) . '">' . hsc($mail) . '</a>'
69524870174SAndreas Gohr        ];
696774514c9SGerrit Uitslag        $signature = $lang['email_signature_text'];
697774514c9SGerrit Uitslag        if (!empty($lang['email_signature_html'])) {
698774514c9SGerrit Uitslag            $signature = $lang['email_signature_html'];
699774514c9SGerrit Uitslag        }
700774514c9SGerrit Uitslag        $signature = str_replace(
70124870174SAndreas Gohr            ['@DOKUWIKIURL@', "\n"],
70224870174SAndreas Gohr            [$this->replacements['html']['DOKUWIKIURL'], '<br />'],
703774514c9SGerrit Uitslag            $signature
704774514c9SGerrit Uitslag        );
705774514c9SGerrit Uitslag        $this->replacements['html']['EMAILSIGNATURE'] = $signature;
7069ea45836SChristopher Smith    }
7079ea45836SChristopher Smith
7089ea45836SChristopher Smith    /**
7091d045709SAndreas Gohr     * Send the mail
7101d045709SAndreas Gohr     *
7111d045709SAndreas Gohr     * Call this after all data was set
7121d045709SAndreas Gohr     *
71328d2ad80SAndreas Gohr     * @triggers MAIL_MESSAGE_SEND
7141d045709SAndreas Gohr     * @return bool true if the mail was successfully passed to the MTA
7151d045709SAndreas Gohr     */
716d868eb89SAndreas Gohr    public function send()
717d868eb89SAndreas Gohr    {
7183f6872b1SMyron Turner        global $lang;
71928d2ad80SAndreas Gohr        $success = false;
720a36fc348SAndreas Gohr
72128d2ad80SAndreas Gohr        // prepare hook data
72224870174SAndreas Gohr        $data = [
72328d2ad80SAndreas Gohr            // pass the whole mail class to plugin
72428d2ad80SAndreas Gohr            'mail'    => $this,
72528d2ad80SAndreas Gohr            // pass references for backward compatibility
72628d2ad80SAndreas Gohr            'to'      => &$this->headers['To'],
72728d2ad80SAndreas Gohr            'cc'      => &$this->headers['Cc'],
72828d2ad80SAndreas Gohr            'bcc'     => &$this->headers['Bcc'],
72928d2ad80SAndreas Gohr            'from'    => &$this->headers['From'],
73028d2ad80SAndreas Gohr            'subject' => &$this->headers['Subject'],
73128d2ad80SAndreas Gohr            'body'    => &$this->text,
732a89c75afSAndreas Gohr            'params'  => &$this->sendparam,
73328d2ad80SAndreas Gohr            'headers' => '', // plugins shouldn't use this
73428d2ad80SAndreas Gohr            // signal if we mailed successfully to AFTER event
73528d2ad80SAndreas Gohr            'success' => &$success,
73624870174SAndreas Gohr        ];
73728d2ad80SAndreas Gohr
73828d2ad80SAndreas Gohr        // do our thing if BEFORE hook approves
739e1d9dcc8SAndreas Gohr        $evt = new Event('MAIL_MESSAGE_SEND', $data);
74028d2ad80SAndreas Gohr        if ($evt->advise_before(true)) {
74128d2ad80SAndreas Gohr            // clean up before using the headers
742a36fc348SAndreas Gohr            $this->cleanHeaders();
743a36fc348SAndreas Gohr
7441d045709SAndreas Gohr            // any recipients?
7457d34963bSAndreas Gohr            if (
7467d34963bSAndreas Gohr                trim($this->headers['To']) === '' &&
7471d045709SAndreas Gohr                trim($this->headers['Cc']) === '' &&
748a89c75afSAndreas Gohr                trim($this->headers['Bcc']) === ''
749a89c75afSAndreas Gohr            ) return false;
7501d045709SAndreas Gohr
7511d045709SAndreas Gohr            // The To: header is special
7526be717dbSMichael Hamann            if (array_key_exists('To', $this->headers)) {
7536be717dbSMichael Hamann                $to = (string)$this->headers['To'];
7541d045709SAndreas Gohr                unset($this->headers['To']);
7551d045709SAndreas Gohr            } else {
7561d045709SAndreas Gohr                $to = '';
7571d045709SAndreas Gohr            }
7581d045709SAndreas Gohr
7591d045709SAndreas Gohr            // so is the subject
7606be717dbSMichael Hamann            if (array_key_exists('Subject', $this->headers)) {
7616be717dbSMichael Hamann                $subject = (string)$this->headers['Subject'];
7621d045709SAndreas Gohr                unset($this->headers['Subject']);
7631d045709SAndreas Gohr            } else {
7641d045709SAndreas Gohr                $subject = '';
7651d045709SAndreas Gohr            }
7661d045709SAndreas Gohr
7671d045709SAndreas Gohr            // make the body
7681d045709SAndreas Gohr            $body = $this->prepareBody();
7694c89a7f6SAndreas Gohr            if ($body === false) return false;
7701d045709SAndreas Gohr
7711d045709SAndreas Gohr            // cook the headers
7721d045709SAndreas Gohr            $headers = $this->prepareHeaders();
77328d2ad80SAndreas Gohr            // add any headers set by legacy plugins
77428d2ad80SAndreas Gohr            if (trim($data['headers'])) {
77528d2ad80SAndreas Gohr                $headers .= MAILHEADER_EOL . trim($data['headers']);
77628d2ad80SAndreas Gohr            }
7771d045709SAndreas Gohr
7783f6872b1SMyron Turner            if (!function_exists('mail')) {
7793f6872b1SMyron Turner                $emsg = $lang['email_fail'] . $subject;
7803f6872b1SMyron Turner                error_log($emsg);
7813f6872b1SMyron Turner                msg(hsc($emsg), -1, __LINE__, __FILE__, MSG_MANAGERS_ONLY);
7823f6872b1SMyron Turner                $evt->advise_after();
7833f6872b1SMyron Turner                return false;
7843f6872b1SMyron Turner            }
7853f6872b1SMyron Turner
7861d045709SAndreas Gohr            // send the thing
787bfa6d256SAndreas Gohr            if ($to === '') $to = '(undisclosed-recipients)'; // #1422
788749c0023SAndreas Gohr            if ($this->sendparam === null) {
78928d2ad80SAndreas Gohr                $success = @mail($to, $subject, $body, $headers);
7901d045709SAndreas Gohr            } else {
79128d2ad80SAndreas Gohr                $success = @mail($to, $subject, $body, $headers, $this->sendparam);
7921d045709SAndreas Gohr            }
7931d045709SAndreas Gohr        }
79428d2ad80SAndreas Gohr        // any AFTER actions?
79528d2ad80SAndreas Gohr        $evt->advise_after();
79628d2ad80SAndreas Gohr        return $success;
79728d2ad80SAndreas Gohr    }
798bb01c27cSAndreas Gohr}
799