queue.php

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 491 行 · 第 1/2 页

PHP
491
字号
<?php/* vim: set expandtab tabstop=4 shiftwidth=4: */// +----------------------------------------------------------------------+// | PEAR :: Mail :: Queue                                                |// +----------------------------------------------------------------------+// | Copyright (c) 1997-2004 The PHP Group                                |// +----------------------------------------------------------------------+// | This source file is subject to version 3.0 of the PHP license,       |// | that is bundled with this package in the file LICENSE, and is        |// | available at through the world-wide-web at                           |// | http://www.php.net/license/3_0.txt.                                  |// | If you did not receive a copy of the PHP license and are unable to   |// | obtain it through the world-wide-web, please send a note to          |// | license@php.net so we can mail you a copy immediately.               |// +----------------------------------------------------------------------+// | Authors: Radek Maciaszek <chief@php.net>                             |// |          Lorenzo Alberton <l dot alberton at quipo dot it>           |// +----------------------------------------------------------------------+//// $Id: Queue.php,v 1.15 2004/07/27 08:58:03 quipo Exp $/*** Class for handle mail queue managment.* Wrapper for Pear::Mail and Pear::DB.* Could load, save and send saved mails in background* and also backup some mails.** Mail queue class put mails in a temporary* container waiting to be fed to the MTA (Mail Transport Agent)* and send them later (eg. every few minutes) by crontab or in other way.** -------------------------------------------------------------------------* A basic usage example:* -------------------------------------------------------------------------** $container_options = array(*   'type'        => 'db',*   'database'    => 'dbname',*   'phptype'     => 'mysql',*   'username'    => 'root',*   'password'    => '',*   'mail_table'  => 'mail_queue'* );*   //optionally, a 'dns' string can be provided instead of db parameters.*   //look at DB::connect() method or at DB or MDB docs for details.*   //you could also use mdb container instead db** $mail_options = array(*   'driver'   => 'smtp',*   'host'     => 'your_smtp_server.com',*   'port'     => 25,*   'auth'     => false,*   'username' => '',*   'password' => ''* );** $mail_queue =& new Mail_Queue($container_options, $mail_options);* ****************************************************************** // Here the code differentiates wrt you want to add an email to the queue* // or you want to send the emails that already are in the queue.* ****************************************************************** // TO ADD AN EMAIL TO THE QUEUE* ****************************************************************** $from             = 'user@server.com';* $from_name        = 'admin';* $recipient        = 'recipient@other_server.com';* $recipient_name   = 'recipient';* $message          = 'Test message';* $from_params      = empty($from_name) ? '"'.$from_name.'" <'.$from.'>' : '<'.$from.'>';* $recipient_params = empty($recipient_name) ? '"'.$recipient_name.'" <'.$recipient.'>' : '<'.$recipient.'>';* $hdrs = array( 'From'    => $from_params,*                'To'      => $recipient_params,*                'Subject' => "test message body"  );* $mime =& new Mail_mime();* $mime->setTXTBody($message);* $body = $mime->get();* $hdrs = $mime->headers($hdrs);** // Put message to queue* $mail_queue->put( $from, $recipient, $hdrs, $body );* //Also you could put this msg in more advanced mode [look at Mail_Queue docs for details]* $seconds_to_send = 3600;* $delete_after_send = false;* $id_user = 7;* $mail_queue->put( $from, $recipient, $hdrs, $body, $seconds_to_send, $delete_after_send, $id_user );** ****************************************************************** // TO SEND EMAILS IN THE QUEUE* ****************************************************************** // How many mails could we send each time* $max_ammount_mails = 50;* $mail_queue =& new Mail_Queue($container_options, $mail_options);* $mail_queue->sendMailsInQueue($max_ammount_mails);* ******************************************************************* // for more examples look to docs directory** // end usage example* -------------------------------------------------------------------------** @version $Revision: 1.15 $* $Id: Queue.php,v 1.15 2004/07/27 08:58:03 quipo Exp $* @author Radek Maciaszek <chief@php.net>*//** * This is special constant define start offset for limit sql queries to * get mails. */define('MAILQUEUE_START', 0);/** * You can specify how many mails will be loaded to * queue else object use this constant for load all mails from db. */define('MAILQUEUE_ALL', -1);/** * When you put new mail to queue you could specify user id who send e-mail. * Else you could use system id: MAILQUEUE_SYSTEM or user unknown id: MAILQUEUE_UNKNOWN */define('MAILQUEUE_SYSTEM',  -1);define('MAILQUEUE_UNKNOWN', -2);/** * This constant tells Mail_Queue how many times should try * to send mails again if was any errors before. */define('MAILQUEUE_TRY', 25);/** * MAILQUEUE_ERROR constants */define('MAILQUEUE_ERROR',                   -1);define('MAILQUEUE_ERROR_NO_DRIVER',         -2);define('MAILQUEUE_ERROR_NO_CONTAINER',      -3);define('MAILQUEUE_ERROR_CANNOT_INITIALIZE', -4);define('MAILQUEUE_ERROR_NO_OPTIONS',        -5);define('MAILQUEUE_ERROR_CANNOT_CONNECT',    -6);define('MAILQUEUE_ERROR_QUERY_FAILED',      -7);define('MAILQUEUE_ERROR_UNEXPECTED',        -8);define('MAILQUEUE_ERROR_CANNOT_SEND_MAIL',  -9);require_once 'PEAR.php';require_once 'Mail.php';require_once 'Mail/mime.php';/** * Mail_Queue - base class for mail queue managment. * * @author   Radek Maciaszek <wodzu@tonet.pl> * @version  $Id: Queue.php,v 1.15 2004/07/27 08:58:03 quipo Exp $ * @package  Mail_Queue * @access   public */class Mail_Queue extends PEAR{    // {{{ Class vars    /**     * Mail options: smtp, mail etc. see Mail::factory     *     * @var array     */    var $mail_options;    /**     * Mail_Queue_Container     *     * @var object     */    var $container;    /**     * Reference to Pear_Mail object     *     * @var object     */    var $send_mail;    /**     * Pear error mode (when raiseError is called)     * (see PEAR doc)     *     * @var int $_pearErrorMode     * @access private     */    var $pearErrorMode = PEAR_ERROR_RETURN;    // }}}    // {{{ Mail_Queue    /**     * Mail_Queue constructor     *     * @param  array $container_options  Mail_Queue container options     * @param  array $mail_options  How send mails.     *     * @return mixed  True on success else PEAR error class.     *     * @access public     */    function Mail_Queue($container_options, $mail_options)    {        $this->PEAR();        if (isset($mail_options['pearErrorMode'])) {            $this->pearErrorMode = $mail_options['pearErrorMode'];            // ugly hack to propagate 'pearErrorMode'            $container_options['pearErrorMode'] = $mail_options['pearErrorMode'];        }        if (!is_array($mail_options) || !isset($mail_options['driver'])) {            return new Mail_Queue_Error(MAILQUEUE_ERROR_NO_DRIVER,                        $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__);        }        $this->mail_options = $mail_options;        if (!is_array($container_options) || !isset($container_options['type'])) {            return new Mail_Queue_Error(MAILQUEUE_ERROR_NO_CONTAINER,                        $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__);        }        $container_type = strtolower($container_options['type']);        $container_class = 'Mail_Queue_Container_' . $container_type;        $container_classfile = $container_type . '.php';        include_once 'Mail/Queue/Container/' . $container_classfile;        $this->container = new $container_class($container_options);        if(PEAR::isError($this->container)) {            return new Mail_Queue_Error(MAILQUEUE_ERROR_CANNOT_INITIALIZE,                        $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__);        }        return true;    }    // }}}    // {{{ _Mail_Queue()    /**     * Mail_Queue desctructor     *     * @return void     * @access public     */    function _Mail_Queue()    {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?