Exemplo n.º 1
0
 /**
  * Starts the whole process. The address must either be set here
  * or when creating the object. One or the other.
  *
  * @access public
  * @param string  $address         The address(es) to validate.
  * @param string  $default_domain  Default domain/host etc.
  * @param boolean $nest_groups     Whether to return the structure with groups nested for easier viewing.
  * @param boolean $validate        Whether to validate atoms. Turn this off if you need to run addresses through before encoding the personal names, for instance.
  * 
  * @return array A structured array of addresses.
  */
 function parseAddressList($address = null, $default_domain = null, $nest_groups = null, $validate = null, $limit = null)
 {
     if (!isset($this->mailRFC822)) {
         $obj = new JO_Mail_RFC822($address, $default_domain, $nest_groups, $validate, $limit);
         return $obj->parseAddressList();
     }
     if (isset($address)) {
         $this->address = $address;
     }
     if (isset($default_domain)) {
         $this->default_domain = $default_domain;
     }
     if (isset($nest_groups)) {
         $this->nestGroups = $nest_groups;
     }
     if (isset($validate)) {
         $this->validate = $validate;
     }
     if (isset($limit)) {
         $this->limit = $limit;
     }
     $this->structure = array();
     $this->addresses = array();
     $this->error = null;
     $this->index = null;
     while ($this->address = $this->_splitAddresses($this->address)) {
         continue;
     }
     if ($this->address === false || isset($this->error)) {
         return false;
     }
     // Reset timer since large amounts of addresses can take a long time to
     // get here
     set_time_limit(30);
     // Loop through all the addresses
     for ($i = 0; $i < count($this->addresses); $i++) {
         if (($return = $this->_validateAddress($this->addresses[$i])) === false || isset($this->error)) {
             return false;
         }
         if (!$this->nestGroups) {
             $this->structure = array_merge($this->structure, $return);
         } else {
             $this->structure[] = $return;
         }
     }
     return $this->structure;
 }
Exemplo n.º 2
0
 /**
  * Sends the mail.
  *
  * @param  array  $recipients Array of receipients to send the mail to
  * @param  string $type       How to send the mail ('mail' or 'sendmail' or 'smtp')
  * @return mixed
  */
 public function send($recipients, $type = 'mail')
 {
     if (!defined('RMAIL_CRLF')) {
         $this->setCRLF(($type == 'mail' or $type == 'sendmail') ? "\n" : "\r\n");
     }
     $this->build();
     switch ($type) {
         case 'mail':
             $subject = '';
             if (!empty($this->headers['Subject'])) {
                 //                    $subject = $this->encodeHeader($this->headers['Subject'], $this->build_params['head_charset']);
                 $subject = "=?" . $this->build_params['head_charset'] . "?B?" . base64_encode($this->headers['Subject']) . "?=";
                 unset($this->headers['Subject']);
             }
             // Get flat representation of headers
             foreach ($this->headers as $name => $value) {
                 $headers[] = $name . ': ' . $this->encodeHeader($value, $this->build_params['head_charset']);
             }
             $to = $this->encodeHeader(implode(', ', $recipients), $this->build_params['head_charset']);
             if (!empty($this->return_path)) {
                 $result = mail($to, $subject, $this->output, implode(RMAIL_CRLF, $headers), '-f' . $this->return_path);
             } else {
                 $result = mail($to, $subject, $this->output, implode(RMAIL_CRLF, $headers));
             }
             // Reset the subject in case mail is resent
             if ($subject !== '') {
                 $this->headers['Subject'] = $subject;
             }
             // Return
             return $result;
             break;
         case 'sendmail':
             // Get flat representation of headers
             foreach ($this->headers as $name => $value) {
                 $headers[] = $name . ': ' . $this->encodeHeader($value, $this->build_params['head_charset']);
             }
             // Encode To:
             $headers[] = 'To: ' . $this->encodeHeader(implode(', ', $recipients), $this->build_params['head_charset']);
             // Get return path arg for sendmail command if necessary
             $returnPath = '';
             if (!empty($this->return_path)) {
                 $returnPath = '-f' . $this->return_path;
             }
             $pipe = popen($this->sendmail_path . " " . $returnPath, 'w');
             $bytes = fputs($pipe, implode(RMAIL_CRLF, $headers) . RMAIL_CRLF . RMAIL_CRLF . $this->output);
             $r = pclose($pipe);
             return $r;
             break;
         case 'smtp':
             $smtp =& JO_Mail_Smtp::connect($this->smtp_params);
             // Parse recipients argument for internet addresses
             foreach ($recipients as $recipient) {
                 $addresses = JO_Mail_RFC822::parseAddressList($recipient, $this->smtp_params['helo'], null, false);
                 foreach ($addresses as $address) {
                     $smtp_recipients[] = sprintf('%s@%s', $address->mailbox, $address->host);
                 }
             }
             unset($addresses);
             // These are reused
             unset($address);
             // These are reused
             // Get flat representation of headers, parsing
             // Cc and Bcc as we go
             foreach ($this->headers as $name => $value) {
                 if ($name == 'Cc' or $name == 'Bcc') {
                     $addresses = JO_Mail_RFC822::parseAddressList($value, $this->smtp_params['helo'], null, false);
                     foreach ($addresses as $address) {
                         $smtp_recipients[] = sprintf('%s@%s', $address->mailbox, $address->host);
                     }
                 }
                 if ($name == 'Bcc') {
                     continue;
                 }
                 if ($name == 'Subject') {
                     $headers[] = $name . ': ' . "=?" . $this->build_params['head_charset'] . "?B?" . base64_encode($value) . "?=";
                     continue;
                 }
                 $headers[] = $name . ': ' . $this->encodeHeader($value, $this->build_params['head_charset']);
             }
             // Add To header based on $recipients argument
             $headers[] = 'To: ' . $this->encodeHeader(implode(', ', $recipients), $this->build_params['head_charset']);
             // Add headers to send_params
             $send_params['headers'] = $headers;
             $send_params['recipients'] = array_values(array_unique($smtp_recipients));
             $send_params['body'] = $this->output;
             // Setup return path
             if (isset($this->return_path)) {
                 $send_params['from'] = $this->return_path;
             } elseif (!empty($this->headers['From'])) {
                 $from = JO_Mail_RFC822::parseAddressList($this->headers['From']);
                 $send_params['from'] = sprintf('%s@%s', $from[0]->mailbox, $from[0]->host);
             } else {
                 $send_params['from'] = 'postmaster@' . $this->smtp_params['helo'];
             }
             // Send it
             if (!$smtp->send($send_params)) {
                 $this->errors = $smtp->getErrors();
                 return false;
             }
             return true;
             break;
     }
 }