Snippet
<?PHP
/**mailq.php
*
* <p>
* This package represents a simple interface to the functionality
* of Pear Mail::Queue by creating a function, mailq(), that can
* replace the built-in mail() function.
* </p>
* <ul>
* <li>The mailq() function queues messages for later delivery.
* <li>The queue is described in the file mail.sql.
* <li>The mailq() function is defined in mailq.php.
* <li>The send capability is invoked by running send.php.
* <li>The file config.php defines the data connection and mail type
* for mailq.php and send.php.
* <li>A special variable, $SENDER_ID can be defined before calling mailq()
* to populate the user_id field
* </ul>
*
* @package mailq
* @author Robert Peake <robert@peakepro.com>
* @copyright © 2004
* @link http://www.robertpeake.com/
* @license http://www.php.net/license/3_0.txt
* @version 0.1
*/
/**
* @see mailq_config.php
*/
require_once('mailq_config.php'); //replace this with the full path to the file config.php
/**function mailq
*
* Queues messages in the data structure defined by mailq_config.php.
* Returns TRUE on success, FALSE otherwise
* @see mailq_config.php
*
* @access public
* @param string $to
* @param string $subject
* @param string $body
* @param string $headers
* @return bool
*/
function mailq($to,$subject,$body,$headers='') {
if(!$SENDER_ID) $SENDER_ID = -1;
$hdrs = headers_to_array($headers);
$hdrs['Subject'] = $subject;
$from = $hdrs['From'];
if(!$from) {
$from = $_SERVER['SERVER_ADMIN'];
$hdrs['From'] = $from;
}
$mail_queue =& new Mail_Queue($db_options, $mail_options);
return $mail_queue->put( $from,$to, $hdrs, $body ,0,FALSE,$SENDER_ID);
}
/**function fold_headers
*
* RFC 822 compliant header wrapping
* wraps crlf followed by any number of lwsp
* to a single space ( )
* returns string representing wrapped header
*
* @access public
* @param string $headers
* @return string
*/
function fold_headers($headers) {
}
/**function headers_to_array
*
* RFC 822 compliant header parsing
* header key is delimited by colon (:) followed by any number of lwsp
* headers are delimited by a single crlf
* returns an array suitable for passing to Mail::Queue->put().
* relies on the fold_headers() function to fold headers.
* @see fold_headers
*
* @access public
* @param string $headers
* @return array
*/
function headers_to_array($headers) {
$headers = fold_headers($headers);
$array = preg_split("/$CRLF/", $headers, 0,PREG_SPLIT_NO_EMPTY );
for($i=0;$i<sizeof($array);$i++) {
$header_line = $array[$i];
$header_array = preg_split("/:$LWSP+/", $header_line);
$key = $header_array[0];
$value = $header_array[1];
$return_array[$key] = $value;
}
return $return_array;
}
?>
<?php
/**mailq_config.php
*
* This file defines $CRLF, $LWSP, $db_options, $mail_options
* and optionally defines the is_a function for php < 4.2.0.
* This file also requires the Mail::Queue library
*
* @package mailq
* @see mailq.php
*/
$CRLF = '(\n|\r\n)'; //RFC 822 defines CRLF strictly as \r\n, however many UNIX MTAs seem to prefer \n, so both are accepted
$LWSP = '([ ]|\t)'; //RCF 822 defines LWSP as space or tab only
if(! function_exists('is_a')) { //is_a is defined for PHP > 4.2.0 and required by Mail::Queue
/**function is_a
*
* PHP defined version of built-in function is_a present in PHP > 4.2.0
* returns TRUE if $object is of type $name, FALSE otherwise
*
* @access public
* @param string $name
* @param object $object
* @return bool
*/
function is_a($object, $name) {
return TRUE;
} else {
return FALSE;
}
}
}
require_once "Mail/Queue.php";
$db_options['type'] = 'db';
$db_options['dsn'] = 'mysql://user:password@host/mail'; //replace 'user' with username, 'password' with password, 'host' with host; 'mail' is the database
$db_options['mail_table'] = 'mail_queue'; //this is the table
$mail_options['driver'] = 'smtp';
$mail_options['host'] = 'mail.yourcompany.com'; //MTA host name
$mail_options['port'] = 25;
$mail_options['auth'] = true; //true for SMTP auth
$mail_options['username'] = 'mail_queue'; //SMTP login
$mail_options['password'] = 'password'; //SMTP password
?>
<?php
/**send.php
*
* This script defines $max_amount_mails, which defines the max number of
* messages to be sent at one time. When called (from the command line or
* from a browser) the script sends messages from the Mail::Queue data
* structure defined in mailq_config.php.
*
* @package mailq
* @see mailq.php
*/
/**
* @see mailq_config.php
*/
include 'mailq_config.php'; //this must be the full path to the config.php file
/* How many mails could we send each time the script is called */
$max_amount_mails = 50;
/* we use the db_options and mail_options from the config again */
$mail_queue =& new Mail_Queue($db_options, $mail_options);
/* really sending the messages */
$mail_queue->sendMailsInQueue($max_amount_mails);
?>
# Database : `mail`
# --------------------------------------------------------
#
# Table structure for table `mail_queue`
#
CREATE TABLE mail_queue (
id bigint(20) NOT NULL default '0',
create_time datetime NOT NULL default '0000-00-00 00:00:00',
time_to_send datetime NOT NULL default '0000-00-00 00:00:00',
sent_time datetime default NULL,
id_user bigint(20) NOT NULL default '0',
ip varchar(20) NOT NULL default 'unknown',
sender varchar(50) NOT NULL default '',
recipient varchar(50) NOT NULL default '',
headers text NOT NULL,
body longtext NOT NULL,
try_sent tinyint(4) NOT NULL default '0',
delete_after_send tinyint(1) NOT NULL default '1',
KEY time_to_send (time_to_send ),
)
# --------------------------------------------------------
#
# Table structure for table `mail_queue_seq`
#
CREATE TABLE mail_queue_seq (
id int(10) unsigned NOT NULL auto_increment,
)
# --------------------------------------------------------
#
# Table structure for table `mail_queue_users`
#
CREATE TABLE mail_queue_users (
id int(11) NOT NULL auto_increment,
username varchar(255) NOT NULL default 'unknown',
)
example:
<?PHP
include '/path/to/mailq.php';
$SENDER_ID = 42;
mailq($to, $subject, $body, $headers);
?>
Copy & Paste
|