source: trunk/lib/Email.inc.php @ 648

Last change on this file since 648 was 630, checked in by anonymous, 6 years ago

Disable App::sslOn(). Better logging on Email::send() unreplaced variables. Fix the elusive 'Database table session_tbl has invalid columns' error.

File size: 20.4 KB
RevLine 
[23]1<?php
2/**
[362]3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
[396]5 * Copyright 2001-2012 Strangecode, LLC
[454]6 *
[362]7 * This file is part of The Strangecode Codebase.
8 *
9 * The Strangecode Codebase is free software: you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your option)
12 * any later version.
[454]13 *
[362]14 * The Strangecode Codebase is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
[454]18 *
[362]19 * You should have received a copy of the GNU General Public License along with
20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
21 */
22
[460]23/*
24* Email.inc.php
25*
26* Easy email template usage.
27*
28* @author  Quinn Comendant <quinn@strangecode.com>
29* @version 1.0
30*
31* Example of use:
32---------------------------------------------------------------------
33// Setup email object.
[23]34$email = new Email(array(
35    'to' => array($frm['email'], 'q@lovemachine.local'),
[550]36    'from' => sprintf('"%s" <%s>', addcslashes($app->getParam('site_name'), '"'), $app->getParam('site_email')),
[24]37    'subject' => 'Your account has been activated',
[23]38));
39$email->setTemplate('email_registration_confirm.ihtml');
[41]40// $email->setString('Or you can pass your message body as a string, also with {VARIABLES}.');
[23]41$email->replace(array(
[136]42    'site_name' => $app->getParam('site_name'),
43    'site_url' => $app->getParam('site_url'),
[23]44    'username' => $frm['username'],
45    'password' => $frm['password1'],
46));
47if ($email->send()) {
[136]48    $app->raiseMsg(sprintf(_("A confirmation email has been sent to %s."), $frm['email']), MSG_SUCCESS, __FILE__, __LINE__);
[23]49} else {
[136]50    $app->logMsg(sprintf('Error sending confirmation email to address %s', $frm['email']), LOG_NOTICE, __FILE__, __LINE__);
[23]51}
[460]52---------------------------------------------------------------------
53*/
[136]54
[502]55class Email
56{
[23]57
[24]58    // Default parameters, to be overwritten by setParam() and read with getParam()
[484]59    protected $_params = array(
[23]60        'to' => null,
61        'from' => null,
62        'subject' => null,
[38]63        'headers' => null,
[490]64        'envelope_sender_address' => null, // AKA the bounce-to address. Will default to 'from' if left null.
[212]65        'regex' => null,
[454]66
[212]67        // A single carriage return (\n) should terminate lines for locally injected mail.
68        // A carriage return + line-feed (\r\n) should be used if sending mail directly with SMTP.
69        'crlf' => "\n",
[454]70
[212]71        // RFC 2822 says line length MUST be no more than 998 characters, and SHOULD be no more than 78 characters, excluding the CRLF.
72        // http://mailformat.dan.info/body/linelength.html
73        'wrap' => true,
74        'line_length' => 75,
[622]75
76        'sandbox_mode' => null,
77        'sandbox_to_addr' => null,
[23]78    );
[42]79
[630]80    // String that contains filename of the template used (for logging).
81    protected $_template_filename;
82
[24]83    // String that contains the email body.
[484]84    protected $_template;
[42]85
[24]86    // String that contains the email body after replacements.
[484]87    protected $_template_replaced;
[23]88
[622]89    // Email debug modes.
90    const SANDBOX_MODE_REDIRECT = 1; // Send all mail to 'sandbox_to_addr'
91    const SANDBOX_MODE_STDERR = 2; // Log all mail to stderr
[628]92    const SANDBOX_MODE_LOG = 3; // Log all mail using $app->logMsg(
)
[622]93
[23]94    /**
95     * Constructor.
96     *
97     * @access  public
98     * @param   array   $params     Array of object parameters.
99     * @author  Quinn Comendant <quinn@strangecode.com>
100     * @since   28 Nov 2005 12:59:41
101     */
[468]102    public function __construct($params=null)
[23]103    {
[334]104        // The regex used in validEmail(). Set here instead of in the default _params above so we can use the concatenation . dot.
[357]105        // This matches a (valid) email address as complex as:
[399]106        //      "Jane & Bob Smith" <bob&smith's/dep=sales!@smith-wick.ca.us > (Sales department)
[24]107        // ...and something as simple as:
108        //      x@x.com
[399]109        $this->setParam(array('regex' => '/^(?:(?:"[^"]*?"\s*|[^,@]*)(<\s*)|(?:"[^"]*?"|[^,@]*)\s+|)'   // Display name
[357]110        . '((?:[^.<>\s@",\[\]]+[^<>\s@",\[\]])*[^.<>\s@",\[\]]+)'       // Local-part
[23]111        . '@'                                                           // @
112        . '((?:(\[)|[A-Z0-9]?)'                                         // Domain, first char
113        . '(?(4)'                                                       // Domain conditional for if first domain char is [
114        . '(?:[0-9]{1,3}\.){3}[0-9]{1,3}\]'                             // TRUE, matches IP address
115        . '|'
116        . '[.-]?(?:[A-Z0-9]+[-.])*(?:[A-Z0-9]+\.)+[A-Z]{2,6}))'         // FALSE, matches domain name
117        . '(?(1)'                                                       // Comment conditional for if initial < exists
[490]118        . '(?:\s*>\s*|>\s+\([^,@]+\)\s*)'                               // TRUE, ensure ending >
[23]119        . '|'
120        . '(?:|\s*|\s+\([^,@]+\)\s*))$/i'));                            // FALSE ensure there is no ending >
121
[25]122        if (isset($params)) {
123            $this->setParam($params);
124        }
[23]125    }
126
127    /**
128     * Set (or overwrite existing) parameters by passing an array of new parameters.
129     *
130     * @access public
131     * @param  array    $params     Array of parameters (key => val pairs).
132     */
[468]133    public function setParam($params)
[23]134    {
[479]135        $app =& App::getInstance();
[454]136
[23]137        if (isset($params) && is_array($params)) {
138            // Enforce valid email addresses.
139            if (isset($params['to']) && !$this->validEmail($params['to'])) {
140                $params['to'] = null;
141            }
142            if (isset($params['from']) && !$this->validEmail($params['from'])) {
143                $params['from'] = null;
144            }
[490]145            if (isset($params['envelope_sender_address']) && !$this->validEmail($params['envelope_sender_address'])) {
146                $params['envelope_sender_address'] = null;
147            }
[23]148
149            // Merge new parameters with old overriding only those passed.
150            $this->_params = array_merge($this->_params, $params);
151        } else {
[136]152            $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);
[23]153        }
154    }
155
156    /**
157     * Return the value of a parameter, if it exists.
158     *
159     * @access public
160     * @param string $param        Which parameter to return.
161     * @return mixed               Configured parameter value.
162     */
[468]163    public function getParam($param)
[23]164    {
[479]165        $app =& App::getInstance();
[454]166
[478]167        if (array_key_exists($param, $this->_params)) {
[23]168            return $this->_params[$param];
169        } else {
[146]170            $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
[23]171            return null;
172        }
173    }
174
175    /**
176     * Loads template from file to generate email body.
177     *
178     * @access  public
179     * @param   string  $template   Filename of email template.
180     * @author  Quinn Comendant <quinn@strangecode.com>
181     * @since   28 Nov 2005 12:56:23
182     */
[468]183    public function setTemplate($template)
[23]184    {
[479]185        $app =& App::getInstance();
[454]186
[23]187        // Load file, using include_path.
188        if (!$this->_template = file_get_contents($template, true)) {
[136]189            $app->logMsg(sprintf('Email template file does not exist: %s', $template), LOG_ERR, __FILE__, __LINE__);
[23]190            $this->_template = null;
191            $this->_template_replaced = null;
192            return false;
193        }
[578]194
195        // Ensure template is UTF-8.
196        $detected_encoding = mb_detect_encoding($this->_template, array('UTF-8', 'ISO-8859-1', 'WINDOWS-1252'), true);
197        if ('UTF-8' != strtoupper($detected_encoding)) {
198            $this->_template = mb_convert_encoding($this->_template, 'UTF-8', $detected_encoding);
199        }
200
[24]201        // This could be a new template, so reset the _template_replaced.
[23]202        $this->_template_replaced = null;
[630]203
204        $this->_template_filename = $template;
205
[23]206        return true;
207    }
208
209    /**
210     * Loads template from string to generate email body.
211     *
212     * @access  public
213     * @param   string  $template   Filename of email template.
214     * @author  Quinn Comendant <quinn@strangecode.com>
215     * @since   28 Nov 2005 12:56:23
216     */
[468]217    public function setString($string)
[23]218    {
[479]219        $app =& App::getInstance();
[454]220
[41]221        if ('' == trim($string)) {
[136]222            $app->logMsg(sprintf('Empty string provided.', null), LOG_ERR, __FILE__, __LINE__);
[23]223            $this->_template_replaced = null;
224            return false;
225        } else {
[41]226            $this->_template = $string;
[24]227            // This could be a new template, so reset the _template_replaced.
[23]228            $this->_template_replaced = null;
[630]229
230            $this->_template_filename = '(using Email::setString)';
231
[23]232            return true;
233        }
234    }
[42]235
[23]236    /**
237     * Replace variables in template with argument data.
238     *
239     * @access  public
240     * @param   array   $replacements   Array keys are the values to search for, array vales are the replacement values.
241     * @author  Quinn Comendant <quinn@strangecode.com>
242     * @since   28 Nov 2005 13:08:51
243     */
[468]244    public function replace($replacements)
[23]245    {
[479]246        $app =& App::getInstance();
[454]247
[24]248        // Ensure template exists.
[23]249        if (!isset($this->_template)) {
[136]250            $app->logMsg(sprintf('Cannot replace variables, no template defined.', null), LOG_ERR, __FILE__, __LINE__);
[42]251            return false;
[23]252        }
[42]253
[24]254        // Ensure replacements argument is an array.
[23]255        if (!is_array($replacements)) {
[136]256            $app->logMsg(sprintf('Cannot replace variables, invalid replacements.', null), LOG_ERR, __FILE__, __LINE__);
[42]257            return false;
[23]258        }
[42]259
[23]260        // Apply regex pattern to search elements.
261        $search = array_keys($replacements);
[247]262        array_walk($search, create_function('&$v', '$v = "{" . mb_strtoupper($v) . "}";'));
[23]263
[35]264        // Replacement values.
[23]265        $replace = array_values($replacements);
[42]266
[24]267        // Search and replace all values at once.
[216]268        $this->_template_replaced = str_replace($search, $replace, $this->_template);
[23]269    }
270
[502]271    /*
272    * Returns the body of the current email. This can be used to store the message that is being sent.
273    * It will use the original template, or the replaced template if it has been processed.
[618]274    * You can also use this function to do post-processing on the email body before sending it,
275    * like removing extraneous lines:
276    * $email->setString(preg_replace('/(?:(?:\r\n|\r|\n)\s*){2}/s', "\n\n", $email->getBody()));
[502]277    *
278    * @access   public
279    * @return   string  Message body.
280    * @author   Quinn Comendant <quinn@strangecode.com>
281    * @version  1.0
282    * @since    18 Nov 2014 21:15:19
283    */
284    public function getBody()
285    {
[559]286        $app =& App::getInstance();
287
[502]288        $final_body = isset($this->_template_replaced) ? $this->_template_replaced : $this->_template;
289        // Ensure all placeholders have been replaced. Find anything with {...} characters.
290        if (preg_match('/({[^}]+})/', $final_body, $unreplaced_match)) {
291            unset($unreplaced_match[0]);
[630]292            $app->logMsg(sprintf('Cannot get email body. Unreplaced variable %s in template %s', getDump($unreplaced_match)), LOG_ERR, __FILE__, __LINE__);
[502]293            return false;
294        }
295        return $final_body;
296    }
297
[23]298    /**
299     * Send email using PHP's mail() function.
300     *
301     * @access  public
302     * @param   string  $to
303     * @param   string  $from
304     * @param   string  $subject
305     * @author  Quinn Comendant <quinn@strangecode.com>
306     * @since   28 Nov 2005 12:56:09
307     */
[468]308    public function send($to=null, $from=null, $subject=null, $headers=null)
[23]309    {
[479]310        $app =& App::getInstance();
[282]311
[24]312        // Use arguments if provided.
[23]313        if (isset($to)) {
[490]314            $this->setParam(array('to' => $to));
[23]315        }
316        if (isset($from)) {
[490]317            $this->setParam(array('from' => $from));
[23]318        }
319        if (isset($subject)) {
[490]320            $this->setParam(array('subject' => $subject));
[23]321        }
[38]322        if (isset($headers)) {
[490]323            $this->setParam(array('headers' => $headers));
[38]324        }
[36]325
[24]326        // Ensure required values exist.
[43]327        if (!isset($this->_params['subject'])) {
[364]328            $app->logMsg('Cannot send email. SUBJECT not defined.', LOG_ERR, __FILE__, __LINE__);
[23]329            return false;
[43]330        } else if (!isset($this->_template)) {
[136]331            $app->logMsg(sprintf('Cannot send email: "%s". Template not set.', $this->_params['subject']), LOG_ERR, __FILE__, __LINE__);
[43]332            return false;
[23]333        } else if (!isset($this->_params['to'])) {
[136]334            $app->logMsg(sprintf('Cannot send email: "%s". TO not defined.', $this->_params['subject']), LOG_NOTICE, __FILE__, __LINE__);
[23]335            return false;
336        } else if (!isset($this->_params['from'])) {
[136]337            $app->logMsg(sprintf('Cannot send email: "%s". FROM not defined.', $this->_params['subject']), LOG_ERR, __FILE__, __LINE__);
[23]338            return false;
339        }
340
[24]341        // Wrap email text body, using _template_replaced if replacements have been used, or just a fresh _template if not.
[212]342        $final_body = isset($this->_template_replaced) ? $this->_template_replaced : $this->_template;
343        if (false !== $this->getParam('wrap')) {
[454]344            $final_body = wordwrap($final_body, $this->getParam('line_length'), $this->getParam('crlf'));
[212]345        }
[24]346
347        // Ensure all placeholders have been replaced. Find anything with {...} characters.
[23]348        if (preg_match('/({[^}]+})/', $final_body, $unreplaced_match)) {
[502]349            unset($unreplaced_match[0]);
[630]350            $app->logMsg(sprintf('Unreplaced variable "%s" in template "%s"', getDump($unreplaced_match), $this->_template_filename), LOG_ERR, __FILE__, __LINE__);
[23]351        }
[42]352
[24]353        // Final "to" header can have multiple addresses if in an array.
[23]354        $final_to = is_array($this->_params['to']) ? join(', ', $this->_params['to']) : $this->_params['to'];
[42]355
[24]356        // From headers are custom headers.
[38]357        $headers = array('From' => $this->_params['from']);
358
359        // Additional headers.
360        if (isset($this->_params['headers']) && is_array($this->_params['headers'])) {
361            $headers = array_merge($this->_params['headers'], $headers);
362        }
[42]363
[38]364        // Process headers.
365        $final_headers = array();
366        foreach ($headers as $key => $val) {
[490]367            // Validate key and values.
[500]368            if (empty($val)) {
369                $app->logMsg(sprintf('Empty email header provided: %s', $key), LOG_DEBUG, __FILE__, __LINE__);
[490]370                continue;
[424]371            }
[500]372            if (empty($key) || !is_string($key) || !is_string($val) || preg_match("/[\n\r]/", $key . $val) || preg_match('/[^\w-]/', $key)) {
373                $app->logMsg(sprintf('Broken email header provided: %s=%s', $key, $val), LOG_WARNING, __FILE__, __LINE__);
374                continue;
375            }
[490]376            // If the envelope_sender_address was given as a header, move it to the correct place.
[622]377            if ('envelope_sender_address' == strtolower($key)) {
[490]378                $this->_params['envelope_sender_address'] = isset($this->_params['envelope_sender_address']) ? $this->_params['envelope_sender_address'] : $val;
379                continue;
380            }
[622]381            // If we're sending in sandbox mode, remove any headers with recipient addresses.
382            if ($this->getParam('sandbox_mode') == self::SANDBOX_MODE_REDIRECT && in_array(strtolower($key), array('to', 'cc', 'bcc')) && mb_strpos($val, '@') !== false) {
383                // Don't carry this into the $final_headers.
384                $app->logMsg(sprintf('Skipping header in sandbox mode: %s=%s', $key, $val), LOG_DEBUG, __FILE__, __LINE__);
385                continue;
386            }
[38]387            $final_headers[] = sprintf('%s: %s', $key, $val);
388        }
[212]389        $final_headers = join($this->getParam('crlf'), $final_headers);
[42]390
[24]391        // This is the address where delivery problems are sent to. We must strip off everything except the local@domain part.
[490]392        if (isset($this->_params['envelope_sender_address'])) {
393            $envelope_sender_address = sprintf('<%s>', trim($this->_params['envelope_sender_address'], '<>'));
394        } else {
395            $envelope_sender_address = preg_replace('/^.*<?([^\s@\[\]<>()]+\@[A-Za-z0-9.-]{1,}\.[A-Za-z]{2,5})>?$/iU', '$1', $this->_params['from']);
396        }
[212]397        if ('' != $envelope_sender_address && $this->validEmail($envelope_sender_address)) {
[490]398            $additional_parameter = sprintf('-f %s', $envelope_sender_address);
[212]399        } else {
[490]400            $additional_parameter = '';
[212]401        }
[42]402
[36]403        // Check for mail header injection attacks.
[212]404        $full_mail_content = join($this->getParam('crlf'), array($final_to, $this->_params['subject'], $final_body));
[293]405        if (preg_match("/(^|[\n\r])(Content-Type|MIME-Version|Content-Transfer-Encoding|Bcc|Cc)\s*:/i", $full_mail_content)) {
[136]406            $app->logMsg(sprintf('Mail header injection attack in content: %s', $full_mail_content), LOG_WARNING, __FILE__, __LINE__);
[36]407            return false;
408        }
[346]409
[622]410        // Enter sandbox mode, if specified.
411        switch ($this->getParam('sandbox_mode')) {
412        case self::SANDBOX_MODE_REDIRECT:
413            if (!$this->getParam('sandbox_to_addr')) {
414                $app->logMsg(sprintf('Email sandbox_mode is SANDBOX_MODE_REDIRECT but sandbox_to_addr is not set.', null), LOG_ERR, __FILE__, __LINE__);
415                break;
416            }
417            $final_to = $this->getParam('sandbox_to_addr');
418            break;
419
420        case self::SANDBOX_MODE_STDERR:
421            file_put_contents('php://stderr', sprintf("Subject: %s\nTo: %s\n%s\n\n%s", $this->getParam('subject'), $final_to, str_replace($this->getParam('crlf'), "\n", $final_headers), $final_body), FILE_APPEND);
422            return true;
[628]423
424        case self::SANDBOX_MODE_LOG:
425            $log_serialize = $app->getParam('log_serialize');
426            $app->setParam(array('log_serialize' => false));
427            $app->logMsg(sprintf("\nSubject: %s\nTo: %s\n%s\n\n%s", $this->getParam('subject'), $final_to, str_replace($this->getParam('crlf'), "\n", $final_headers), $final_body), LOG_DEBUG, __FILE__, __LINE__);
428            $app->setParam(array('log_serialize' => $log_serialize));
429            return true;
[622]430        }
431
[454]432        // Send email without 5th parameter if safemode is enabled.
[345]433        if (ini_get('safe_mode')) {
[346]434            $ret = mb_send_mail($final_to, $this->_params['subject'], $final_body, $final_headers);
[479]435        } else {
[490]436            $ret = mb_send_mail($final_to, $this->_params['subject'], $final_body, $final_headers, $additional_parameter);
[347]437        }
[454]438
[346]439        // Ensure message was successfully accepted for delivery.
440        if ($ret) {
441            $app->logMsg(sprintf('Email successfully sent to %s', $final_to), LOG_INFO, __FILE__, __LINE__);
442            return true;
443        } else {
[490]444            $app->logMsg(sprintf('Email failure: %s, %s, %s, %s', $final_to, $this->_params['subject'], str_replace("\r\n", '\r\n', $final_headers), $additional_parameter), LOG_WARNING, __FILE__, __LINE__);
[346]445            return false;
[454]446        }
[23]447    }
[42]448
[23]449    /**
450     * Validates an email address based on the recommendations in RFC 3696.
[42]451     * Is more loose than restrictive, to allow the many valid variants of
[23]452     * email addresses while catching the most common mistakes. Checks an array too.
453     * http://www.faqs.org/rfcs/rfc822.html
454     * http://www.faqs.org/rfcs/rfc2822.html
455     * http://www.faqs.org/rfcs/rfc3696.html
456     * http://www.faqs.org/rfcs/rfc1035.html
457     *
458     * @access  public
459     * @param   mixed  $email  Address to check, string or array.
460     * @return  bool    Validity of address.
461     * @author  Quinn Comendant <quinn@strangecode.com>
462     * @since   30 Nov 2005 22:00:50
463     */
[468]464    public function validEmail($email)
[23]465    {
[479]466        $app =& App::getInstance();
[454]467
[24]468        // If an array, check values recursively.
[23]469        if (is_array($email)) {
470            foreach ($email as $e) {
471                if (!$this->validEmail($e)) {
472                    return false;
473                }
474            }
475            return true;
476        } else {
[334]477            // To be valid email address must match regex and fit within the length constraints.
[247]478            if (preg_match($this->getParam('regex'), $email, $e_parts) && mb_strlen($e_parts[2]) < 64 && mb_strlen($e_parts[3]) < 255) {
[24]479                return true;
480            } else {
[394]481                $app->logMsg(sprintf('Invalid email address: %s', $email), LOG_INFO, __FILE__, __LINE__);
[23]482                return false;
483            }
484        }
485    }
486}
487
Note: See TracBrowser for help on using the repository browser.