xref: /dokuwiki/inc/Mailer.class.php (revision dccd6b2bba7367e4d1d2d7aa84c9f9d15584b593)
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     */
39d868eb89SAndreas Gohr    public function __construct()
40d868eb89SAndreas 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     */
82d868eb89SAndreas Gohr    public function attachFile($path, $mime, $name = '', $embed = '')
83d868eb89SAndreas 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     */
104d868eb89SAndreas Gohr    public function attachContent($data, $mime, $name = '', $embed = '')
105d868eb89SAndreas 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     */
125d868eb89SAndreas Gohr    protected function autoEmbedCallBack($matches)
126d868eb89SAndreas 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     */
150d868eb89SAndreas Gohr    public function setHeader($header, $value, $clean = true)
151d868eb89SAndreas 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     */
181d868eb89SAndreas Gohr    public function setParameters($param)
182d868eb89SAndreas 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     */
201d868eb89SAndreas Gohr    public function setBody($text, $textrep = null, $htmlrep = null, $html = null, $wrap = true)
202d868eb89SAndreas 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\(([^\)]+)\)@/',
238*dccd6b2bSAndreas Gohr            [$this, 'autoEmbedCallBack'],
239*dccd6b2bSAndreas Gohr            $html
240a89c75afSAndreas Gohr        );
241850dbf1fSAndreas Gohr
2429ea45836SChristopher Smith        // add default token replacements
24324870174SAndreas Gohr        $trep = array_merge($this->replacements['text'], $textrep);
24424870174SAndreas Gohr        $hrep = array_merge($this->replacements['html'], $htmlrep);
245abbf0890SAndreas Gohr
246abbf0890SAndreas Gohr        // Apply replacements
247abbf0890SAndreas Gohr        foreach($trep as $key => $substitution) {
248abbf0890SAndreas Gohr            $text = str_replace('@'.strtoupper($key).'@', $substitution, $text);
249abbf0890SAndreas Gohr        }
250abbf0890SAndreas Gohr        foreach($hrep as $key => $substitution) {
251abbf0890SAndreas Gohr            $html = str_replace('@'.strtoupper($key).'@', $substitution, $html);
252abbf0890SAndreas Gohr        }
253abbf0890SAndreas Gohr
254abbf0890SAndreas Gohr        $this->setHTML($html);
255abbf0890SAndreas Gohr        $this->setText($text);
256abbf0890SAndreas Gohr    }
257abbf0890SAndreas Gohr
258abbf0890SAndreas Gohr    /**
259bb01c27cSAndreas Gohr     * Set the HTML part of the mail
260bb01c27cSAndreas Gohr     *
261bb01c27cSAndreas Gohr     * Placeholders can be used to reference embedded attachments
262abbf0890SAndreas Gohr     *
263abbf0890SAndreas Gohr     * You probably want to use setBody() instead
26442ea7f44SGerrit Uitslag     *
26542ea7f44SGerrit Uitslag     * @param string $html
266bb01c27cSAndreas Gohr     */
267d868eb89SAndreas Gohr    public function setHTML($html)
268d868eb89SAndreas Gohr    {
269bb01c27cSAndreas Gohr        $this->html = $html;
270bb01c27cSAndreas Gohr    }
271bb01c27cSAndreas Gohr
272bb01c27cSAndreas Gohr    /**
273bb01c27cSAndreas Gohr     * Set the plain text part of the mail
274abbf0890SAndreas Gohr     *
275abbf0890SAndreas Gohr     * You probably want to use setBody() instead
27642ea7f44SGerrit Uitslag     *
27742ea7f44SGerrit Uitslag     * @param string $text
278bb01c27cSAndreas Gohr     */
279d868eb89SAndreas Gohr    public function setText($text)
280d868eb89SAndreas Gohr    {
281bb01c27cSAndreas Gohr        $this->text = $text;
282bb01c27cSAndreas Gohr    }
283bb01c27cSAndreas Gohr
284bb01c27cSAndreas Gohr    /**
285a36fc348SAndreas Gohr     * Add the To: recipients
286a36fc348SAndreas Gohr     *
2878c253612SGerrit Uitslag     * @see cleanAddress
28859bc3b48SGerrit Uitslag     * @param string|string[]  $address Multiple adresses separated by commas or as array
289a36fc348SAndreas Gohr     */
290d868eb89SAndreas Gohr    public function to($address)
291d868eb89SAndreas Gohr    {
292a36fc348SAndreas Gohr        $this->setHeader('To', $address, false);
293a36fc348SAndreas Gohr    }
294a36fc348SAndreas Gohr
295a36fc348SAndreas Gohr    /**
296a36fc348SAndreas Gohr     * Add the Cc: recipients
297a36fc348SAndreas Gohr     *
2988c253612SGerrit Uitslag     * @see cleanAddress
29959bc3b48SGerrit Uitslag     * @param string|string[]  $address Multiple adresses separated by commas or as array
300a36fc348SAndreas Gohr     */
301d868eb89SAndreas Gohr    public function cc($address)
302d868eb89SAndreas Gohr    {
303a36fc348SAndreas Gohr        $this->setHeader('Cc', $address, false);
304a36fc348SAndreas Gohr    }
305a36fc348SAndreas Gohr
306a36fc348SAndreas Gohr    /**
307a36fc348SAndreas Gohr     * Add the Bcc: recipients
308a36fc348SAndreas Gohr     *
3098c253612SGerrit Uitslag     * @see cleanAddress
31059bc3b48SGerrit Uitslag     * @param string|string[]  $address Multiple adresses separated by commas or as array
311a36fc348SAndreas Gohr     */
312d868eb89SAndreas Gohr    public function bcc($address)
313d868eb89SAndreas Gohr    {
314a36fc348SAndreas Gohr        $this->setHeader('Bcc', $address, false);
315a36fc348SAndreas Gohr    }
316a36fc348SAndreas Gohr
317a36fc348SAndreas Gohr    /**
318a36fc348SAndreas Gohr     * Add the From: address
319a36fc348SAndreas Gohr     *
320a36fc348SAndreas Gohr     * This is set to $conf['mailfrom'] when not specified so you shouldn't need
321a36fc348SAndreas Gohr     * to call this function
322a36fc348SAndreas Gohr     *
3238c253612SGerrit Uitslag     * @see cleanAddress
324a36fc348SAndreas Gohr     * @param string  $address from address
325a36fc348SAndreas Gohr     */
326d868eb89SAndreas Gohr    public function from($address)
327d868eb89SAndreas Gohr    {
328a36fc348SAndreas Gohr        $this->setHeader('From', $address, false);
329a36fc348SAndreas Gohr    }
330a36fc348SAndreas Gohr
331a36fc348SAndreas Gohr    /**
332a36fc348SAndreas Gohr     * Add the mail's Subject: header
333a36fc348SAndreas Gohr     *
334a36fc348SAndreas Gohr     * @param string $subject the mail subject
335a36fc348SAndreas Gohr     */
336d868eb89SAndreas Gohr    public function subject($subject)
337d868eb89SAndreas Gohr    {
338a36fc348SAndreas Gohr        $this->headers['Subject'] = $subject;
339a36fc348SAndreas Gohr    }
340a36fc348SAndreas Gohr
341a36fc348SAndreas Gohr    /**
342102cdbd7SLarsGit223     * Return a clean name which can be safely used in mail address
343102cdbd7SLarsGit223     * fields. That means the name will be enclosed in '"' if it includes
344102cdbd7SLarsGit223     * a '"' or a ','. Also a '"' will be escaped as '\"'.
345102cdbd7SLarsGit223     *
346102cdbd7SLarsGit223     * @param string $name the name to clean-up
347102cdbd7SLarsGit223     * @see cleanAddress
348102cdbd7SLarsGit223     */
349d868eb89SAndreas Gohr    public function getCleanName($name)
350d868eb89SAndreas Gohr    {
3512b58f049SAndreas Gohr        $name = trim($name, " \t\"");
352102cdbd7SLarsGit223        $name = str_replace('"', '\"', $name, $count);
353102cdbd7SLarsGit223        if ($count > 0 || strpos($name, ',') !== false) {
354102cdbd7SLarsGit223            $name = '"'.$name.'"';
355102cdbd7SLarsGit223        }
356102cdbd7SLarsGit223        return $name;
357102cdbd7SLarsGit223    }
358102cdbd7SLarsGit223
359102cdbd7SLarsGit223    /**
3601d045709SAndreas Gohr     * Sets an email address header with correct encoding
361bb01c27cSAndreas Gohr     *
362bb01c27cSAndreas Gohr     * Unicode characters will be deaccented and encoded base64
363bb01c27cSAndreas Gohr     * for headers. Addresses may not contain Non-ASCII data!
364bb01c27cSAndreas Gohr     *
365102cdbd7SLarsGit223     * If @$addresses is a string then it will be split into multiple
366102cdbd7SLarsGit223     * addresses. Addresses must be separated by a comma. If the display
367102cdbd7SLarsGit223     * name includes a comma then it MUST be properly enclosed by '"' to
368102cdbd7SLarsGit223     * prevent spliting at the wrong point.
369102cdbd7SLarsGit223     *
370bb01c27cSAndreas Gohr     * Example:
3718c253612SGerrit Uitslag     *   cc("föö <foo@bar.com>, me@somewhere.com","TBcc");
372102cdbd7SLarsGit223     *   to("foo, Dr." <foo@bar.com>, me@somewhere.com");
373bb01c27cSAndreas Gohr     *
37442ea7f44SGerrit Uitslag     * @param string|string[]  $addresses Multiple adresses separated by commas or as array
37542ea7f44SGerrit Uitslag     * @return false|string  the prepared header (can contain multiple lines)
376bb01c27cSAndreas Gohr     */
377d868eb89SAndreas Gohr    public function cleanAddress($addresses)
378d868eb89SAndreas Gohr    {
379bb01c27cSAndreas Gohr        $headers = '';
380b6c97c70SAndreas Gohr        if(!is_array($addresses)){
381d31a1599SLarsGit223            $count = preg_match_all('/\s*(?:("[^"]*"[^,]+),*)|([^,]+)\s*,*/', $addresses, $matches, PREG_SET_ORDER);
38224870174SAndreas Gohr            $addresses = [];
383743792d0SLarsGit223            if ($count !== false && is_array($matches)) {
384102cdbd7SLarsGit223                foreach ($matches as $match) {
38524870174SAndreas Gohr                    $addresses[] = rtrim($match[0], ',');
386102cdbd7SLarsGit223                }
387b6c97c70SAndreas Gohr            }
388b6c97c70SAndreas Gohr        }
389b6c97c70SAndreas Gohr
390b6c97c70SAndreas Gohr        foreach($addresses as $part) {
391b6c97c70SAndreas Gohr            $part = preg_replace('/[\r\n\0]+/', ' ', $part); // remove attack vectors
392bb01c27cSAndreas Gohr            $part = trim($part);
393bb01c27cSAndreas Gohr
394bb01c27cSAndreas Gohr            // parse address
395bb01c27cSAndreas Gohr            if(preg_match('#(.*?)<(.*?)>#', $part, $matches)) {
396bb01c27cSAndreas Gohr                $text = trim($matches[1]);
397bb01c27cSAndreas Gohr                $addr = $matches[2];
398bb01c27cSAndreas Gohr            } else {
39910da1f74SAndreas Gohr                $text = '';
400bb01c27cSAndreas Gohr                $addr = $part;
401bb01c27cSAndreas Gohr            }
402bb01c27cSAndreas Gohr            // skip empty ones
403bb01c27cSAndreas Gohr            if(empty($addr)) {
404bb01c27cSAndreas Gohr                continue;
405bb01c27cSAndreas Gohr            }
406bb01c27cSAndreas Gohr
407bb01c27cSAndreas Gohr            // FIXME: is there a way to encode the localpart of a emailaddress?
40824870174SAndreas Gohr            if(!Clean::isASCII($addr)) {
4094772cf38SAndreas Gohr                msg(hsc("E-Mail address <$addr> is not ASCII"), -1, __LINE__, __FILE__, MSG_ADMINS_ONLY);
410bb01c27cSAndreas Gohr                continue;
411bb01c27cSAndreas Gohr            }
412bb01c27cSAndreas Gohr
41364d23c16SAndreas Gohr            if(!mail_isvalid($addr)) {
4144772cf38SAndreas Gohr                msg(hsc("E-Mail address <$addr> is not valid"), -1, __LINE__, __FILE__, MSG_ADMINS_ONLY);
415bb01c27cSAndreas Gohr                continue;
416bb01c27cSAndreas Gohr            }
417bb01c27cSAndreas Gohr
418bb01c27cSAndreas Gohr            // text was given
41930085ef3SYurii K            if(!empty($text) && !isWindows()) { // No named recipients for To: in Windows (see FS#652)
420bb01c27cSAndreas Gohr                // add address quotes
421bb01c27cSAndreas Gohr                $addr = "<$addr>";
422bb01c27cSAndreas Gohr
423bb01c27cSAndreas Gohr                if(defined('MAILHEADER_ASCIIONLY')) {
42424870174SAndreas Gohr                    $text = Clean::deaccent($text);
42524870174SAndreas Gohr                    $text = Clean::strip($text);
426bb01c27cSAndreas Gohr                }
427bb01c27cSAndreas Gohr
42824870174SAndreas Gohr                if(strpos($text, ',') !== false || !Clean::isASCII($text)) {
429bb01c27cSAndreas Gohr                    $text = '=?UTF-8?B?'.base64_encode($text).'?=';
430bb01c27cSAndreas Gohr                }
431bb01c27cSAndreas Gohr            } else {
432bb01c27cSAndreas Gohr                $text = '';
433bb01c27cSAndreas Gohr            }
434bb01c27cSAndreas Gohr
435bb01c27cSAndreas Gohr            // add to header comma seperated
436bb01c27cSAndreas Gohr            if($headers != '') {
437bb01c27cSAndreas Gohr                $headers .= ', ';
438bb01c27cSAndreas Gohr            }
439bb01c27cSAndreas Gohr            $headers .= $text.' '.$addr;
440bb01c27cSAndreas Gohr        }
441bb01c27cSAndreas Gohr
442b6c97c70SAndreas Gohr        $headers = trim($headers);
443bb01c27cSAndreas Gohr        if(empty($headers)) return false;
444bb01c27cSAndreas Gohr
445bb01c27cSAndreas Gohr        return $headers;
446bb01c27cSAndreas Gohr    }
447bb01c27cSAndreas Gohr
448bb01c27cSAndreas Gohr
449bb01c27cSAndreas Gohr    /**
450bb01c27cSAndreas Gohr     * Prepare the mime multiparts for all attachments
451bb01c27cSAndreas Gohr     *
452bb01c27cSAndreas Gohr     * Replaces placeholders in the HTML with the correct CIDs
45342ea7f44SGerrit Uitslag     *
45442ea7f44SGerrit Uitslag     * @return string mime multiparts
455bb01c27cSAndreas Gohr     */
456d868eb89SAndreas Gohr    protected function prepareAttachments()
457d868eb89SAndreas Gohr    {
458bb01c27cSAndreas Gohr        $mime = '';
459bb01c27cSAndreas Gohr        $part = 1;
460bb01c27cSAndreas Gohr        // embedded attachments
461bb01c27cSAndreas Gohr        foreach($this->attach as $media) {
462ce9d2cc8SAndreas Gohr            $media['name'] = str_replace(':', '_', cleanID($media['name'], true));
463ce9d2cc8SAndreas Gohr
464bb01c27cSAndreas Gohr            // create content id
465bb01c27cSAndreas Gohr            $cid = 'part'.$part.'.'.$this->partid;
466bb01c27cSAndreas Gohr
467bb01c27cSAndreas Gohr            // replace wildcards
468bb01c27cSAndreas Gohr            if($media['embed']) {
469bb01c27cSAndreas Gohr                $this->html = str_replace('%%'.$media['embed'].'%%', 'cid:'.$cid, $this->html);
470bb01c27cSAndreas Gohr            }
471bb01c27cSAndreas Gohr
472bb01c27cSAndreas Gohr            $mime .= '--'.$this->boundary.MAILHEADER_EOL;
4731d8036c2SAndreas Gohr            $mime .= $this->wrappedHeaderLine('Content-Type', $media['mime'].'; id="'.$cid.'"');
4741d8036c2SAndreas Gohr            $mime .= $this->wrappedHeaderLine('Content-Transfer-Encoding', 'base64');
4751d8036c2SAndreas Gohr            $mime .= $this->wrappedHeaderLine('Content-ID', "<$cid>");
476bb01c27cSAndreas Gohr            if($media['embed']) {
4771d8036c2SAndreas Gohr                $mime .= $this->wrappedHeaderLine('Content-Disposition', 'inline; filename='.$media['name']);
478bb01c27cSAndreas Gohr            } else {
4791d8036c2SAndreas Gohr                $mime .= $this->wrappedHeaderLine('Content-Disposition', 'attachment; filename='.$media['name']);
480bb01c27cSAndreas Gohr            }
481bb01c27cSAndreas Gohr            $mime .= MAILHEADER_EOL; //end of headers
482bb01c27cSAndreas Gohr            $mime .= chunk_split(base64_encode($media['data']), 74, MAILHEADER_EOL);
483bb01c27cSAndreas Gohr
484bb01c27cSAndreas Gohr            $part++;
485bb01c27cSAndreas Gohr        }
486bb01c27cSAndreas Gohr        return $mime;
487bb01c27cSAndreas Gohr    }
488bb01c27cSAndreas Gohr
4891d045709SAndreas Gohr    /**
4901d045709SAndreas Gohr     * Build the body and handles multi part mails
4911d045709SAndreas Gohr     *
4921d045709SAndreas Gohr     * Needs to be called before prepareHeaders!
4931d045709SAndreas Gohr     *
4941d045709SAndreas Gohr     * @return string the prepared mail body, false on errors
4951d045709SAndreas Gohr     */
496d868eb89SAndreas Gohr    protected function prepareBody()
497d868eb89SAndreas Gohr    {
4981d045709SAndreas Gohr
4992398a2b5SAndreas Gohr        // no HTML mails allowed? remove HTML body
5002398a2b5SAndreas Gohr        if(!$this->allowhtml) {
5012398a2b5SAndreas Gohr            $this->html = '';
5022398a2b5SAndreas Gohr        }
5032398a2b5SAndreas Gohr
504bb01c27cSAndreas Gohr        // check for body
505bb01c27cSAndreas Gohr        if(!$this->text && !$this->html) {
506bb01c27cSAndreas Gohr            return false;
507bb01c27cSAndreas Gohr        }
508bb01c27cSAndreas Gohr
509bb01c27cSAndreas Gohr        // add general headers
510bb01c27cSAndreas Gohr        $this->headers['MIME-Version'] = '1.0';
511bb01c27cSAndreas Gohr
5121d045709SAndreas Gohr        $body = '';
5131d045709SAndreas Gohr
514bb01c27cSAndreas Gohr        if(!$this->html && !count($this->attach)) { // we can send a simple single part message
515bb01c27cSAndreas Gohr            $this->headers['Content-Type']              = 'text/plain; charset=UTF-8';
516bb01c27cSAndreas Gohr            $this->headers['Content-Transfer-Encoding'] = 'base64';
517be3cc6abSAndreas Gohr            $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL);
518bb01c27cSAndreas Gohr        } else { // multi part it is
5191d045709SAndreas Gohr            $body .= "This is a multi-part message in MIME format.".MAILHEADER_EOL;
520bb01c27cSAndreas Gohr
521bb01c27cSAndreas Gohr            // prepare the attachments
522bb01c27cSAndreas Gohr            $attachments = $this->prepareAttachments();
523bb01c27cSAndreas Gohr
524bb01c27cSAndreas Gohr            // do we have alternative text content?
525bb01c27cSAndreas Gohr            if($this->text && $this->html) {
526a36fc348SAndreas Gohr                $this->headers['Content-Type'] = 'multipart/alternative;'.MAILHEADER_EOL.
527a36fc348SAndreas Gohr                    '  boundary="'.$this->boundary.'XX"';
528bb01c27cSAndreas Gohr                $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL;
5291d045709SAndreas Gohr                $body .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL;
5301d045709SAndreas Gohr                $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL;
531bb01c27cSAndreas Gohr                $body .= MAILHEADER_EOL;
532be3cc6abSAndreas Gohr                $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL);
533bb01c27cSAndreas Gohr                $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL;
534a36fc348SAndreas Gohr                $body .= 'Content-Type: multipart/related;'.MAILHEADER_EOL.
535d6e04b60SAndreas Gohr                    '  boundary="'.$this->boundary.'";'.MAILHEADER_EOL.
536d6e04b60SAndreas Gohr                    '  type="text/html"'.MAILHEADER_EOL;
537bb01c27cSAndreas Gohr                $body .= MAILHEADER_EOL;
538bb01c27cSAndreas Gohr            }
539bb01c27cSAndreas Gohr
5401d045709SAndreas Gohr            $body .= '--'.$this->boundary.MAILHEADER_EOL;
5411d045709SAndreas Gohr            $body .= 'Content-Type: text/html; charset=UTF-8'.MAILHEADER_EOL;
5421d045709SAndreas Gohr            $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL;
543bb01c27cSAndreas Gohr            $body .= MAILHEADER_EOL;
544be3cc6abSAndreas Gohr            $body .= chunk_split(base64_encode($this->html), 72, MAILHEADER_EOL);
545bb01c27cSAndreas Gohr            $body .= MAILHEADER_EOL;
546bb01c27cSAndreas Gohr            $body .= $attachments;
547bb01c27cSAndreas Gohr            $body .= '--'.$this->boundary.'--'.MAILHEADER_EOL;
548bb01c27cSAndreas Gohr
549bb01c27cSAndreas Gohr            // close open multipart/alternative boundary
550bb01c27cSAndreas Gohr            if($this->text && $this->html) {
551bb01c27cSAndreas Gohr                $body .= '--'.$this->boundary.'XX--'.MAILHEADER_EOL;
552bb01c27cSAndreas Gohr            }
553bb01c27cSAndreas Gohr        }
554bb01c27cSAndreas Gohr
555bb01c27cSAndreas Gohr        return $body;
556bb01c27cSAndreas Gohr    }
557bb01c27cSAndreas Gohr
558bb01c27cSAndreas Gohr    /**
559a36fc348SAndreas Gohr     * Cleanup and encode the headers array
560a36fc348SAndreas Gohr     */
561d868eb89SAndreas Gohr    protected function cleanHeaders()
562d868eb89SAndreas Gohr    {
563a36fc348SAndreas Gohr        global $conf;
564a36fc348SAndreas Gohr
565a36fc348SAndreas Gohr        // clean up addresses
566a36fc348SAndreas Gohr        if(empty($this->headers['From'])) $this->from($conf['mailfrom']);
56724870174SAndreas Gohr        $addrs = ['To', 'From', 'Cc', 'Bcc', 'Reply-To', 'Sender'];
568a36fc348SAndreas Gohr        foreach($addrs as $addr) {
569a36fc348SAndreas Gohr            if(isset($this->headers[$addr])) {
570a36fc348SAndreas Gohr                $this->headers[$addr] = $this->cleanAddress($this->headers[$addr]);
571a36fc348SAndreas Gohr            }
572a36fc348SAndreas Gohr        }
573a36fc348SAndreas Gohr
57445992a63SAndreas Gohr        if(isset($this->headers['Subject'])) {
575a36fc348SAndreas Gohr            // add prefix to subject
57654f30755SAndreas Gohr            if(empty($conf['mailprefix'])) {
57724870174SAndreas Gohr                if(PhpString::strlen($conf['title']) < 20) {
57854f30755SAndreas Gohr                    $prefix = '['.$conf['title'].']';
57954f30755SAndreas Gohr                } else {
58024870174SAndreas Gohr                    $prefix = '['.PhpString::substr($conf['title'], 0, 20).'...]';
5818a215f09SAndreas Gohr                }
5828a215f09SAndreas Gohr            } else {
583a36fc348SAndreas Gohr                $prefix = '['.$conf['mailprefix'].']';
58454f30755SAndreas Gohr            }
585a36fc348SAndreas Gohr            $len = strlen($prefix);
58624870174SAndreas Gohr            if(substr($this->headers['Subject'], 0, $len) !== $prefix) {
58745992a63SAndreas Gohr                $this->headers['Subject'] = $prefix.' '.$this->headers['Subject'];
588a36fc348SAndreas Gohr            }
589a36fc348SAndreas Gohr
590a36fc348SAndreas Gohr            // encode subject
591a36fc348SAndreas Gohr            if(defined('MAILHEADER_ASCIIONLY')) {
59224870174SAndreas Gohr                $this->headers['Subject'] = Clean::deaccent($this->headers['Subject']);
59324870174SAndreas Gohr                $this->headers['Subject'] = Clean::strip($this->headers['Subject']);
594a36fc348SAndreas Gohr            }
59524870174SAndreas Gohr            if(!Clean::isASCII($this->headers['Subject'])) {
59645992a63SAndreas Gohr                $this->headers['Subject'] = '=?UTF-8?B?'.base64_encode($this->headers['Subject']).'?=';
597a36fc348SAndreas Gohr            }
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?
7451d045709SAndreas Gohr            if(trim($this->headers['To']) === '' &&
7461d045709SAndreas Gohr                trim($this->headers['Cc']) === '' &&
747a89c75afSAndreas Gohr                trim($this->headers['Bcc']) === ''
748a89c75afSAndreas Gohr            ) return false;
7491d045709SAndreas Gohr
7501d045709SAndreas Gohr            // The To: header is special
7516be717dbSMichael Hamann            if(array_key_exists('To', $this->headers)) {
7526be717dbSMichael Hamann                $to = (string)$this->headers['To'];
7531d045709SAndreas Gohr                unset($this->headers['To']);
7541d045709SAndreas Gohr            } else {
7551d045709SAndreas Gohr                $to = '';
7561d045709SAndreas Gohr            }
7571d045709SAndreas Gohr
7581d045709SAndreas Gohr            // so is the subject
7596be717dbSMichael Hamann            if(array_key_exists('Subject', $this->headers)) {
7606be717dbSMichael Hamann                $subject = (string)$this->headers['Subject'];
7611d045709SAndreas Gohr                unset($this->headers['Subject']);
7621d045709SAndreas Gohr            } else {
7631d045709SAndreas Gohr                $subject = '';
7641d045709SAndreas Gohr            }
7651d045709SAndreas Gohr
7661d045709SAndreas Gohr            // make the body
7671d045709SAndreas Gohr            $body = $this->prepareBody();
7684c89a7f6SAndreas Gohr            if($body === false) return false;
7691d045709SAndreas Gohr
7701d045709SAndreas Gohr            // cook the headers
7711d045709SAndreas Gohr            $headers = $this->prepareHeaders();
77228d2ad80SAndreas Gohr            // add any headers set by legacy plugins
77328d2ad80SAndreas Gohr            if(trim($data['headers'])) {
77428d2ad80SAndreas Gohr                $headers .= MAILHEADER_EOL.trim($data['headers']);
77528d2ad80SAndreas Gohr            }
7761d045709SAndreas Gohr
7773f6872b1SMyron Turner            if(!function_exists('mail')){
7783f6872b1SMyron Turner                $emsg = $lang['email_fail'] . $subject;
7793f6872b1SMyron Turner                error_log($emsg);
7803f6872b1SMyron Turner                msg(hsc($emsg), -1, __LINE__, __FILE__, MSG_MANAGERS_ONLY);
7813f6872b1SMyron Turner                $evt->advise_after();
7823f6872b1SMyron Turner                return false;
7833f6872b1SMyron Turner            }
7843f6872b1SMyron Turner
7851d045709SAndreas Gohr            // send the thing
786bfa6d256SAndreas Gohr            if($to === '') $to = '(undisclosed-recipients)'; // #1422
787749c0023SAndreas Gohr            if($this->sendparam === null) {
78828d2ad80SAndreas Gohr                $success = @mail($to, $subject, $body, $headers);
7891d045709SAndreas Gohr            } else {
79028d2ad80SAndreas Gohr                $success = @mail($to, $subject, $body, $headers, $this->sendparam);
7911d045709SAndreas Gohr            }
7921d045709SAndreas Gohr        }
79328d2ad80SAndreas Gohr        // any AFTER actions?
79428d2ad80SAndreas Gohr        $evt->advise_after();
79528d2ad80SAndreas Gohr        return $success;
79628d2ad80SAndreas Gohr    }
797bb01c27cSAndreas Gohr}
798