Zend - The PHP Company




Email

Add Code


Mass email solution  

Type: code fragment
Added by: ctafuro
Entered: 06/02/2003
Last modified: 02/12/2004
Rating: **** (6 votes)
Views: 12448
mass_email_function is a working mass email solution for web application. It solves the problem of script execution timeout that can happen due to the long time the process of sending e-mails require. Moreover, the proposed solution offers the feature of email delivery scheduling.


<?php

/**************************************************************************
*
* Updated: 11:30 a.m. CET (GMT + 1.00h) November 9, 2004
* Author: Carlo Tafuro <carlo.tafuro@poste.it>
*
* funtion mass_email_function($arg_array, $mail_to_array)
*
*   mass_email_function is a working mass email solution for web application.
*   It solves the problem of script execution timeout that can happen due to
*   the long time the process of sending e-mails require.
*
*   Moreover, the proposed solution offers
*   the feature of email delivery scheduling.
*
*
* example of usage:
*
*
*    $arg = array();
*
*    $arg['mail_from_addr']   = 'roby@example.com';
*    $arg['mail_from_name']   = 'Roby';
*    $arg['mail_subject']     = 'mass email solution';
*
*    $arg['mail_body']        = 'mass_email_function is a working mass ';
*    $arg['mail_body']       .= 'email solution for web application, ';
*    $arg['mail_body']       .= 'this function solve the problem due to ';
*    $arg['mail_body']       .= 'the maximum time in seconds a script ';
*    $arg['mail_body']       .= 'is allowed to run before it is ';
*    $arg['mail_body']       .= 'terminated by the parser.';
*
*    // $mail_start_date format: mm/dd/yy
*    $arg['mail_start_date']  = date('m/d/y', time());
*
*    // $mail_start_time format: hh:mm - hour
*    // 24-hour format; i.e. '00' to '23'
*    $arg['mail_start_time']  = date('H:i', time());
*
*    $arg['debug']  = 0;
*
*    $target = array();
*
*    $target['Thomas'] = 'thomas@example.com';
*    $target['Mary']   = 'mary@example.com';
*    $target['Paul']   = 'paul@example.com';
*
*    mass_email_function($arg, $target);
*
***************************************************************************/
function mass_email_function($arg_array$mail_to_array) {

$mail_from_addr       $arg_array['mail_from_addr'];
$mail_from_name       $arg_array['mail_from_name'];
$mail_subject         $arg_array['mail_subject'];
$mail_body            $arg_array['mail_body'];

// $mail_start_date format: mm/dd/yy
$mail_start_date      $arg_array['mail_start_date'];

// $mail_start_time format: hh:mm - hour
// 24-hour format; i.e. '00' to '23'
$mail_start_time      $arg_array['mail_start_time'];

$debug                $arg_array['debug']; //boolean

$sendmail_binary      '/usr/sbin/sendmail';
$at_binary            '/usr/bin/at';
$here_doc_identifier  'This_is_a_limit_string_sufficiently_unusual_that_it_will_not_occur_anywhere_in_the_text';

if (empty(
$mail_start_date)) { $mail_start_date date('m/d/y'time()); }
if (empty(
$mail_start_time)) { $mail_start_time date('H:i'time()); }

if (
$debug) {

   
$at_pipe popen ('cat > debug.txt','w');
   
$debug_comment  '# to debug this script at shell prompt type: ';
   
$debug_comment .= $at_binary.' '.$mail_start_time.' '.$mail_start_date;
   
$debug_comment .= ' < debug.txt'.chr(10);
   
fwrite ($at_pipe$debug_comment);

} else {

   
$at_pipe popen ($at_binary.' '.$mail_start_time.' '.$mail_start_date,'w');
}

$shell_script  chr(10);
$shell_script .= 'send_mail_to () {'.chr(10);
$shell_script .= chr(10);
$shell_script .= $sendmail_binary.' -i -t -f '.$mail_from_addr.' 1>/dev/null <<'.$here_doc_identifier.chr(10);
$shell_script .= 'Subject: '.$mail_subject.chr(10);
$shell_script .= 'From: "'.escapeshellcmd($mail_from_name).'" <'.escapeshellcmd($mail_from_addr).'>'.chr(10);
$shell_script .= 'To: "$1" <$2>'.chr(10);
$shell_script .= $mail_body.chr(10);
$shell_script .= $here_doc_identifier.chr(10);
$shell_script .= chr(10);
$shell_script .= 'return 0'.chr(10);
$shell_script .= '}'.chr(10);
$shell_script .= chr(10);

fwrite ($at_pipe$shell_script);
fwrite ($at_pipechr(10).chr(10).'# start to send_mail_to function calls '.chr(10));

reset ($mail_to_array);
while (list (
$name$email) = each ($mail_to_array)) {

    
fwrite ($at_pipe'send_mail_to '.escapeshellarg($name).' '.escapeshellarg($email).chr(10));
}

fwrite ($at_pipechr(10).'exit 0'.chr(10));
pclose ($at_pipe);

return 
True;
}

?>


Usage Example


<?php

$arg 
= array();

$arg['mail_from_addr']   = 'roby@example.com';
$arg['mail_from_name']   = 'Roby';
$arg['mail_subject']     = 'mass email solution';

$arg['mail_body']        = 'mass_email_function is a working mass ';
$arg['mail_body']       .= 'email solution for web application, ';
$arg['mail_body']       .= 'this function solve the problem due to ';
$arg['mail_body']       .= 'the maximum time in seconds a script ';
$arg['mail_body']       .= 'is allowed to run before it is ';
$arg['mail_body']       .= 'terminated by the parser.';

// $mail_start_date format: mm/dd/yy
$arg['mail_start_date']  = date('m/d/y'time());

// $mail_start_time format: hh:mm - hour
// 24-hour format; i.e. '00' to '23'
$arg['mail_start_time']  = date('H:i'time());

$arg['debug']  = 0;

$target = array();

$target['Thomas'] = 'thomas@example.com';
$target['Mary']   = 'mary@example.com';
$target['Paul']   = 'paul@example.com';

mass_email_function($arg$target);

?>


Rate This Script





Search



This Category All Categories