<?php 

/************************************************************************** 
* 
* Updated: 06:30 p.m. CET (GMT + 1.00h) March 22, 2003 
* 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,\n"; 
*    $arg["mail_body"]       .= "this function solve the problem due to "; 
*    $arg["mail_body"]       .= "the maximum time in seconds a script\n"; 
*    $arg["mail_body"]       .= "is allowed to run before it is "; 
*    $arg["mail_body"]       .= "terminated by the parser.\n"; 
* 
*    // $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\n"; 
   fwrite ($at_pipe, $debug_comment); 

} else { 

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

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

fwrite ($at_pipe, $shell_script); 
fwrite ($at_pipe, "\n\n# start to send_mail_to function calls \n"); 

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

    fwrite ($at_pipe, "send_mail_to '".$name."' ".$email."\n"); 
} 

fwrite ($at_pipe, "\nexit 0\n"); 
pclose ($at_pipe); 

return True; 
} 

?> 

 
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,\n"; 
$arg["mail_body"]       .= "this function solve the problem due to "; 
$arg["mail_body"]       .= "the maximum time in seconds a script\n"; 
$arg["mail_body"]       .= "is allowed to run before it is "; 
$arg["mail_body"]       .= "terminated by the parser.\n"; 

// $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); 

?>  
