Exemple #1
0
 /**
  * Connect to the SMTP server by instantiating a Net_SMTP object.
  *
  * @return Net_SMTP  The SMTP object.
  * @throws Horde_Mail_Exception
  */
 public function getSMTPObject()
 {
     if ($this->_smtp) {
         return $this->_smtp;
     }
     $this->_smtp = new Net_SMTP($this->_params['host'], $this->_params['port'], $this->_params['localhost']);
     /* Set pipelining. */
     if ($this->_params['pipelining']) {
         $this->_smtp->pipelining = true;
     }
     /* If we still don't have an SMTP object at this point, fail. */
     if (!$this->_smtp instanceof Net_SMTP) {
         throw new Horde_Mail_Exception('Failed to create a Net_SMTP object', self::ERROR_CREATE);
     }
     /* Configure the SMTP connection. */
     if ($this->_params['debug']) {
         $this->_smtp->setDebug(true);
     }
     /* Attempt to connect to the configured SMTP server. */
     $res = $this->_smtp->connect($this->_params['timeout']);
     if ($res instanceof PEAR_Error) {
         $this->_error('Failed to connect to ' . $this->_params['host'] . ':' . $this->_params['port'], $res, self::ERROR_CONNECT);
     }
     /* Attempt to authenticate if authentication has been enabled. */
     if ($this->_params['auth']) {
         $method = is_string($this->_params['auth']) ? $this->_params['auth'] : '';
         $res = $this->_smtp->auth($this->_params['username'], $this->_params['password'], $method);
         if ($res instanceof PEAR_Error) {
             $this->_error("{$method} authentication failure", $res, self::ERROR_AUTH);
         }
     }
     return $this->_smtp;
 }
Exemple #2
0
    public function deliver(&$Mailer, $settings = array())
    {
        $Message =& $Mailer->Message;
        
        $SmtpClient =& Mail::factory('smtp', $settings);

        include_once 'Net/SMTP.php';

        if (!($smtp = new Net_SMTP($SmtpClient->host, $SmtpClient->port, $SmtpClient->localhost))) {
            return PEAR::raiseError('unable to instantiate Net_SMTP object');
        }

        if ($SmtpClient->debug) {
            $smtp->setDebug(true);
        }

        if (PEAR::isError($smtp->connect($SmtpClient->timeout))) {
            trigger_error('unable to connect to smtp server '.$SmtpClient->host.':'.$SmtpClient->port, E_USER_NOTICE);
            return false;
        }

        if ($SmtpClient->auth) {
            $method = is_string($SmtpClient->auth) ? $SmtpClient->auth : '';

            if (PEAR::isError($smtp->auth($SmtpClient->username, $SmtpClient->password, $method))) {
                trigger_error('unable to authenticate to smtp server', E_USER_ERROR);
            }
        }

        $from = is_array($Message->from) ? array_shift(array_values($Message->from)) : $Message->from;

        if (PEAR::isError($smtp->mailFrom($from))) {
            trigger_error('unable to set sender to [' . $from . ']', E_USER_ERROR);
        }

        $recipients = $SmtpClient->parseRecipients($Message->getRecipients());
        
        if (PEAR::isError($recipients)) {
            return $recipients;
        }

        foreach ($recipients as $recipient) {
            if (PEAR::isError($res = $smtp->rcptTo($recipient))) {
                return PEAR::raiseError('unable to add recipient [' .
                $recipient . ']: ' . $res->getMessage());
            }
        }

        if (PEAR::isError($smtp->data($Mailer->getRawMessage()))) {
            return PEAR::raiseError('unable to send data');
        }

        $smtp->disconnect();
    }
Exemple #3
0
 /**
  */
 public function send($recipients, array $headers, $body)
 {
     $headers = $this->_sanitizeHeaders($headers);
     // Prepare headers
     list($from, $textHeaders) = $this->prepareHeaders($headers);
     try {
         $from = $this->_getFrom($from, $headers);
     } catch (Horde_Mail_Exception $e) {
         $this->_error('no_from');
     }
     // Prepare recipients
     foreach ($this->parseRecipients($recipients) as $rcpt) {
         list(, $host) = explode('@', $rcpt);
         $mx = $this->_getMx($host);
         if (!$mx) {
             $this->_error('no_mx', array('rcpt' => $rcpt));
         }
         $connected = false;
         foreach (array_keys($mx) as $mserver) {
             $this->_smtp = new Net_SMTP($mserver, $this->_params['port'], $this->_params['mailname']);
             // configure the SMTP connection.
             if ($this->_params['debug']) {
                 $this->_smtp->setDebug(true);
             }
             // attempt to connect to the configured SMTP server.
             $res = $this->_smtp->connect($this->_params['timeout']);
             if ($res instanceof PEAR_Error) {
                 $this->_smtp = null;
                 continue;
             }
             // connection established
             if ($res) {
                 $connected = true;
                 break;
             }
         }
         if (!$connected) {
             $this->_error('not_connected', array('host' => implode(', ', array_keys($mx)), 'port' => $this->_params['port'], 'rcpt' => $rcpt));
         }
         // Verify recipient
         if ($this->_params['vrfy']) {
             $res = $this->_smtp->vrfy($rcpt);
             if ($res instanceof PEAR_Error) {
                 $this->_error('failed_vrfy_rcpt', array('rcpt' => $rcpt));
             }
         }
         // mail from:
         $args['verp'] = $this->_params['verp'];
         $res = $this->_smtp->mailFrom($from, $args);
         if ($res instanceof PEAR_Error) {
             $this->_error('failed_set_from', array('from' => $from));
         }
         // rcpt to:
         $res = $this->_smtp->rcptTo($rcpt);
         if ($res instanceof PEAR_Error) {
             $this->_error('failed_set_rcpt', array('rcpt' => $rcpt));
         }
         // Don't send anything in test mode
         if ($this->_params['test']) {
             $res = $this->_smtp->rset();
             if ($res instanceof PEAR_Error) {
                 $this->_error('failed_rset');
             }
             $this->_smtp->disconnect();
             $this->_smtp = null;
             return;
         }
         // Send data. Net_SMTP does necessary EOL conversions.
         $res = $this->_smtp->data($body, $textHeaders);
         if ($res instanceof PEAR_Error) {
             $this->_error('failed_send_data', array('rcpt' => $rcpt));
         }
         $this->_smtp->disconnect();
         $this->_smtp = null;
     }
 }
function open_smtp(&$arr_mail_account, &$smtp, $account_no, $debug_flag)
{
    // account list array, from config.php
    global $arrAccountsSmtp;
    // $account_noの範囲チェック
    if ($account_no <= 0 || $account_no > count($arrAccountsSmtp)) {
        printf("<p>アカウント指定Noが範囲外です<br/>account=%d</p>\n", $account_no);
        return false;
    }
    // メールアカウント情報(サーバ名、ユーザ名、パスワード等)を得る
    $arr_mail_account = GetMailAccount($arrAccountsSmtp[$account_no - 1][1], 'smtp');
    if (!isset($arr_mail_account['server']) || !isset($arr_mail_account['user']) || !isset($arr_mail_account['password']) || strcmp($arr_mail_account['protocol'], 'smtp')) {
        print "<p>メールアカウント管理エラー(server/user/password値が得られないか、protocolがsmtpでない)</p>\n";
        return false;
    }
    // 新しい Net_SMTP オブジェクトを作成します
    if (!($smtp = new Net_SMTP($arr_mail_account['server'], $arr_mail_account['port']))) {
        print "<p>ネットワーク異常(SMTPオブジェクト作成不能)</p>";
        return false;
    }
    // デバッグ出力を、stdoutに送る
    if ($debug_flag) {
        $smtp->setDebug(true);
    }
    // SMTP サーバに接続します
    if (PEAR::isError($e = $smtp->connect())) {
        print "<p>ネットワーク異常(SMTPでコネクション不能)</p>";
        return false;
    }
    // ユーザ名、パスワードを用いてSMTPログオンします
    // (Digest-MD5, CRAMMD5, LOGIN, PLAINの順に暗号強度の高いもので接続します)
    if (PEAR::isError($e = $smtp->auth($arr_mail_account['user'], $arr_mail_account['password']))) {
        $smtp->disconnect();
        print "<p>SMTPサーバの認証に失敗</p>";
        return false;
    }
    printf("<p>接続:%s@%s:%s</p>\n", $arr_mail_account['user'], $arr_mail_account['server'], $arr_mail_account['port']);
    return true;
}