Code Snippets

  

PHP Source Code


Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 107,710 PHP Programmers for FREE! Ask your question and get quick answers from experts. There are 1,108 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




Pear Mail_Queue interface

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.

Submitted By: cyberscribe
Actions:
Rating:
Views: 6,988

Language: PHP

Last Modified: March 2, 2005
Instructions: run the commands in mail.sql on your MySQL database, then include the mailq.php file in any script using mail() and replace the mail() function with a call to mailq(). Then call send.php in php using a scheduler like cron.

Snippet


  1. file mailq.php
  2.  
  3. <?PHP
  4. /**mailq.php
  5. *
  6. * <p>
  7. * This package represents a simple interface to the functionality
  8. * of Pear Mail::Queue by creating a function, mailq(), that can
  9. * replace the built-in mail() function.
  10. * </p>
  11. * <ul>
  12. * <li>The mailq() function queues messages for later delivery.
  13. * <li>The queue is described in the file mail.sql.
  14. * <li>The mailq() function is defined in mailq.php.
  15. * <li>The send capability is invoked by running send.php.
  16. * <li>The file config.php defines the data connection and mail type
  17. * for mailq.php and send.php.
  18. * <li>A special variable, $SENDER_ID can be defined before calling mailq()
  19. * to populate the user_id field
  20. * </ul>
  21. *
  22. * @package mailq
  23. * @author Robert Peake <robert@peakepro.com>
  24. * @copyright © 2004
  25. * @link http://www.robertpeake.com/
  26. * @license http://www.php.net/license/3_0.txt
  27. * @version 0.1
  28. */
  29. /**
  30. * @see mailq_config.php
  31. */
  32. require_once('mailq_config.php'); //replace this with the full path to the file config.php
  33. /**function mailq
  34. *
  35. * Queues messages in the data structure defined by mailq_config.php.
  36. * Returns TRUE on success, FALSE otherwise
  37. * @see mailq_config.php
  38. *
  39. * @access public
  40. * @param string $to
  41. * @param string $subject
  42. * @param string $body
  43. * @param string $headers
  44. * @return bool
  45. */
  46. function mailq($to,$subject,$body,$headers='') {
  47.   global $SENDER_ID;
  48.   global $db_options;
  49.   global $mail_options;
  50.   if(!$SENDER_ID) $SENDER_ID = -1;
  51.   $hdrs = headers_to_array($headers);
  52.   $hdrs['Subject'] = $subject;
  53.   $from = $hdrs['From'];
  54.   if(!$from) {
  55.     $from = $_SERVER['SERVER_ADMIN'];
  56.     $hdrs['From'] = $from;
  57.   }
  58.   $mail_queue =& new Mail_Queue($db_options, $mail_options);
  59.   return $mail_queue->put( $from,$to, $hdrs, $body ,0,FALSE,$SENDER_ID);
  60. }
  61. /**function fold_headers
  62. *
  63. * RFC 822 compliant header wrapping
  64. * wraps crlf followed by any number of lwsp
  65. * to a single space ( )
  66. * returns string representing wrapped header
  67. *
  68. * @access public
  69. * @param string $headers
  70. * @return string
  71. */
  72. function fold_headers($headers) {
  73.   global $CRLF;
  74.   global $LWSP;
  75.   return preg_replace("/$CRLF$LWSP+/"," ",$headers);
  76. }
  77. /**function headers_to_array
  78. *
  79. * RFC 822 compliant header parsing
  80. * header key is delimited by colon (:) followed by any number of lwsp
  81. * headers are delimited by a single crlf
  82. * returns an array suitable for passing to Mail::Queue->put().
  83. * relies on the fold_headers() function to fold headers.
  84. * @see fold_headers
  85. *
  86. * @access public
  87. * @param string $headers
  88. * @return array
  89. */
  90. function headers_to_array($headers) {
  91.   global $CRLF;
  92.   global $LWSP;
  93.   $headers = fold_headers($headers);
  94.   $array = preg_split("/$CRLF/",$headers,0,PREG_SPLIT_NO_EMPTY);
  95.   for($i=0;$i<sizeof($array);$i++) {
  96.     $header_line = $array[$i];
  97.     $header_array = preg_split("/:$LWSP+/",$header_line);
  98.     $key = $header_array[0];
  99.     $value = $header_array[1];
  100.     $return_array[$key] = $value;
  101.   }
  102.   return $return_array;
  103. }
  104. ?>
  105.  
  106. file mailq_config.php
  107.  
  108. <?php
  109. /**mailq_config.php
  110. *
  111. * This file defines $CRLF, $LWSP, $db_options, $mail_options
  112. * and optionally defines the is_a function for php < 4.2.0.
  113. * This file also requires the Mail::Queue library
  114. *
  115. * @package mailq
  116. * @see mailq.php
  117. */
  118. $CRLF = '(\n|\r\n)'; //RFC 822 defines CRLF strictly as \r\n, however many UNIX MTAs seem to prefer \n, so both are accepted
  119. $LWSP = '([ ]|\t)'; //RCF 822 defines LWSP as space or tab only
  120.  
  121. if(!function_exists('is_a')) { //is_a is defined for PHP > 4.2.0 and required by Mail::Queue
  122.   /**function is_a
  123.    *
  124.    * PHP defined version of built-in function is_a present in PHP > 4.2.0
  125.    * returns TRUE if $object is of type $name, FALSE otherwise
  126.    *
  127.    * @access public
  128.    * @param string $name
  129.    * @param object $object
  130.    * @return bool
  131.   */
  132.   function is_a($object,$name) {
  133.     if($name == get_class($object)) {
  134.       return TRUE;
  135.     } else {
  136.       return FALSE;
  137.     }
  138.   }
  139. }
  140. require_once "Mail/Queue.php";
  141.  
  142. $db_options['type']       = 'db';
  143. $db_options['dsn']        = 'mysql://user:password@host/mail'; //replace 'user' with username, 'password' with password, 'host' with host; 'mail' is the database
  144. $db_options['mail_table'] = 'mail_queue'; //this is the table
  145.  
  146. $mail_options['driver']   = 'smtp';
  147. $mail_options['host']     = 'mail.yourcompany.com'; //MTA host name
  148. $mail_options['port']     = 25;
  149. $mail_options['auth']     = true; //true for SMTP auth
  150. $mail_options['username'] = 'mail_queue'; //SMTP login
  151. $mail_options['password'] = 'password'; //SMTP password
  152. ?>
  153.  
  154. file send.php
  155.  
  156. <?php
  157. /**send.php
  158. *
  159. * This script defines $max_amount_mails, which defines the max number of
  160. * messages to be sent at one time. When called (from the command line or
  161. * from a browser) the script sends messages from the Mail::Queue data
  162. * structure defined in mailq_config.php.
  163. *
  164. * @package mailq
  165. * @see mailq.php
  166. */
  167. /**
  168. * @see mailq_config.php
  169. */
  170. include 'mailq_config.php'; //this must be the full path to the config.php file
  171.  
  172. /* How many mails could we send each time the script is called */
  173. $max_amount_mails = 50;
  174.  
  175. /* we use the db_options and mail_options from the config again  */
  176. $mail_queue =& new Mail_Queue($db_options, $mail_options);
  177.  
  178. /* really sending the messages */
  179. $mail_queue->sendMailsInQueue($max_amount_mails);
  180. ?>
  181.  
  182.  
  183. # Database : `mail`
  184. # --------------------------------------------------------
  185.  
  186. #
  187. # Table structure for table `mail_queue`
  188. #
  189.  
  190. CREATE TABLE mail_queue (
  191.   id bigint(20) NOT NULL default '0',
  192.   create_time datetime NOT NULL default '0000-00-00 00:00:00',
  193.   time_to_send datetime NOT NULL default '0000-00-00 00:00:00',
  194.   sent_time datetime default NULL,
  195.   id_user bigint(20) NOT NULL default '0',
  196.   ip varchar(20) NOT NULL default 'unknown',
  197.   sender varchar(50) NOT NULL default '',
  198.   recipient varchar(50) NOT NULL default '',
  199.   headers text NOT NULL,
  200.   body longtext NOT NULL,
  201.   try_sent tinyint(4) NOT NULL default '0',
  202.   delete_after_send tinyint(1) NOT NULL default '1',
  203.   PRIMARY KEY  (id),
  204.   KEY id (id),
  205.   KEY time_to_send (time_to_send),
  206.   KEY id_user (id_user)
  207. )
  208. # --------------------------------------------------------
  209.  
  210. #
  211. # Table structure for table `mail_queue_seq`
  212. #
  213.  
  214. CREATE TABLE mail_queue_seq (
  215.   id int(10) unsigned NOT NULL auto_increment,
  216.   PRIMARY KEY  (id)
  217. )
  218. # --------------------------------------------------------
  219.  
  220. #
  221. # Table structure for table `mail_queue_users`
  222. #
  223.  
  224. CREATE TABLE mail_queue_users (
  225.   id int(11) NOT NULL auto_increment,
  226.   username varchar(255) NOT NULL default 'unknown',
  227.   PRIMARY KEY  (id)
  228. )
  229.  
  230. example:
  231.  
  232. <?PHP
  233. include '/path/to/mailq.php';
  234. $SENDER_ID = 42;
  235. mailq($to, $subject, $body, $headers);
  236. ?>

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month