Changeset 3142

Show
Ignore:
Timestamp:
07/17/2008 10:38:11 AM (5 months ago)
Author:
Shadowhand
Message:

Fixing #585, thanks Igor. New syntax supported: email::send(array('to' => $to, 'cc' => $cc, 'bcc' => $bcc), ...)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/helpers/email.php

    r2958 r3142  
    107107         * Send an email message. 
    108108         * 
    109          * @param   string|array  recipient email (and name) 
     109         * @param   string|array  recipient email (and name), or an array of To, Cc, Bcc names 
    110110         * @param   string|array  sender email (and name) 
    111111         * @param   string        message subject 
     
    125125                $message = new Swift_Message($subject, $message, $html, '8bit', 'utf-8'); 
    126126 
    127                 // Make a personalized To: address 
    128                 is_object($to) or $to = is_array($to) ? new Swift_Address($to[0], $to[1]) : new Swift_Address($to); 
     127                if (is_string($to)) 
     128                { 
     129                        // Single recipient 
     130                        $recipients = new Swift_Address($to); 
     131                } 
     132                elseif (is_array($to)) 
     133                { 
     134                        if (isset($to[0]) AND isset($to[1])) 
     135                        { 
     136                                // Create To: address set 
     137                                $to = array('to' => $to); 
     138                        } 
    129139 
    130                 // Make a personalized From: address 
    131                 is_object($from) or $from = is_array($from) ? new Swift_Address($from[0], $from[1]) : new Swift_Address($from); 
     140                        // Create a list of recipients 
     141                        $recipients = new Swift_RecipientList; 
    132142 
    133                 return email::$mail->send($message, $to, $from); 
     143                        foreach ($to as $method => $set) 
     144                        { 
     145                                if ( ! in_array($method, array('to', 'cc', 'bcc'))) 
     146                                { 
     147                                        // Use To: by default 
     148                                        $method = 'to'; 
     149                                } 
     150 
     151                                // Create method name 
     152                                $method = 'add'.ucfirst($method); 
     153 
     154                                if (is_array($set)) 
     155                                { 
     156                                        // Add a recipient with name 
     157                                        $recipients->$method($set[0], $set[1]); 
     158                                } 
     159                                else 
     160                                { 
     161                                        // Add a recipient without name 
     162                                        $recipients->$method($set); 
     163                                } 
     164                        } 
     165                } 
     166 
     167                if (is_string($from)) 
     168                { 
     169                        // From without a name 
     170                        $from = new Swift_Address($from); 
     171                } 
     172                elseif (is_array($from)) 
     173                { 
     174                        // From with a name 
     175                        $from = new Swift_Address($from[0], $from[1]); 
     176                } 
     177 
     178                return email::$mail->send($message, $recipients, $from); 
    134179        } 
    135180