Changeset 24


Ignore:
Timestamp:
Dec 1, 2005 9:09:45 PM (18 years ago)
Author:
scdev
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Email.inc.php

    r23 r24  
    88 * @author  Quinn Comendant <quinn@strangecode.com>
    99 * @version 1.0
    10  
    1110-------------------------------------------------------------------------------------
    1211// Example.
     
    1413    'to' => array($frm['email'], 'q@lovemachine.local'),
    1514    'from' => sprintf('%s <%s>', App::getParam('site_name'), App::getParam('site_email')),
    16     'subject' => 'Your Golbon account has been activated',
     15    'subject' => 'Your account has been activated',
    1716));
    1817$email->setTemplate('email_registration_confirm.ihtml');
     
    2221    'username' => $frm['username'],
    2322    'password' => $frm['password1'],
    24     'inviting_member_business_name' => $frm['inviting_member_business_name'],
    2523));
    2624if ($email->send()) {
     
    2826} else {
    2927    App::logMsg(sprintf('Error sending confirmation email to address %s', $frm['email']), LOG_DEBUG, __FILE__, __LINE__);
    30     App::raiseMsg(sprintf('We tried sending you a confirmation email to address %s but there was an error. After logging in, please visit the "My Account" page to confirm that this address is correct.', $frm['email']), MSG_NOTICE, __FILE__, __LINE__);
    3128}
    3229-------------------------------------------------------------------------------------
     
    3431class Email {
    3532
     33    // Default parameters, to be overwritten by setParam() and read with getParam()
    3634    var $_params = array(
    3735        'to' => null,
     
    4038        'regex' => null
    4139    );
     40   
     41    // String that contains the email body.
    4242    var $_template;
     43   
     44    // String that contains the email body after replacements.
    4345    var $_template_replaced;
    44     var $regex;
    4546
    4647    /**
     
    5455    function Email($params=null)
    5556    {
     57        // The regex used in validEmail(). Set here instead of in the default _params above so we can use the concatination . dot.
     58        // This matches an email address as complex as:
     59        //      Bob Smith <bob&smith's/dep=sales!@smith-wick.ca.us> (Sales department)
     60        // ...and something as simple as:
     61        //      x@x.com
    5662        $this->setParam(array('regex' => '/^(?:[^,@]*\s+|[^,@]*(<)|)'   // Display name
    5763        . '((?:[^.<>\s@\",\[\]]+[^<>\s@\",\[\]])*[^.<>\s@\",\[\]]+)'    // Local-part
     
    7985    {
    8086        if (isset($params) && is_array($params)) {
    81 
    8287            // Enforce valid email addresses.
    8388            if (isset($params['to']) && !$this->validEmail($params['to'])) {
     
    129134            return false;
    130135        }
     136        // This could be a new template, so reset the _template_replaced.
    131137        $this->_template_replaced = null;
    132138        return true;
     
    150156        } else {
    151157            $_template = $string;
     158            // This could be a new template, so reset the _template_replaced.
    152159            $this->_template_replaced = null;
    153160            return true;
     
    165172    function replace($replacements)
    166173    {
     174        // Ensure template exists.
    167175        if (!isset($this->_template)) {
    168176            App::logMsg(sprintf('Cannot replace variables, no template defined.', null), LOG_ERR, __FILE__, __LINE__);
    169177            return false;
    170178        }
    171 
     179       
     180        // Ensure replacements argument is an array.
    172181        if (!is_array($replacements)) {
    173182            App::logMsg(sprintf('Cannot replace variables, invalid replacements.', null), LOG_ERR, __FILE__, __LINE__);
     
    181190        // Replace values.
    182191        $replace = array_values($replacements);
    183 
     192       
     193        // Search and replace all values at once.
    184194        $this->_template_replaced = preg_replace($search, $replace, $this->_template);
    185195    }
     
    197207    function send($to=null, $from=null, $subject=null)
    198208    {
     209        // Use arguments if provided.
    199210        if (isset($to)) {
    200211             $this->setParam(array('to' => $to));
     
    206217             $this->setParam(array('subject' => $subject));
    207218        }
    208 
     219   
     220   
     221        // Ensure required values exist.
    209222        if (!isset($this->_template)) {
    210223            App::logMsg(sprintf('Cannot send email. Template not set.', null), LOG_ERR, __FILE__, __LINE__);
     
    221234        }
    222235
     236        // Wrap email text body, using _template_replaced if replacements have been used, or just a fresh _template if not.
    223237        $final_body = wordwrap(isset($this->_template_replaced) ? $this->_template_replaced : $this->_template);
    224         // Test that all placeholders have been replaced.
     238
     239        // Ensure all placeholders have been replaced. Find anything with {...} characters.
    225240        if (preg_match('/({[^}]+})/', $final_body, $unreplaced_match)) {
    226241            App::logMsg(sprintf('Cannot send email. Variables left unreplaced in template: %s', (isset($unreplaced_match) ? $unreplaced_match[1] : '')), LOG_ERR, __FILE__, __LINE__);
     
    228243        }
    229244       
     245        // Final "to" header can have multiple addresses if in an array.
    230246        $final_to = is_array($this->_params['to']) ? join(', ', $this->_params['to']) : $this->_params['to'];
    231         $final_from = sprintf("From: %s\r\n", $this->_params['from']);
     247       
     248        // From headers are custom headers.
     249        $headers = sprintf("From: %s\r\n", $this->_params['from']);
     250       
     251        // This is the address where delivery problems are sent to. We must strip off everything except the local@domain part.
    232252        $envelope_sender_header = sprintf('-f %s', preg_replace('/^.*<?([^\s@\[\]<>()]+\@[A-Za-z0-9.-]{1,}\.[A-Za-z]{2,5})>?$/iU', '$1', $this->_params['from']));
    233         if (!mail($final_to, $this->_params['subject'], $final_body, $final_from, $envelope_sender_header)) {
    234             App::logMsg(sprintf('Email failure with parameters: %s, %s, %s, %s', $this->_params['to'], $this->_params['subject'], str_replace("\r\n", '', $final_from), $envelope_sender_header), LOG_NOTICE, __FILE__, __LINE__);
     253
     254        // Ensure message was successfully accepted for delivery.
     255        if (!mail($final_to, $this->_params['subject'], $final_body, $headers, $envelope_sender_header)) {
     256            App::logMsg(sprintf('Email failure with parameters: %s, %s, %s, %s', $this->_params['to'], $this->_params['subject'], str_replace("\r\n", '', $headers), $envelope_sender_header), LOG_NOTICE, __FILE__, __LINE__);
    235257            return false;
    236258        }
     
    256278    function validEmail($email)
    257279    {
     280        // If an array, check values recursively.
    258281        if (is_array($email)) {
    259282            foreach ($email as $e) {
     
    264287            return true;
    265288        } else {
    266             if (!preg_match($this->getParam('regex'), $email)) {
     289            // To be valid email address must match regex and fit within the lenth constraints.
     290            if (preg_match($this->getParam('regex'), $email, $e_parts) && strlen($e_parts[2]) < 64 && strlen($e_parts[3]) < 255) {
     291                return true;
     292            } else {
    267293                App::logMsg(sprintf('Invalid email: %s', $email), LOG_DEBUG, __FILE__, __LINE__);
    268294                return false;
    269295            }
    270             return true;
    271296        }
    272297    }
Note: See TracChangeset for help on using the changeset viewer.