xref: /dokuwiki/inc/Mailer.class.php (revision d31a1599b2067cded97f1b5a3ceee56b685a864f)
1bb01c27cSAndreas Gohr<?php
2bb01c27cSAndreas Gohr/**
31d045709SAndreas Gohr * A class to build and send multi part mails (with HTML content and embedded
41d045709SAndreas Gohr * attachments). All mails are assumed to be in UTF-8 encoding.
51d045709SAndreas Gohr *
61d045709SAndreas Gohr * Attachments are handled in memory so this shouldn't be used to send huge
71d045709SAndreas Gohr * files, but then again mail shouldn't be used to send huge files either.
81d045709SAndreas Gohr *
9bb01c27cSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
10bb01c27cSAndreas Gohr */
11bb01c27cSAndreas Gohr
12bb01c27cSAndreas Gohr// end of line for mail lines - RFC822 says CRLF but postfix (and other MTAs?)
13bb01c27cSAndreas Gohr// think different
14bb01c27cSAndreas Gohrif(!defined('MAILHEADER_EOL')) define('MAILHEADER_EOL', "\n");
15bb01c27cSAndreas Gohr#define('MAILHEADER_ASCIIONLY',1);
16bb01c27cSAndreas Gohr
17a89c75afSAndreas Gohr/**
18a89c75afSAndreas Gohr * Mail Handling
19a89c75afSAndreas Gohr */
20bb01c27cSAndreas Gohrclass Mailer {
21bb01c27cSAndreas Gohr
22f41c79d7SAndreas Gohr    protected $headers   = array();
23f41c79d7SAndreas Gohr    protected $attach    = array();
24f41c79d7SAndreas Gohr    protected $html      = '';
25f41c79d7SAndreas Gohr    protected $text      = '';
26bb01c27cSAndreas Gohr
27f41c79d7SAndreas Gohr    protected $boundary  = '';
28f41c79d7SAndreas Gohr    protected $partid    = '';
29f41c79d7SAndreas Gohr    protected $sendparam = null;
30bb01c27cSAndreas Gohr
31f41c79d7SAndreas Gohr    protected $allowhtml = true;
321d045709SAndreas Gohr
339ea45836SChristopher Smith    protected $replacements = array('text'=> array(), 'html' => array());
349ea45836SChristopher Smith
351d045709SAndreas Gohr    /**
361d045709SAndreas Gohr     * Constructor
371d045709SAndreas Gohr     *
389ea45836SChristopher Smith     * Initializes the boundary strings, part counters and token replacements
391d045709SAndreas Gohr     */
401d045709SAndreas Gohr    public function __construct() {
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);
46d6e04b60SAndreas Gohr        if(strpos($server,'.') === false) $server = $server.'.localhost';
471d045709SAndreas Gohr
481d8036c2SAndreas Gohr        $this->partid   = substr(md5(uniqid(rand(), true)),0, 8).'@'.$server;
49b3577cf9SAndreas Gohr        $this->boundary = '__________'.md5(uniqid(rand(), true));
509f3eca0bSAndreas Gohr
519f3eca0bSAndreas Gohr        $listid = join('.', array_reverse(explode('/', DOKU_BASE))).$server;
529f3eca0bSAndreas Gohr        $listid = strtolower(trim($listid, '.'));
539f3eca0bSAndreas Gohr
542398a2b5SAndreas Gohr        $this->allowhtml = (bool)$conf['htmlmail'];
552398a2b5SAndreas Gohr
569f3eca0bSAndreas Gohr        // add some default headers for mailfiltering FS#2247
575f43dcf4SLukas Rademacher        if(!empty($conf['mailreturnpath'])) {
585f43dcf4SLukas Rademacher            $this->setHeader('Return-Path', $conf['mailreturnpath']);
595f43dcf4SLukas Rademacher        }
606a1f928fSAndreas Gohr        $this->setHeader('X-Mailer', 'DokuWiki');
61585bf44eSChristopher Smith        $this->setHeader('X-DokuWiki-User', $INPUT->server->str('REMOTE_USER'));
629f3eca0bSAndreas Gohr        $this->setHeader('X-DokuWiki-Title', $conf['title']);
639f3eca0bSAndreas Gohr        $this->setHeader('X-DokuWiki-Server', $server);
649f3eca0bSAndreas Gohr        $this->setHeader('X-Auto-Response-Suppress', 'OOF');
659f3eca0bSAndreas Gohr        $this->setHeader('List-Id', $conf['title'].' <'.$listid.'>');
66d6e04b60SAndreas Gohr        $this->setHeader('Date', date('r'), false);
679ea45836SChristopher Smith
689ea45836SChristopher Smith        $this->prepareTokenReplacements();
69bb01c27cSAndreas Gohr    }
70bb01c27cSAndreas Gohr
71bb01c27cSAndreas Gohr    /**
72bb01c27cSAndreas Gohr     * Attach a file
73bb01c27cSAndreas Gohr     *
74a89c75afSAndreas Gohr     * @param string $path  Path to the file to attach
75a89c75afSAndreas Gohr     * @param string $mime  Mimetype of the attached file
76a89c75afSAndreas Gohr     * @param string $name The filename to use
77a89c75afSAndreas Gohr     * @param string $embed Unique key to reference this file from the HTML part
78bb01c27cSAndreas Gohr     */
79bb01c27cSAndreas Gohr    public function attachFile($path, $mime, $name = '', $embed = '') {
80bb01c27cSAndreas Gohr        if(!$name) {
813009a773SAndreas Gohr            $name = utf8_basename($path);
82bb01c27cSAndreas Gohr        }
83bb01c27cSAndreas Gohr
84bb01c27cSAndreas Gohr        $this->attach[] = array(
85bb01c27cSAndreas Gohr            'data'  => file_get_contents($path),
86bb01c27cSAndreas Gohr            'mime'  => $mime,
87bb01c27cSAndreas Gohr            'name'  => $name,
88bb01c27cSAndreas Gohr            'embed' => $embed
89bb01c27cSAndreas Gohr        );
90bb01c27cSAndreas Gohr    }
91bb01c27cSAndreas Gohr
92bb01c27cSAndreas Gohr    /**
93bb01c27cSAndreas Gohr     * Attach a file
94bb01c27cSAndreas Gohr     *
95a89c75afSAndreas Gohr     * @param string $data  The file contents to attach
96a89c75afSAndreas Gohr     * @param string $mime  Mimetype of the attached file
97a89c75afSAndreas Gohr     * @param string $name  The filename to use
98a89c75afSAndreas Gohr     * @param string $embed Unique key to reference this file from the HTML part
99bb01c27cSAndreas Gohr     */
100bb01c27cSAndreas Gohr    public function attachContent($data, $mime, $name = '', $embed = '') {
101bb01c27cSAndreas Gohr        if(!$name) {
102a89c75afSAndreas Gohr            list(, $ext) = explode('/', $mime);
103bb01c27cSAndreas Gohr            $name = count($this->attach).".$ext";
104bb01c27cSAndreas Gohr        }
105bb01c27cSAndreas Gohr
106bb01c27cSAndreas Gohr        $this->attach[] = array(
107bb01c27cSAndreas Gohr            'data'  => $data,
108bb01c27cSAndreas Gohr            'mime'  => $mime,
109bb01c27cSAndreas Gohr            'name'  => $name,
110bb01c27cSAndreas Gohr            'embed' => $embed
111bb01c27cSAndreas Gohr        );
112bb01c27cSAndreas Gohr    }
113bb01c27cSAndreas Gohr
114bb01c27cSAndreas Gohr    /**
115850dbf1fSAndreas Gohr     * Callback function to automatically embed images referenced in HTML templates
11642ea7f44SGerrit Uitslag     *
11742ea7f44SGerrit Uitslag     * @param array $matches
11842ea7f44SGerrit Uitslag     * @return string placeholder
119850dbf1fSAndreas Gohr     */
120850dbf1fSAndreas Gohr    protected function autoembed_cb($matches) {
121850dbf1fSAndreas Gohr        static $embeds = 0;
122850dbf1fSAndreas Gohr        $embeds++;
123850dbf1fSAndreas Gohr
124850dbf1fSAndreas Gohr        // get file and mime type
125850dbf1fSAndreas Gohr        $media = cleanID($matches[1]);
126a89c75afSAndreas Gohr        list(, $mime) = mimetype($media);
127850dbf1fSAndreas Gohr        $file = mediaFN($media);
128850dbf1fSAndreas Gohr        if(!file_exists($file)) return $matches[0]; //bad reference, keep as is
129850dbf1fSAndreas Gohr
130850dbf1fSAndreas Gohr        // attach it and set placeholder
131850dbf1fSAndreas Gohr        $this->attachFile($file, $mime, '', 'autoembed'.$embeds);
132850dbf1fSAndreas Gohr        return '%%autoembed'.$embeds.'%%';
133850dbf1fSAndreas Gohr    }
134850dbf1fSAndreas Gohr
135850dbf1fSAndreas Gohr    /**
1361d045709SAndreas Gohr     * Add an arbitrary header to the mail
1371d045709SAndreas Gohr     *
138a36fc348SAndreas Gohr     * If an empy value is passed, the header is removed
139a36fc348SAndreas Gohr     *
1401d045709SAndreas Gohr     * @param string $header the header name (no trailing colon!)
14159bc3b48SGerrit Uitslag     * @param string|string[] $value  the value of the header
1421d045709SAndreas Gohr     * @param bool   $clean  remove all non-ASCII chars and line feeds?
1431d045709SAndreas Gohr     */
1441d045709SAndreas Gohr    public function setHeader($header, $value, $clean = true) {
1459f3eca0bSAndreas Gohr        $header = str_replace(' ', '-', ucwords(strtolower(str_replace('-', ' ', $header)))); // streamline casing
1461d045709SAndreas Gohr        if($clean) {
147578b2c23SAndreas Gohr            $header = preg_replace('/[^a-zA-Z0-9_ \-\.\+\@]+/', '', $header);
148578b2c23SAndreas Gohr            $value  = preg_replace('/[^a-zA-Z0-9_ \-\.\+\@<>]+/', '', $value);
1491d045709SAndreas Gohr        }
150a36fc348SAndreas Gohr
151a36fc348SAndreas Gohr        // empty value deletes
152b6c97c70SAndreas Gohr        if(is_array($value)){
153b6c97c70SAndreas Gohr            $value = array_map('trim', $value);
154b6c97c70SAndreas Gohr            $value = array_filter($value);
155b6c97c70SAndreas Gohr            if(!$value) $value = '';
156b6c97c70SAndreas Gohr        }else{
157a36fc348SAndreas Gohr            $value = trim($value);
158b6c97c70SAndreas Gohr        }
159a36fc348SAndreas Gohr        if($value === '') {
160a36fc348SAndreas Gohr            if(isset($this->headers[$header])) unset($this->headers[$header]);
161a36fc348SAndreas Gohr        } else {
1621d045709SAndreas Gohr            $this->headers[$header] = $value;
1631d045709SAndreas Gohr        }
164a36fc348SAndreas Gohr    }
1651d045709SAndreas Gohr
1661d045709SAndreas Gohr    /**
1671d045709SAndreas Gohr     * Set additional parameters to be passed to sendmail
1681d045709SAndreas Gohr     *
1691d045709SAndreas Gohr     * Whatever is set here is directly passed to PHP's mail() command as last
1701d045709SAndreas Gohr     * parameter. Depending on the PHP setup this might break mailing alltogether
17142ea7f44SGerrit Uitslag     *
17242ea7f44SGerrit Uitslag     * @param string $param
1731d045709SAndreas Gohr     */
1741d045709SAndreas Gohr    public function setParameters($param) {
1751d045709SAndreas Gohr        $this->sendparam = $param;
1761d045709SAndreas Gohr    }
1771d045709SAndreas Gohr
1781d045709SAndreas Gohr    /**
179abbf0890SAndreas Gohr     * Set the text and HTML body and apply replacements
180abbf0890SAndreas Gohr     *
181abbf0890SAndreas Gohr     * This function applies a whole bunch of default replacements in addition
18204dcb5b2SChristopher Smith     * to the ones specified as parameters
183abbf0890SAndreas Gohr     *
184abbf0890SAndreas Gohr     * If you pass the HTML part or HTML replacements yourself you have to make
185abbf0890SAndreas Gohr     * sure you encode all HTML special chars correctly
186abbf0890SAndreas Gohr     *
187abbf0890SAndreas Gohr     * @param string $text     plain text body
188abbf0890SAndreas Gohr     * @param array  $textrep  replacements to apply on the text part
1899796f26fSSzymon Olewniczak     * @param array  $htmlrep  replacements to apply on the HTML part, null to use $textrep (with urls wrapped in <a> tags)
19059bc3b48SGerrit Uitslag     * @param string $html     the HTML body, leave null to create it from $text
191f08086ecSAndreas Gohr     * @param bool   $wrap     wrap the HTML in the default header/Footer
192abbf0890SAndreas Gohr     */
193f08086ecSAndreas Gohr    public function setBody($text, $textrep = null, $htmlrep = null, $html = null, $wrap = true) {
194585bf44eSChristopher Smith
19576efd6d0SAndreas Gohr        $htmlrep = (array)$htmlrep;
19676efd6d0SAndreas Gohr        $textrep = (array)$textrep;
197abbf0890SAndreas Gohr
198abbf0890SAndreas Gohr        // create HTML from text if not given
199abbf0890SAndreas Gohr        if(is_null($html)) {
200ba9c057bSAndreas Gohr            $html = $text;
201ba9c057bSAndreas Gohr            $html = hsc($html);
202ba2c2f17Sfurun            $html = preg_replace('/^----+$/m', '<hr >', $html);
203ba9c057bSAndreas Gohr            $html = nl2br($html);
204abbf0890SAndreas Gohr        }
205f08086ecSAndreas Gohr        if($wrap) {
2063819cafdSfurun            $wrap = rawLocale('mailwrap', 'html');
2073819cafdSfurun            $html = preg_replace('/\n-- <br \/>.*$/s', '', $html); //strip signature
2083819cafdSfurun            $html = str_replace('@EMAILSIGNATURE@', '', $html); //strip @EMAILSIGNATURE@
2093819cafdSfurun            $html = str_replace('@HTMLBODY@', $html, $wrap);
210f08086ecSAndreas Gohr        }
211f08086ecSAndreas Gohr
2123819cafdSfurun        if(strpos($text, '@EMAILSIGNATURE@') === false) {
2139ea45836SChristopher Smith            $text .= '@EMAILSIGNATURE@';
2143819cafdSfurun        }
215ba2c2f17Sfurun
21676efd6d0SAndreas Gohr        // copy over all replacements missing for HTML (autolink URLs)
21776efd6d0SAndreas Gohr        foreach($textrep as $key => $value) {
21876efd6d0SAndreas Gohr            if(isset($htmlrep[$key])) continue;
2193e7e6067SKlap-in            if(media_isexternal($value)) {
22076efd6d0SAndreas Gohr                $htmlrep[$key] = '<a href="'.hsc($value).'">'.hsc($value).'</a>';
22176efd6d0SAndreas Gohr            } else {
22276efd6d0SAndreas Gohr                $htmlrep[$key] = hsc($value);
22376efd6d0SAndreas Gohr            }
224abbf0890SAndreas Gohr        }
225abbf0890SAndreas Gohr
226850dbf1fSAndreas Gohr        // embed media from templates
227a89c75afSAndreas Gohr        $html = preg_replace_callback(
228a89c75afSAndreas Gohr            '/@MEDIA\(([^\)]+)\)@/',
229a89c75afSAndreas Gohr            array($this, 'autoembed_cb'), $html
230a89c75afSAndreas Gohr        );
231850dbf1fSAndreas Gohr
2329ea45836SChristopher Smith        // add default token replacements
2339ea45836SChristopher Smith        $trep = array_merge($this->replacements['text'], (array)$textrep);
2349ea45836SChristopher Smith        $hrep = array_merge($this->replacements['html'], (array)$htmlrep);
235abbf0890SAndreas Gohr
236abbf0890SAndreas Gohr        // Apply replacements
237abbf0890SAndreas Gohr        foreach($trep as $key => $substitution) {
238abbf0890SAndreas Gohr            $text = str_replace('@'.strtoupper($key).'@', $substitution, $text);
239abbf0890SAndreas Gohr        }
240abbf0890SAndreas Gohr        foreach($hrep as $key => $substitution) {
241abbf0890SAndreas Gohr            $html = str_replace('@'.strtoupper($key).'@', $substitution, $html);
242abbf0890SAndreas Gohr        }
243abbf0890SAndreas Gohr
244abbf0890SAndreas Gohr        $this->setHTML($html);
245abbf0890SAndreas Gohr        $this->setText($text);
246abbf0890SAndreas Gohr    }
247abbf0890SAndreas Gohr
248abbf0890SAndreas Gohr    /**
249bb01c27cSAndreas Gohr     * Set the HTML part of the mail
250bb01c27cSAndreas Gohr     *
251bb01c27cSAndreas Gohr     * Placeholders can be used to reference embedded attachments
252abbf0890SAndreas Gohr     *
253abbf0890SAndreas Gohr     * You probably want to use setBody() instead
25442ea7f44SGerrit Uitslag     *
25542ea7f44SGerrit Uitslag     * @param string $html
256bb01c27cSAndreas Gohr     */
2571d045709SAndreas Gohr    public function setHTML($html) {
258bb01c27cSAndreas Gohr        $this->html = $html;
259bb01c27cSAndreas Gohr    }
260bb01c27cSAndreas Gohr
261bb01c27cSAndreas Gohr    /**
262bb01c27cSAndreas Gohr     * Set the plain text part of the mail
263abbf0890SAndreas Gohr     *
264abbf0890SAndreas Gohr     * You probably want to use setBody() instead
26542ea7f44SGerrit Uitslag     *
26642ea7f44SGerrit Uitslag     * @param string $text
267bb01c27cSAndreas Gohr     */
2681d045709SAndreas Gohr    public function setText($text) {
269bb01c27cSAndreas Gohr        $this->text = $text;
270bb01c27cSAndreas Gohr    }
271bb01c27cSAndreas Gohr
272bb01c27cSAndreas Gohr    /**
273a36fc348SAndreas Gohr     * Add the To: recipients
274a36fc348SAndreas Gohr     *
2758c253612SGerrit Uitslag     * @see cleanAddress
27659bc3b48SGerrit Uitslag     * @param string|string[]  $address Multiple adresses separated by commas or as array
277a36fc348SAndreas Gohr     */
278a36fc348SAndreas Gohr    public function to($address) {
279a36fc348SAndreas Gohr        $this->setHeader('To', $address, false);
280a36fc348SAndreas Gohr    }
281a36fc348SAndreas Gohr
282a36fc348SAndreas Gohr    /**
283a36fc348SAndreas Gohr     * Add the Cc: recipients
284a36fc348SAndreas Gohr     *
2858c253612SGerrit Uitslag     * @see cleanAddress
28659bc3b48SGerrit Uitslag     * @param string|string[]  $address Multiple adresses separated by commas or as array
287a36fc348SAndreas Gohr     */
288a36fc348SAndreas Gohr    public function cc($address) {
289a36fc348SAndreas Gohr        $this->setHeader('Cc', $address, false);
290a36fc348SAndreas Gohr    }
291a36fc348SAndreas Gohr
292a36fc348SAndreas Gohr    /**
293a36fc348SAndreas Gohr     * Add the Bcc: recipients
294a36fc348SAndreas Gohr     *
2958c253612SGerrit Uitslag     * @see cleanAddress
29659bc3b48SGerrit Uitslag     * @param string|string[]  $address Multiple adresses separated by commas or as array
297a36fc348SAndreas Gohr     */
298a36fc348SAndreas Gohr    public function bcc($address) {
299a36fc348SAndreas Gohr        $this->setHeader('Bcc', $address, false);
300a36fc348SAndreas Gohr    }
301a36fc348SAndreas Gohr
302a36fc348SAndreas Gohr    /**
303a36fc348SAndreas Gohr     * Add the From: address
304a36fc348SAndreas Gohr     *
305a36fc348SAndreas Gohr     * This is set to $conf['mailfrom'] when not specified so you shouldn't need
306a36fc348SAndreas Gohr     * to call this function
307a36fc348SAndreas Gohr     *
3088c253612SGerrit Uitslag     * @see cleanAddress
309a36fc348SAndreas Gohr     * @param string  $address from address
310a36fc348SAndreas Gohr     */
311a36fc348SAndreas Gohr    public function from($address) {
312a36fc348SAndreas Gohr        $this->setHeader('From', $address, false);
313a36fc348SAndreas Gohr    }
314a36fc348SAndreas Gohr
315a36fc348SAndreas Gohr    /**
316a36fc348SAndreas Gohr     * Add the mail's Subject: header
317a36fc348SAndreas Gohr     *
318a36fc348SAndreas Gohr     * @param string $subject the mail subject
319a36fc348SAndreas Gohr     */
320a36fc348SAndreas Gohr    public function subject($subject) {
321a36fc348SAndreas Gohr        $this->headers['Subject'] = $subject;
322a36fc348SAndreas Gohr    }
323a36fc348SAndreas Gohr
324a36fc348SAndreas Gohr    /**
325102cdbd7SLarsGit223     * Return a clean name which can be safely used in mail address
326102cdbd7SLarsGit223     * fields. That means the name will be enclosed in '"' if it includes
327102cdbd7SLarsGit223     * a '"' or a ','. Also a '"' will be escaped as '\"'.
328102cdbd7SLarsGit223     *
329102cdbd7SLarsGit223     * @param string $name the name to clean-up
330102cdbd7SLarsGit223     * @see cleanAddress
331102cdbd7SLarsGit223     */
332102cdbd7SLarsGit223    public function getCleanName($name) {
333102cdbd7SLarsGit223        $name = trim($name, ' \t"');
334102cdbd7SLarsGit223        $name = str_replace('"', '\"', $name, $count);
335102cdbd7SLarsGit223        if ($count > 0 || strpos($name, ',') !== false) {
336102cdbd7SLarsGit223            $name = '"'.$name.'"';
337102cdbd7SLarsGit223        }
338102cdbd7SLarsGit223        return $name;
339102cdbd7SLarsGit223    }
340102cdbd7SLarsGit223
341102cdbd7SLarsGit223    /**
3421d045709SAndreas Gohr     * Sets an email address header with correct encoding
343bb01c27cSAndreas Gohr     *
344bb01c27cSAndreas Gohr     * Unicode characters will be deaccented and encoded base64
345bb01c27cSAndreas Gohr     * for headers. Addresses may not contain Non-ASCII data!
346bb01c27cSAndreas Gohr     *
347102cdbd7SLarsGit223     * If @$addresses is a string then it will be split into multiple
348102cdbd7SLarsGit223     * addresses. Addresses must be separated by a comma. If the display
349102cdbd7SLarsGit223     * name includes a comma then it MUST be properly enclosed by '"' to
350102cdbd7SLarsGit223     * prevent spliting at the wrong point.
351102cdbd7SLarsGit223     *
352bb01c27cSAndreas Gohr     * Example:
3538c253612SGerrit Uitslag     *   cc("föö <foo@bar.com>, me@somewhere.com","TBcc");
354102cdbd7SLarsGit223     *   to("foo, Dr." <foo@bar.com>, me@somewhere.com");
355bb01c27cSAndreas Gohr     *
35642ea7f44SGerrit Uitslag     * @param string|string[]  $addresses Multiple adresses separated by commas or as array
35742ea7f44SGerrit Uitslag     * @return false|string  the prepared header (can contain multiple lines)
358bb01c27cSAndreas Gohr     */
359b6c97c70SAndreas Gohr    public function cleanAddress($addresses) {
360bb01c27cSAndreas Gohr        $headers = '';
361b6c97c70SAndreas Gohr        if(!is_array($addresses)){
362*d31a1599SLarsGit223            $count = preg_match_all('/\s*(?:("[^"]*"[^,]+),*)|([^,]+)\s*,*/', $addresses, $matches, PREG_SET_ORDER);
363*d31a1599SLarsGit223            if ($count !== false && is_array($matches)) {
364102cdbd7SLarsGit223                $addresses = array();
365102cdbd7SLarsGit223                foreach ($matches as $match) {
366102cdbd7SLarsGit223                    array_push($addresses, $match[0]);
367102cdbd7SLarsGit223                }
368b6c97c70SAndreas Gohr            }
369*d31a1599SLarsGit223        }
370b6c97c70SAndreas Gohr
371b6c97c70SAndreas Gohr        foreach($addresses as $part) {
372b6c97c70SAndreas Gohr            $part = preg_replace('/[\r\n\0]+/', ' ', $part); // remove attack vectors
373bb01c27cSAndreas Gohr            $part = trim($part);
374bb01c27cSAndreas Gohr
375bb01c27cSAndreas Gohr            // parse address
376bb01c27cSAndreas Gohr            if(preg_match('#(.*?)<(.*?)>#', $part, $matches)) {
377bb01c27cSAndreas Gohr                $text = trim($matches[1]);
378bb01c27cSAndreas Gohr                $addr = $matches[2];
379bb01c27cSAndreas Gohr            } else {
380bb01c27cSAndreas Gohr                $addr = $part;
381bb01c27cSAndreas Gohr            }
382bb01c27cSAndreas Gohr            // skip empty ones
383bb01c27cSAndreas Gohr            if(empty($addr)) {
384bb01c27cSAndreas Gohr                continue;
385bb01c27cSAndreas Gohr            }
386bb01c27cSAndreas Gohr
387bb01c27cSAndreas Gohr            // FIXME: is there a way to encode the localpart of a emailaddress?
388bb01c27cSAndreas Gohr            if(!utf8_isASCII($addr)) {
38965cc1598SPhy                msg(hsc("E-Mail address <$addr> is not ASCII"), -1);
390bb01c27cSAndreas Gohr                continue;
391bb01c27cSAndreas Gohr            }
392bb01c27cSAndreas Gohr
39364d23c16SAndreas Gohr            if(!mail_isvalid($addr)) {
39465cc1598SPhy                msg(hsc("E-Mail address <$addr> is not valid"), -1);
395bb01c27cSAndreas Gohr                continue;
396bb01c27cSAndreas Gohr            }
397bb01c27cSAndreas Gohr
398bb01c27cSAndreas Gohr            // text was given
39930085ef3SYurii K            if(!empty($text) && !isWindows()) { // No named recipients for To: in Windows (see FS#652)
400bb01c27cSAndreas Gohr                // add address quotes
401bb01c27cSAndreas Gohr                $addr = "<$addr>";
402bb01c27cSAndreas Gohr
403bb01c27cSAndreas Gohr                if(defined('MAILHEADER_ASCIIONLY')) {
404bb01c27cSAndreas Gohr                    $text = utf8_deaccent($text);
405bb01c27cSAndreas Gohr                    $text = utf8_strip($text);
406bb01c27cSAndreas Gohr                }
407bb01c27cSAndreas Gohr
408b6c97c70SAndreas Gohr                if(strpos($text, ',') !== false || !utf8_isASCII($text)) {
409bb01c27cSAndreas Gohr                    $text = '=?UTF-8?B?'.base64_encode($text).'?=';
410bb01c27cSAndreas Gohr                }
411bb01c27cSAndreas Gohr            } else {
412bb01c27cSAndreas Gohr                $text = '';
413bb01c27cSAndreas Gohr            }
414bb01c27cSAndreas Gohr
415bb01c27cSAndreas Gohr            // add to header comma seperated
416bb01c27cSAndreas Gohr            if($headers != '') {
417bb01c27cSAndreas Gohr                $headers .= ', ';
418bb01c27cSAndreas Gohr            }
419bb01c27cSAndreas Gohr            $headers .= $text.' '.$addr;
420bb01c27cSAndreas Gohr        }
421bb01c27cSAndreas Gohr
422b6c97c70SAndreas Gohr        $headers = trim($headers);
423bb01c27cSAndreas Gohr        if(empty($headers)) return false;
424bb01c27cSAndreas Gohr
425bb01c27cSAndreas Gohr        return $headers;
426bb01c27cSAndreas Gohr    }
427bb01c27cSAndreas Gohr
428bb01c27cSAndreas Gohr
429bb01c27cSAndreas Gohr    /**
430bb01c27cSAndreas Gohr     * Prepare the mime multiparts for all attachments
431bb01c27cSAndreas Gohr     *
432bb01c27cSAndreas Gohr     * Replaces placeholders in the HTML with the correct CIDs
43342ea7f44SGerrit Uitslag     *
43442ea7f44SGerrit Uitslag     * @return string mime multiparts
435bb01c27cSAndreas Gohr     */
436bb01c27cSAndreas Gohr    protected function prepareAttachments() {
437bb01c27cSAndreas Gohr        $mime = '';
438bb01c27cSAndreas Gohr        $part = 1;
439bb01c27cSAndreas Gohr        // embedded attachments
440bb01c27cSAndreas Gohr        foreach($this->attach as $media) {
441ce9d2cc8SAndreas Gohr            $media['name'] = str_replace(':', '_', cleanID($media['name'], true));
442ce9d2cc8SAndreas Gohr
443bb01c27cSAndreas Gohr            // create content id
444bb01c27cSAndreas Gohr            $cid = 'part'.$part.'.'.$this->partid;
445bb01c27cSAndreas Gohr
446bb01c27cSAndreas Gohr            // replace wildcards
447bb01c27cSAndreas Gohr            if($media['embed']) {
448bb01c27cSAndreas Gohr                $this->html = str_replace('%%'.$media['embed'].'%%', 'cid:'.$cid, $this->html);
449bb01c27cSAndreas Gohr            }
450bb01c27cSAndreas Gohr
451bb01c27cSAndreas Gohr            $mime .= '--'.$this->boundary.MAILHEADER_EOL;
4521d8036c2SAndreas Gohr            $mime .= $this->wrappedHeaderLine('Content-Type', $media['mime'].'; id="'.$cid.'"');
4531d8036c2SAndreas Gohr            $mime .= $this->wrappedHeaderLine('Content-Transfer-Encoding', 'base64');
4541d8036c2SAndreas Gohr            $mime .= $this->wrappedHeaderLine('Content-ID',"<$cid>");
455bb01c27cSAndreas Gohr            if($media['embed']) {
4561d8036c2SAndreas Gohr                $mime .= $this->wrappedHeaderLine('Content-Disposition', 'inline; filename='.$media['name']);
457bb01c27cSAndreas Gohr            } else {
4581d8036c2SAndreas Gohr                $mime .= $this->wrappedHeaderLine('Content-Disposition', 'attachment; filename='.$media['name']);
459bb01c27cSAndreas Gohr            }
460bb01c27cSAndreas Gohr            $mime .= MAILHEADER_EOL; //end of headers
461bb01c27cSAndreas Gohr            $mime .= chunk_split(base64_encode($media['data']), 74, MAILHEADER_EOL);
462bb01c27cSAndreas Gohr
463bb01c27cSAndreas Gohr            $part++;
464bb01c27cSAndreas Gohr        }
465bb01c27cSAndreas Gohr        return $mime;
466bb01c27cSAndreas Gohr    }
467bb01c27cSAndreas Gohr
4681d045709SAndreas Gohr    /**
4691d045709SAndreas Gohr     * Build the body and handles multi part mails
4701d045709SAndreas Gohr     *
4711d045709SAndreas Gohr     * Needs to be called before prepareHeaders!
4721d045709SAndreas Gohr     *
4731d045709SAndreas Gohr     * @return string the prepared mail body, false on errors
4741d045709SAndreas Gohr     */
4751d045709SAndreas Gohr    protected function prepareBody() {
4761d045709SAndreas Gohr
4772398a2b5SAndreas Gohr        // no HTML mails allowed? remove HTML body
4782398a2b5SAndreas Gohr        if(!$this->allowhtml) {
4792398a2b5SAndreas Gohr            $this->html = '';
4802398a2b5SAndreas Gohr        }
4812398a2b5SAndreas Gohr
482bb01c27cSAndreas Gohr        // check for body
483bb01c27cSAndreas Gohr        if(!$this->text && !$this->html) {
484bb01c27cSAndreas Gohr            return false;
485bb01c27cSAndreas Gohr        }
486bb01c27cSAndreas Gohr
487bb01c27cSAndreas Gohr        // add general headers
488bb01c27cSAndreas Gohr        $this->headers['MIME-Version'] = '1.0';
489bb01c27cSAndreas Gohr
4901d045709SAndreas Gohr        $body = '';
4911d045709SAndreas Gohr
492bb01c27cSAndreas Gohr        if(!$this->html && !count($this->attach)) { // we can send a simple single part message
493bb01c27cSAndreas Gohr            $this->headers['Content-Type']              = 'text/plain; charset=UTF-8';
494bb01c27cSAndreas Gohr            $this->headers['Content-Transfer-Encoding'] = 'base64';
495be3cc6abSAndreas Gohr            $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL);
496bb01c27cSAndreas Gohr        } else { // multi part it is
4971d045709SAndreas Gohr            $body .= "This is a multi-part message in MIME format.".MAILHEADER_EOL;
498bb01c27cSAndreas Gohr
499bb01c27cSAndreas Gohr            // prepare the attachments
500bb01c27cSAndreas Gohr            $attachments = $this->prepareAttachments();
501bb01c27cSAndreas Gohr
502bb01c27cSAndreas Gohr            // do we have alternative text content?
503bb01c27cSAndreas Gohr            if($this->text && $this->html) {
504a36fc348SAndreas Gohr                $this->headers['Content-Type'] = 'multipart/alternative;'.MAILHEADER_EOL.
505a36fc348SAndreas Gohr                    '  boundary="'.$this->boundary.'XX"';
506bb01c27cSAndreas Gohr                $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL;
5071d045709SAndreas Gohr                $body .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL;
5081d045709SAndreas Gohr                $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL;
509bb01c27cSAndreas Gohr                $body .= MAILHEADER_EOL;
510be3cc6abSAndreas Gohr                $body .= chunk_split(base64_encode($this->text), 72, MAILHEADER_EOL);
511bb01c27cSAndreas Gohr                $body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL;
512a36fc348SAndreas Gohr                $body .= 'Content-Type: multipart/related;'.MAILHEADER_EOL.
513d6e04b60SAndreas Gohr                    '  boundary="'.$this->boundary.'";'.MAILHEADER_EOL.
514d6e04b60SAndreas Gohr                    '  type="text/html"'.MAILHEADER_EOL;
515bb01c27cSAndreas Gohr                $body .= MAILHEADER_EOL;
516bb01c27cSAndreas Gohr            }
517bb01c27cSAndreas Gohr
5181d045709SAndreas Gohr            $body .= '--'.$this->boundary.MAILHEADER_EOL;
5191d045709SAndreas Gohr            $body .= 'Content-Type: text/html; charset=UTF-8'.MAILHEADER_EOL;
5201d045709SAndreas Gohr            $body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL;
521bb01c27cSAndreas Gohr            $body .= MAILHEADER_EOL;
522be3cc6abSAndreas Gohr            $body .= chunk_split(base64_encode($this->html), 72, MAILHEADER_EOL);
523bb01c27cSAndreas Gohr            $body .= MAILHEADER_EOL;
524bb01c27cSAndreas Gohr            $body .= $attachments;
525bb01c27cSAndreas Gohr            $body .= '--'.$this->boundary.'--'.MAILHEADER_EOL;
526bb01c27cSAndreas Gohr
527bb01c27cSAndreas Gohr            // close open multipart/alternative boundary
528bb01c27cSAndreas Gohr            if($this->text && $this->html) {
529bb01c27cSAndreas Gohr                $body .= '--'.$this->boundary.'XX--'.MAILHEADER_EOL;
530bb01c27cSAndreas Gohr            }
531bb01c27cSAndreas Gohr        }
532bb01c27cSAndreas Gohr
533bb01c27cSAndreas Gohr        return $body;
534bb01c27cSAndreas Gohr    }
535bb01c27cSAndreas Gohr
536bb01c27cSAndreas Gohr    /**
537a36fc348SAndreas Gohr     * Cleanup and encode the headers array
538a36fc348SAndreas Gohr     */
539a36fc348SAndreas Gohr    protected function cleanHeaders() {
540a36fc348SAndreas Gohr        global $conf;
541a36fc348SAndreas Gohr
542a36fc348SAndreas Gohr        // clean up addresses
543a36fc348SAndreas Gohr        if(empty($this->headers['From'])) $this->from($conf['mailfrom']);
544acbf061cSGerrit Uitslag        $addrs = array('To', 'From', 'Cc', 'Bcc', 'Reply-To', 'Sender');
545a36fc348SAndreas Gohr        foreach($addrs as $addr) {
546a36fc348SAndreas Gohr            if(isset($this->headers[$addr])) {
547a36fc348SAndreas Gohr                $this->headers[$addr] = $this->cleanAddress($this->headers[$addr]);
548a36fc348SAndreas Gohr            }
549a36fc348SAndreas Gohr        }
550a36fc348SAndreas Gohr
55145992a63SAndreas Gohr        if(isset($this->headers['Subject'])) {
552a36fc348SAndreas Gohr            // add prefix to subject
55354f30755SAndreas Gohr            if(empty($conf['mailprefix'])) {
5548a215f09SAndreas Gohr                if(utf8_strlen($conf['title']) < 20) {
55554f30755SAndreas Gohr                    $prefix = '['.$conf['title'].']';
55654f30755SAndreas Gohr                } else {
5578a215f09SAndreas Gohr                    $prefix = '['.utf8_substr($conf['title'], 0, 20).'...]';
5588a215f09SAndreas Gohr                }
5598a215f09SAndreas Gohr            } else {
560a36fc348SAndreas Gohr                $prefix = '['.$conf['mailprefix'].']';
56154f30755SAndreas Gohr            }
562a36fc348SAndreas Gohr            $len = strlen($prefix);
56345992a63SAndreas Gohr            if(substr($this->headers['Subject'], 0, $len) != $prefix) {
56445992a63SAndreas Gohr                $this->headers['Subject'] = $prefix.' '.$this->headers['Subject'];
565a36fc348SAndreas Gohr            }
566a36fc348SAndreas Gohr
567a36fc348SAndreas Gohr            // encode subject
568a36fc348SAndreas Gohr            if(defined('MAILHEADER_ASCIIONLY')) {
56945992a63SAndreas Gohr                $this->headers['Subject'] = utf8_deaccent($this->headers['Subject']);
57045992a63SAndreas Gohr                $this->headers['Subject'] = utf8_strip($this->headers['Subject']);
571a36fc348SAndreas Gohr            }
572a36fc348SAndreas Gohr            if(!utf8_isASCII($this->headers['Subject'])) {
57345992a63SAndreas Gohr                $this->headers['Subject'] = '=?UTF-8?B?'.base64_encode($this->headers['Subject']).'?=';
574a36fc348SAndreas Gohr            }
575a36fc348SAndreas Gohr        }
576a36fc348SAndreas Gohr
577a36fc348SAndreas Gohr    }
5781d8036c2SAndreas Gohr
5791d8036c2SAndreas Gohr    /**
5801d8036c2SAndreas Gohr     * Returns a complete, EOL terminated header line, wraps it if necessary
5811d8036c2SAndreas Gohr     *
58242ea7f44SGerrit Uitslag     * @param string $key
58342ea7f44SGerrit Uitslag     * @param string $val
58442ea7f44SGerrit Uitslag     * @return string line
5851d8036c2SAndreas Gohr     */
5861d8036c2SAndreas Gohr    protected function wrappedHeaderLine($key, $val){
5871d8036c2SAndreas Gohr        return wordwrap("$key: $val", 78, MAILHEADER_EOL.'  ').MAILHEADER_EOL;
588a36fc348SAndreas Gohr    }
589a36fc348SAndreas Gohr
590a36fc348SAndreas Gohr    /**
591bb01c27cSAndreas Gohr     * Create a string from the headers array
5921d045709SAndreas Gohr     *
5931d045709SAndreas Gohr     * @returns string the headers
594bb01c27cSAndreas Gohr     */
595bb01c27cSAndreas Gohr    protected function prepareHeaders() {
596bb01c27cSAndreas Gohr        $headers = '';
597bb01c27cSAndreas Gohr        foreach($this->headers as $key => $val) {
5986be717dbSMichael Hamann            if ($val === '' || is_null($val)) continue;
5991d8036c2SAndreas Gohr            $headers .= $this->wrappedHeaderLine($key, $val);
600bb01c27cSAndreas Gohr        }
601bb01c27cSAndreas Gohr        return $headers;
602bb01c27cSAndreas Gohr    }
603bb01c27cSAndreas Gohr
604bb01c27cSAndreas Gohr    /**
605bb01c27cSAndreas Gohr     * return a full email with all headers
606bb01c27cSAndreas Gohr     *
6071d045709SAndreas Gohr     * This is mainly intended for debugging and testing but could also be
6081d045709SAndreas Gohr     * used for MHT exports
6091d045709SAndreas Gohr     *
6101d045709SAndreas Gohr     * @return string the mail, false on errors
611bb01c27cSAndreas Gohr     */
612bb01c27cSAndreas Gohr    public function dump() {
613a36fc348SAndreas Gohr        $this->cleanHeaders();
614bb01c27cSAndreas Gohr        $body = $this->prepareBody();
6154d18e936SAndreas Gohr        if($body === false) return false;
6161d045709SAndreas Gohr        $headers = $this->prepareHeaders();
617bb01c27cSAndreas Gohr
618bb01c27cSAndreas Gohr        return $headers.MAILHEADER_EOL.$body;
619bb01c27cSAndreas Gohr    }
6201d045709SAndreas Gohr
6211d045709SAndreas Gohr    /**
6229ea45836SChristopher Smith     * Prepare default token replacement strings
6239ea45836SChristopher Smith     *
6249ea45836SChristopher Smith     * Populates the '$replacements' property.
6259ea45836SChristopher Smith     * Should be called by the class constructor
6269ea45836SChristopher Smith     */
6279ea45836SChristopher Smith    protected function prepareTokenReplacements() {
6289ea45836SChristopher Smith        global $INFO;
6299ea45836SChristopher Smith        global $conf;
6309ea45836SChristopher Smith        /* @var Input $INPUT */
6319ea45836SChristopher Smith        global $INPUT;
6329ea45836SChristopher Smith        global $lang;
6339ea45836SChristopher Smith
6349ea45836SChristopher Smith        $ip   = clientIP();
6359ea45836SChristopher Smith        $cip  = gethostsbyaddrs($ip);
6369ea45836SChristopher Smith
6379ea45836SChristopher Smith        $this->replacements['text'] = array(
6389ea45836SChristopher Smith            'DATE' => dformat(),
6399ea45836SChristopher Smith            'BROWSER' => $INPUT->server->str('HTTP_USER_AGENT'),
6409ea45836SChristopher Smith            'IPADDRESS' => $ip,
6419ea45836SChristopher Smith            'HOSTNAME' => $cip,
6429ea45836SChristopher Smith            'TITLE' => $conf['title'],
6439ea45836SChristopher Smith            'DOKUWIKIURL' => DOKU_URL,
6449ea45836SChristopher Smith            'USER' => $INPUT->server->str('REMOTE_USER'),
6459ea45836SChristopher Smith            'NAME' => $INFO['userinfo']['name'],
646774514c9SGerrit Uitslag            'MAIL' => $INFO['userinfo']['mail']
6479ea45836SChristopher Smith        );
648774514c9SGerrit Uitslag        $signature = str_replace('@DOKUWIKIURL@', $this->replacements['text']['DOKUWIKIURL'], $lang['email_signature_text']);
649774514c9SGerrit Uitslag        $this->replacements['text']['EMAILSIGNATURE'] = "\n-- \n" . $signature . "\n";
6509ea45836SChristopher Smith
6519ea45836SChristopher Smith        $this->replacements['html'] = array(
6529ea45836SChristopher Smith            'DATE' => '<i>' . hsc(dformat()) . '</i>',
6539ea45836SChristopher Smith            'BROWSER' => hsc($INPUT->server->str('HTTP_USER_AGENT')),
6549ea45836SChristopher Smith            'IPADDRESS' => '<code>' . hsc($ip) . '</code>',
6559ea45836SChristopher Smith            'HOSTNAME' => '<code>' . hsc($cip) . '</code>',
6569ea45836SChristopher Smith            'TITLE' => hsc($conf['title']),
6579ea45836SChristopher Smith            'DOKUWIKIURL' => '<a href="' . DOKU_URL . '">' . DOKU_URL . '</a>',
6589ea45836SChristopher Smith            'USER' => hsc($INPUT->server->str('REMOTE_USER')),
6599ea45836SChristopher Smith            'NAME' => hsc($INFO['userinfo']['name']),
6609ea45836SChristopher Smith            'MAIL' => '<a href="mailto:"' . hsc($INFO['userinfo']['mail']) . '">' .
661774514c9SGerrit Uitslag                hsc($INFO['userinfo']['mail']) . '</a>'
6629ea45836SChristopher Smith        );
663774514c9SGerrit Uitslag        $signature = $lang['email_signature_text'];
664774514c9SGerrit Uitslag        if(!empty($lang['email_signature_html'])) {
665774514c9SGerrit Uitslag            $signature = $lang['email_signature_html'];
666774514c9SGerrit Uitslag        }
667774514c9SGerrit Uitslag        $signature = str_replace(
668774514c9SGerrit Uitslag            array(
669774514c9SGerrit Uitslag                '@DOKUWIKIURL@',
670774514c9SGerrit Uitslag                "\n"
671774514c9SGerrit Uitslag            ),
672774514c9SGerrit Uitslag            array(
673774514c9SGerrit Uitslag                $this->replacements['html']['DOKUWIKIURL'],
674774514c9SGerrit Uitslag                '<br />'
675774514c9SGerrit Uitslag            ),
676774514c9SGerrit Uitslag            $signature
677774514c9SGerrit Uitslag        );
678774514c9SGerrit Uitslag        $this->replacements['html']['EMAILSIGNATURE'] = $signature;
6799ea45836SChristopher Smith    }
6809ea45836SChristopher Smith
6819ea45836SChristopher Smith    /**
6821d045709SAndreas Gohr     * Send the mail
6831d045709SAndreas Gohr     *
6841d045709SAndreas Gohr     * Call this after all data was set
6851d045709SAndreas Gohr     *
68628d2ad80SAndreas Gohr     * @triggers MAIL_MESSAGE_SEND
6871d045709SAndreas Gohr     * @return bool true if the mail was successfully passed to the MTA
6881d045709SAndreas Gohr     */
6891d045709SAndreas Gohr    public function send() {
69028d2ad80SAndreas Gohr        $success = false;
691a36fc348SAndreas Gohr
69228d2ad80SAndreas Gohr        // prepare hook data
69328d2ad80SAndreas Gohr        $data = array(
69428d2ad80SAndreas Gohr            // pass the whole mail class to plugin
69528d2ad80SAndreas Gohr            'mail'    => $this,
69628d2ad80SAndreas Gohr            // pass references for backward compatibility
69728d2ad80SAndreas Gohr            'to'      => &$this->headers['To'],
69828d2ad80SAndreas Gohr            'cc'      => &$this->headers['Cc'],
69928d2ad80SAndreas Gohr            'bcc'     => &$this->headers['Bcc'],
70028d2ad80SAndreas Gohr            'from'    => &$this->headers['From'],
70128d2ad80SAndreas Gohr            'subject' => &$this->headers['Subject'],
70228d2ad80SAndreas Gohr            'body'    => &$this->text,
703a89c75afSAndreas Gohr            'params'  => &$this->sendparam,
70428d2ad80SAndreas Gohr            'headers' => '', // plugins shouldn't use this
70528d2ad80SAndreas Gohr            // signal if we mailed successfully to AFTER event
70628d2ad80SAndreas Gohr            'success' => &$success,
70728d2ad80SAndreas Gohr        );
70828d2ad80SAndreas Gohr
70928d2ad80SAndreas Gohr        // do our thing if BEFORE hook approves
71028d2ad80SAndreas Gohr        $evt = new Doku_Event('MAIL_MESSAGE_SEND', $data);
71128d2ad80SAndreas Gohr        if($evt->advise_before(true)) {
71228d2ad80SAndreas Gohr            // clean up before using the headers
713a36fc348SAndreas Gohr            $this->cleanHeaders();
714a36fc348SAndreas Gohr
7151d045709SAndreas Gohr            // any recipients?
7161d045709SAndreas Gohr            if(trim($this->headers['To']) === '' &&
7171d045709SAndreas Gohr                trim($this->headers['Cc']) === '' &&
718a89c75afSAndreas Gohr                trim($this->headers['Bcc']) === ''
719a89c75afSAndreas Gohr            ) return false;
7201d045709SAndreas Gohr
7211d045709SAndreas Gohr            // The To: header is special
7226be717dbSMichael Hamann            if(array_key_exists('To', $this->headers)) {
7236be717dbSMichael Hamann                $to = (string)$this->headers['To'];
7241d045709SAndreas Gohr                unset($this->headers['To']);
7251d045709SAndreas Gohr            } else {
7261d045709SAndreas Gohr                $to = '';
7271d045709SAndreas Gohr            }
7281d045709SAndreas Gohr
7291d045709SAndreas Gohr            // so is the subject
7306be717dbSMichael Hamann            if(array_key_exists('Subject', $this->headers)) {
7316be717dbSMichael Hamann                $subject = (string)$this->headers['Subject'];
7321d045709SAndreas Gohr                unset($this->headers['Subject']);
7331d045709SAndreas Gohr            } else {
7341d045709SAndreas Gohr                $subject = '';
7351d045709SAndreas Gohr            }
7361d045709SAndreas Gohr
7371d045709SAndreas Gohr            // make the body
7381d045709SAndreas Gohr            $body = $this->prepareBody();
7394c89a7f6SAndreas Gohr            if($body === false) return false;
7401d045709SAndreas Gohr
7411d045709SAndreas Gohr            // cook the headers
7421d045709SAndreas Gohr            $headers = $this->prepareHeaders();
74328d2ad80SAndreas Gohr            // add any headers set by legacy plugins
74428d2ad80SAndreas Gohr            if(trim($data['headers'])) {
74528d2ad80SAndreas Gohr                $headers .= MAILHEADER_EOL.trim($data['headers']);
74628d2ad80SAndreas Gohr            }
7471d045709SAndreas Gohr
7481d045709SAndreas Gohr            // send the thing
7491d045709SAndreas Gohr            if(is_null($this->sendparam)) {
75028d2ad80SAndreas Gohr                $success = @mail($to, $subject, $body, $headers);
7511d045709SAndreas Gohr            } else {
75228d2ad80SAndreas Gohr                $success = @mail($to, $subject, $body, $headers, $this->sendparam);
7531d045709SAndreas Gohr            }
7541d045709SAndreas Gohr        }
75528d2ad80SAndreas Gohr        // any AFTER actions?
75628d2ad80SAndreas Gohr        $evt->advise_after();
75728d2ad80SAndreas Gohr        return $success;
75828d2ad80SAndreas Gohr    }
759bb01c27cSAndreas Gohr}
760