예제 #1
0
 /**
  * Connect function. This will, when called
  * statically, create a new smtp object, 
  * call the connect function (ie this function)
  * and return it. When not called statically,
  * it will connect to the server and send
  * the HELO command.
  */
 public function connect($params = array())
 {
     if (!isset($this->status)) {
         $obj = new JO_Mail_Smtp($params);
         if ($obj->connect()) {
             $obj->status = SMTP_STATUS_CONNECTED;
         }
         return $obj;
     } else {
         $this->connection = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
         if (function_exists('socket_set_timeout')) {
             @socket_set_timeout($this->connection, 5, 0);
         }
         $greeting = $this->get_data();
         if (is_resource($this->connection)) {
             return $this->auth ? $this->ehlo() : $this->helo();
         } else {
             $this->errors[] = 'Failed to connect to server: ' . $errstr;
             return FALSE;
         }
     }
 }
예제 #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;
     }
 }