thomascube
2009-07-13 afc6e4bd108525104b61a46f9973f8f8358c6dd2
program/include/rcube_smtp.inc
@@ -5,7 +5,7 @@
 | program/include/rcube_smtp.inc                                        |
 |                                                                       |
 | This file is part of the RoundCube Webmail client                     |
 | Copyright (C) 2005, RoundCube Dev. - Switzerland                      |
 | Copyright (C) 2005-2007, RoundCube Dev. - Switzerland                 |
 | Licensed under the GNU GPL                                            |
 |                                                                       |
 | PURPOSE:                                                              |
@@ -19,10 +19,11 @@
*/
// include required PEAR classes
require_once('Net/SMTP.php');
/**
 * SMTP delivery functions
 *
 * @package Mail
 */
// define headers delimiter
define('SMTP_MIME_CRLF', "\r\n");
@@ -47,45 +48,57 @@
 * @param string The full text of the message body, including any Mime parts, etc.
 *
 * @return bool  Returns TRUE on success, or FALSE on error
 * @access public
 */
function smtp_mail($from, $recipients, &$headers, &$body, &$response)
function smtp_mail($from, $recipients, &$headers, &$body, &$response, &$error)
  {
  global $SMTP_CONN, $CONFIG;
  $smtp_timeout = null;
  $smtp_host = $CONFIG['smtp_server'];
  $smtp_port = is_numeric($CONFIG['smtp_port']) ? $CONFIG['smtp_port'] : 25;
  $smtp_host_url = parse_url($CONFIG['smtp_server']);
  global $SMTP_CONN, $RCMAIL;
  
  // let plugins alter smtp connection config
  $CONFIG = $RCMAIL->plugins->exec_hook('smtp_connect', array(
    'smtp_server' => $RCMAIL->config->get('smtp_server'),
    'smtp_port'   => $RCMAIL->config->get('smtp_port', 25),
    'smtp_user'   => $RCMAIL->config->get('smtp_user'),
    'smtp_pass'   => $RCMAIL->config->get('smtp_pass'),
    'smtp_auth_type' => $RCMAIL->config->get('smtp_auth_type'),
    'smtp_helo_host' => $RCMAIL->config->get('smtp_helo_host'),
  ));
  $smtp_timeout = null;
  $smtp_host = str_replace('%h', $_SESSION['imap_host'], $CONFIG['smtp_server']);
  $smtp_port = is_numeric($CONFIG['smtp_port']) ? $CONFIG['smtp_port'] : 25;
  $smtp_host_url = parse_url($smtp_host);
  // overwrite port
  if ($smtp_host_url['host'] && $smtp_host_url['port'])
  if (isset($smtp_host_url['host']) && isset($smtp_host_url['port']))
    {
    $smtp_host = $smtp_host_url['host'];
    $smtp_port = $smtp_host_url['port'];
    }
  // re-write smtp host
  if ($smtp_host_url['host'] && $smtp_host_url['scheme'])
  if (isset($smtp_host_url['host']) && isset($smtp_host_url['scheme']))
    $smtp_host = sprintf('%s://%s', $smtp_host_url['scheme'], $smtp_host_url['host']);
  // create Net_SMTP object and connect to server
  if (!is_object($smtp_conn))
  if (!is_object($SMTP_CONN))
    {
    $helo_host = !empty($_SERVER['server_name']) ? $_SERVER['server_name'] : 'localhost';
    if (!empty($CONFIG['smtp_helo_host']))
      $helo_host = $CONFIG['smtp_helo_host'];
    else if (!empty($_SERVER['SERVER_NAME']))
      $helo_host = preg_replace('/:\d+$/', '', $_SERVER['SERVER_NAME']);
    else
      $helo_host = 'localhost';
    $SMTP_CONN = new Net_SMTP($smtp_host, $smtp_port, $helo_host);
    // set debugging
    if ($CONFIG['debug_level'] & 8)
      $SMTP_CONN->setDebug(TRUE);
    // try to connect to server and exit on failure
    $result = $SMTP_CONN->connect($smtp_timeout);
    if (PEAR::isError($result))
      {
      $SMTP_CONN = null;
      $response[] = "Connection failed: ".$result->getMessage();
      $error = array('label' => 'smtpconnerror', 'vars' => array('code' => $SMTP_CONN->_code));
      $SMTP_CONN = null;
      return FALSE;
      }
      
@@ -98,7 +111,7 @@
        $smtp_user = $CONFIG['smtp_user'];
      if (strstr($CONFIG['smtp_pass'], '%p'))
        $smtp_pass = str_replace('%p', decrypt_passwd($_SESSION['password']), $CONFIG['smtp_pass']);
        $smtp_pass = str_replace('%p', $RCMAIL->decrypt($_SESSION['password']), $CONFIG['smtp_pass']);
      else
        $smtp_pass = $CONFIG['smtp_pass'];
@@ -107,8 +120,9 @@
    
      if (PEAR::isError($result))
        {
        $error = array('label' => 'smtpautherror', 'vars' => array('code' => $SMTP_CONN->_code));
        $response[] .= 'Authentication failure: ' . $result->getMessage() . ' (Code: ' . $result->getCode() . ')';
        smtp_reset();
        $response[] .= "Authentication failure: ".$result->getMessage();
        return FALSE;
        }
      }
@@ -148,8 +162,9 @@
  // set From: address
  if (PEAR::isError($SMTP_CONN->mailFrom($from)))
    {
    smtp_reset();
    $error = array('label' => 'smtpfromerror', 'vars' => array('from' => $from, 'code' => $SMTP_CONN->_code));
    $response[] .= "Failed to set sender '$from'";
    smtp_reset();
    return FALSE;
    }
@@ -158,6 +173,7 @@
  $recipients = smtp_parse_rfc822($recipients);
  if (PEAR::isError($recipients))
    {
    $error = array('label' => 'smtprecipientserror');
    smtp_reset();
    return FALSE;
    }
@@ -168,8 +184,9 @@
    {
    if (PEAR::isError($SMTP_CONN->rcptTo($recipient)))
      {
      smtp_reset();
      $error = array('label' => 'smtptoerror', 'vars' => array('to' => $recipient, 'code' => $SMTP_CONN->_code));
      $response[] .= "Failed to add recipient '$recipient'";
      smtp_reset();
      return FALSE;
      }
    }
@@ -185,10 +202,11 @@
  unset($text_headers, $body);
   
  // Send the message's headers and the body as SMTP data.
  if (PEAR::isError($SMTP_CONN->data($data)))
  if (PEAR::isError($result = $SMTP_CONN->data($data)))
    {
    smtp_reset();
    $error = array('label' => 'smtperror', 'vars' => array('msg' => $result->getMessage()));
    $response[] .= "Failed to send data";
    smtp_reset();
    return FALSE;
    }
@@ -206,13 +224,12 @@
  {
  global $SMTP_CONN;
  if (is_object($SMTP_CONN))
  if (is_object($SMTP_CONN) && is_resource($SMTP_CONN->_socket->fp))
    {
    $SMTP_CONN->rset();
    smtp_disconnect();
    }
  }
/**
@@ -322,7 +339,7 @@
    $recipients = implode(', ', $recipients);
    
  $addresses = array();
  $recipients = smtp_explode_quoted_str(",", $recipients);
  $recipients = rcube_explode_quoted_string(',', $recipients);
  
  reset($recipients);
  while (list($k, $recipient) = each($recipients))
@@ -332,7 +349,7 @@
    {
      if ((strpos($word, "@") > 0) && (strpos($word, "\"")===false))
        {
        $word = ereg_replace('^<|>$', '', trim($word));
        $word = preg_replace('/^<|>$/', '', trim($word));
        if (in_array($word, $addresses)===false)
          array_push($addresses, $word);
        }
@@ -340,22 +357,5 @@
    }
  return $addresses;
  }
function smtp_explode_quoted_str($delimiter, $string)
  {
  $quotes=explode("\"", $string);
  while ( list($key, $val) = each($quotes))
    if (($key % 2) == 1)
      $quotes[$key] = str_replace($delimiter, "_!@!_", $quotes[$key]);
    $string=implode("\"", $quotes);
    $result=explode($delimiter, $string);
    while (list($key, $val) = each($result))
      $result[$key] = str_replace("_!@!_", $delimiter, $result[$key]);
  return $result;
  }
?>