2006-12-02 04:36:16 +00:00
< ? php
/*
+ ----------------------------------------------------------------------------+
| e107 website system
|
| ©Steve Dunstan 2001 - 2002
| http :// e107 . org
| jalist @ e107 . org
|
| Released under the terms and conditions of the
| GNU General Public License ( http :// gnu . org ) .
|
| $Source : / cvs_backup / e107_0 . 8 / e107_handlers / phpmailer / mailout_process . php , v $
2007-12-22 14:49:34 +00:00
| $Revision : 1.6 $
| $Date : 2007 - 12 - 22 14 : 49 : 28 $
2007-04-24 20:29:11 +00:00
| $Author : e107steved $
2007-12-22 14:49:34 +00:00
|
| Modifications in hand to work with most recent mailout . php
To do :
1. Decide final resting place for log - possibly accumulate , and write to an admin log entry ?
2. Admin log entries ?
3. Better vetting on 'cancel' button ?
4. Option to add user name in subject line - support |...| and { ... } - done ; test
5. Strip bbcode from plain text emails ( ideally needs updated parser ) .
6. Support phpmailer 2.0 options
7. Log cancellation of email run
|
2006-12-02 04:36:16 +00:00
+----------------------------------------------------------------------------+
*/
require_once ( " ../../class2.php " );
if ( ! getperms ( " W " )){ header ( " location: " . e_BASE . " index.php " ); }
include_lan ( e_LANGUAGEDIR . e_LANGUAGE . " /admin/lan_mailout.php " );
2007-12-22 14:49:34 +00:00
// Directory for log (if enabled)
define ( 'MAIL_LOG_PATH' , e_PLUGIN . " log/logs/ " );
2006-12-02 04:36:16 +00:00
$HEADER = " " ;
$FOOTER = " " ;
2007-12-22 14:49:34 +00:00
define ( " e_PAGETITLE " , LAN_MAILOUT_60 );
2006-12-02 04:36:16 +00:00
require_once ( HEADERF );
set_time_limit ( 18000 );
session_write_close ();
2007-12-22 14:49:34 +00:00
// $logenable - 0 = log disabled, 1 = 'dry run' (debug and log, no send). 2 = 'log all' (send, and log result). 3 = 'dry run' with failures
// $add_email - 1 includes email detail in log
list ( $logenable , $add_email ) = explode ( ',' , varset ( $pref [ 'mail_log_options' ], '0,0' ));
2006-12-02 04:36:16 +00:00
2007-12-22 14:49:34 +00:00
if ( $_POST [ 'cancel_emails' ])
{
2006-12-02 04:36:16 +00:00
$sql -> db_Delete ( " generic " , " gen_datestamp=' " . intval ( $_POST [ 'mail_id' ]) . " ' " );
2007-12-22 14:49:34 +00:00
$text = " <div style='text-align:center;width:220px'><br /> " . LAN_MAILOUT_66 ; // Cancelled Successfully;
2006-12-02 04:36:16 +00:00
$text .= " <div style='text-align:center;margin-left:auto;margin-right:auto;position:absolute;left:10px;top:110px'>
< br />< input type = 'button' class = 'button' name = 'close' value = 'Close' onclick = \ " window.close() \" />
</ div ></ div > " ;
2007-12-22 14:49:34 +00:00
$ns -> tablerender ( LAN_MAILOUT_59 , $text );
2006-12-02 04:36:16 +00:00
echo " </body></html> " ;
exit ;
}
2007-12-22 14:49:34 +00:00
ob_implicit_flush ();
if ( e_QUERY )
{
$tmp = explode ( '.' , e_QUERY );
$mail_id = intval ( varset ( $tmp [ 0 ], 0 )); // ID in 'generic' table corresponding to the recipient entries
$mail_text_id = intval ( varset ( $tmp [ 1 ], 0 )); // Record number in 'generic' table corresponding to the email data
}
else
{
$mail_id = intval ( varset ( $_POST [ 'mail_id' ], 0 )); // ID in 'generic' table corresponding to the recipient entries
$mail_text_id = intval ( varset ( $_POST [ 'mail_text_id' ], 0 )); // ID in 'generic' table corresponding to the recipient entries
}
if (( $mail_id == 0 ) || ( $mail_text_id == 0 ))
{
echo " Invalid parameters: { $mail_id } , { $mail_text_id } !<br /> " ;
exit ;
}
// Get the email itself from the 'generic' table
$qry = " SELECT * FROM #generic WHERE `gen_id` = { $mail_text_id } AND gen_type='savemail' and gen_datestamp = ' " . $mail_id . " ' " ;
if ( ! $sql -> db_Select_gen ( $qry ))
{
echo " Email not found<br /> " ;
exit ;
}
if ( ! $row = $sql -> db_Fetch ())
{
echo " Can't read email<br /> " ;
exit ;
}
$email_info = unserialize ( $row [ 'gen_chardata' ]); // Gives us sender_name, sender_email, email_body
//--------------------------------------------------
// Configure mailout handler (PHPMailer or other)
//--------------------------------------------------
2006-12-02 04:36:16 +00:00
require ( e_HANDLER . " phpmailer/class.phpmailer.php " );
$mail = new PHPMailer ();
2007-12-22 14:49:34 +00:00
$mail -> From = varsettrue ( $email_info [ 'sender_email' ], $pref [ 'siteadminemail' ]);
$mail -> FromName = varsettrue ( $email_info [ 'sender_name' ], $pref [ 'siteadmin' ]);
2006-12-02 04:36:16 +00:00
// $mail->Host = "smtp1.site.com;smtp2.site.com";
if ( $pref [ 'mailer' ] == 'smtp' )
{
$mail -> Mailer = " smtp " ;
2007-12-22 14:49:34 +00:00
$mail -> SMTPKeepAlive = varsettrue ( $pref [ 'smtp_keepalive' ]) ? TRUE : FALSE ;
2006-12-02 04:36:16 +00:00
if ( $pref [ 'smtp_server' ])
{
$mail -> Host = $pref [ 'smtp_server' ];
}
if ( $pref [ 'smtp_username' ] && $pref [ 'smtp_password' ])
{
$mail -> SMTPAuth = TRUE ;
$mail -> Username = $pref [ 'smtp_username' ];
$mail -> Password = $pref [ 'smtp_password' ];
$mail -> PluginDir = e_HANDLER . " phpmailer/ " ;
}
}
elseif ( $pref [ 'mailer' ] == 'sendmail' )
{
$mail -> Mailer = " sendmail " ;
$mail -> Sendmail = ( $pref [ 'sendmail' ]) ? $pref [ 'sendmail' ] : " /usr/sbin/sendmail -t -i -r " . $pref [ 'siteadminemail' ];
}
else
{
$mail -> Mailer = " mail " ;
}
2007-12-22 14:49:34 +00:00
$message_subject = stripslashes ( $tp -> toHTML ( $email_info [ 'email_subject' ], FALSE , RAWTEXT ));
2006-12-02 04:36:16 +00:00
$mail -> WordWrap = 50 ;
2006-12-22 21:01:42 +00:00
$mail -> CharSet = CHARSET ;
2006-12-02 04:36:16 +00:00
$mail -> IsHTML ( TRUE );
$mail -> SMTPDebug = ( e_MENU == " debug " ) ? TRUE : FALSE ;
2007-12-22 14:49:34 +00:00
if ( $email_info [ 'copy_to' ])
2006-12-02 04:36:16 +00:00
{
2007-12-22 14:49:34 +00:00
$tmp = explode ( " , " , $email_info [ 'copy_to' ]);
2006-12-02 04:36:16 +00:00
foreach ( $tmp as $addc )
{
2007-12-22 14:49:34 +00:00
$mail -> AddCC ( trim ( $addc ));
2006-12-02 04:36:16 +00:00
}
}
2007-12-22 14:49:34 +00:00
if ( $email_info [ 'bcopy_to' ])
2006-12-02 04:36:16 +00:00
{
2007-12-22 14:49:34 +00:00
$tmp = explode ( " , " , $email_info [ 'bcopy_to' ]);
foreach ( $tmp as $addc )
2006-12-02 04:36:16 +00:00
{
2007-12-22 14:49:34 +00:00
$mail -> AddBCC ( trim ( $addc ));
2006-12-02 04:36:16 +00:00
}
}
2007-12-22 14:49:34 +00:00
2006-12-02 04:36:16 +00:00
if ( $pref [ 'mail_bounce_email' ] != '' )
{
$mail -> Sender = $pref [ 'mail_bounce_email' ];
}
2007-12-22 14:49:34 +00:00
$attach = trim ( $email_info [ 'attach' ]);
2006-12-02 04:36:16 +00:00
if ( is_readable ( e_DOWNLOAD . $attach ))
{
$attach_link = e_DOWNLOAD . $attach ;
}
else
{
$attach_link = e_FILE . 'public/' . $attach ;
}
2007-08-02 20:37:11 +00:00
if (( $temp = strrchr ( $attach , '/' )) !== FALSE )
{ // Just specify filename as attachment - no path
$attach = substr ( $temp , 1 );
}
2006-12-02 04:36:16 +00:00
if ( $attach != " " && ! $mail -> AddAttachment ( $attach_link , $attach ))
{
2007-12-22 14:49:34 +00:00
$mss = LAN_MAILOUT_58 . " <br /> { $attach_link } -> { $attach } " ; // problem with attachment.
2006-12-02 04:36:16 +00:00
$ns -> tablerender ( " Error " , $mss );
exit ;
}
// ---------------------------- Setup the Email ----------------------------->
$mail_head = " <!DOCTYPE html PUBLIC \" -//W3C//DTD XHTML 1.1//EN \" \" http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd \" > \n " ;
$mail_head .= " <html xmlns='http://www.w3.org/1999/xhtml' > \n " ;
2007-12-22 14:49:34 +00:00
$mail_head .= " <head><meta http-equiv='content-type' content='text/html; charset= " . CHARSET . " ' /> \n " ;
2006-12-02 04:36:16 +00:00
2007-12-22 14:49:34 +00:00
if ( varsettrue ( $email_info [ 'use_theme' ]))
2006-12-02 04:36:16 +00:00
{
$theme = $THEMES_DIRECTORY . $pref [ 'sitetheme' ] . " / " ;
$style_css = file_get_contents ( e_THEME . $pref [ 'sitetheme' ] . " /style.css " );
$mail_head .= " <style> \n " . $style_css . " \n </style> " ;
$message_body = $mail_head ;
$message_body .= " </head> \n <body> \n " ;
$message_body .= " <div style='padding:10px;width:97%'><div class='forumheader3'> \n " ;
2007-12-22 14:49:34 +00:00
$message_body .= $tp -> toEmail ( $email_info [ 'email_body' ]) . " </div></div></body></html> " ;
2006-12-02 04:36:16 +00:00
}
else
{
$message_body = $mail_head ;
$message_body .= " </head> \n <body> \n " ;
2007-12-22 14:49:34 +00:00
$message_body .= $tp -> toEmail ( $email_info [ 'email_body' ]) . " </body></html> " ;
2006-12-02 04:36:16 +00:00
$message_body = str_replace ( " " " , '"' , $message_body );
$message_body = str_replace ( 'src="' , 'src="' . SITEURL , $message_body );
}
$message_body = stripslashes ( $message_body );
// ---------------- Display Progress and Send Emails. ----------------------->
2007-12-22 14:49:34 +00:00
echo " <div class='fcaption'> " . LAN_MAILOUT_59 . " </div> " ;
// $qry = "SELECT g.*,u.* FROM #generic AS g LEFT JOIN #user AS u ON g.gen_user_id = u.user_id WHERE g.gen_type='sendmail' and g.gen_datestamp = '".intval($_POST['mail_id'])."' ";
// All the user info is in the generic table now - simplifies the query a bit
$qry = " SELECT g.* FROM #generic AS g WHERE g.gen_type='sendmail' and g.gen_datestamp = ' " . $mail_id . " ' " ;
2006-12-02 04:36:16 +00:00
$count = $sql -> db_Select_gen ( $qry );
2007-12-22 14:49:34 +00:00
// echo date("H:i:s d.m.y")." Start of mail run by ".USERNAME." - {$count} emails to go. ID: {$mail_id}. Subject: ".$mail_subject."<br />";
2006-12-02 04:36:16 +00:00
if ( ! $count )
{
2007-12-22 14:49:34 +00:00
echo " <div style='text-align:center;width:200px'><br /> " . LAN_MAILOUT_61 . " </div> " ;
2006-12-02 04:36:16 +00:00
echo " </body></html> " ;
echo " <div style='text-align:center;margin-left:auto;margin-right:auto;position:absolute;left:10px;top:110px'>
< input type = 'button' class = 'button' name = 'close' value = 'Close' onclick = \ " window.close() \" />
</ div > " ;
exit ;
}
$c = 0 ; $d = 0 ;
2007-12-22 14:49:34 +00:00
$cur = 0 ;
$send_ok = 0 ; $send_fail = 0 ;
2006-12-02 04:36:16 +00:00
$pause_count = 1 ;
$pause_amount = ( $pref [ 'mail_pause' ]) ? $pref [ 'mail_pause' ] : 10 ;
$pause_time = ( $pref [ 'mail_pausetime' ]) ? $pref [ 'mail_pausetime' ] : 1 ;
2007-12-22 14:49:34 +00:00
$unit = ( 1 / $count ) * 100 ; // Percentage 'weight' of each email
echo " <div class='blocks' style='text-align:left;width:199px'><div id='bar' class='bar' style='border:0px;width: " . $cur . " %' > </div></div> " ;
echo " <div class='percents'><span id='numbers'> " . ( $c + 1 ) . " / " . $count . " ( " . $cur . " </span>%) " . LAN_MAILOUT_117 . " </div> " ;
2006-12-02 04:36:16 +00:00
stopwatch ();
2007-12-22 14:49:34 +00:00
// Debug/mailout log
if ( $logenable )
{
$logfilename = MAIL_LOG_PATH . 'mailoutlog.txt' ;
$loghandle = fopen ( $logfilename , 'a' ); // Always append to file
fwrite ( $loghandle , " =====----------------------------------------------------------------------------------===== \r \n " );
fwrite ( $loghandle , date ( " H:i:s d.m.y " ) . " Start of mail run by " . USERNAME . " - { $count } emails to go. ID: { $mail_id } . Subject: " . $mail_subject . " \r \n " );
if ( $add_email )
{
fwrite ( $loghandle , " From: " . $mail -> From . ' (' . $mail -> FromName . " ) \r \n " );
fwrite ( $loghandle , " Subject: " . $mail -> Subject . " \r \n " );
fwrite ( $loghandle , " CC: " . $email_info [ 'copy_to' ] . " \r \n " );
fwrite ( $loghandle , " BCC: " . $email_info [ 'bcopy_to' ] . " \r \n " );
fwrite ( $loghandle , " Attach: " . $attach . " \r \n " );
fwrite ( $loghandle , " Body: " . $email_info [ 'email_body' ] . " \r \n " );
fwrite ( $loghandle , " ----------------------------------------------------------- \r \n " );
}
}
2007-02-17 18:18:50 +00:00
while ( $row = $sql -> db_Fetch ())
{
2007-12-22 14:49:34 +00:00
//-------------------------------
// Send one email
//-------------------------------
$mail_info = unserialize ( $row [ 'gen_chardata' ]); // Has most of the info needed
2006-12-02 04:36:16 +00:00
2007-12-22 14:49:34 +00:00
$activator = ( substr ( SITEURL , - 1 ) == " / " ? SITEURL . " signup.php?activate. " . $row [ 'gen_user_id' ] . " . " . $mail_info [ 'user_signup' ] : SITEURL . " /signup.php?activate. " . $row [ 'gen_user_id' ] . " . " . $mail_info [ 'user_signup' ]);
$signup_link = ( $mail_info [ 'user_signup' ]) ? " <a href=' { $activator } '> { $activator } </a> " : " " ;
2006-12-02 04:36:16 +00:00
2007-12-22 14:49:34 +00:00
// Allow username in subject
$mail_subject = str_replace ( array ( '|USERNAME|' , '{USERNAME}' ), $mail_info [ 'user_name' ], $message_subject );
$mail -> Subject = $mail_subject ;
2006-12-02 04:36:16 +00:00
2007-12-22 14:49:34 +00:00
// Allow username, userID, signup link in body
$search = array ( '|USERNAME|' , '|USERID|' , '|SIGNUP_LINK|' );
$replace = array ( $mail_info [ 'user_name' ], $row [ 'gen_user_id' ], $signup_link );
2006-12-02 04:36:16 +00:00
2007-12-22 14:49:34 +00:00
$mes_body = str_replace ( $search , $replace , $message_body );
$alt_body = str_replace ( $search , $replace , stripslashes ( $tp -> toText ( $email_info [ 'email_body' ])));
2006-12-02 04:36:16 +00:00
2007-12-22 14:49:34 +00:00
$mail -> Body = $mes_body ;
$mail -> AltBody = $alt_body ;
2006-12-02 04:36:16 +00:00
2007-12-22 14:49:34 +00:00
$mail -> AddAddress ( $mail_info [ 'user_email' ], $mail_info [ 'user_name' ]);
if ( $row [ 'gen_user_id' ])
{
$mail_custom = $row [ 'gen_user_id' ];
}
else
{
$mail_custom = md5 ( $mail_info [ 'user_name' ] . $mail_info [ 'user_email' ]);
}
$mail_custom = " X-e107-id: " . $mail_id . '/' . $mail_custom ;
$mail -> AddCustomHeader ( $mail_custom );
2006-12-02 04:36:16 +00:00
2007-12-22 14:49:34 +00:00
$debug_message = '' ;
if (( $logenable == 0 ) || ( $logenable == 2 ))
{ // Actually send email
$mail_result = $mail -> Send ();
}
else
{ // Debug mode - decide result of email here
$mail_result = TRUE ;
if (( $logenable == 3 ) && (( $c % 7 ) == 4 )) $mail_result = FALSE ; // Fail one email in 7 for testing
$debug_message = 'Debug' ;
}
if ( $mail_result )
{
$send_ok ++ ;
$sql2 -> db_Delete ( 'generic' , " gen_id= { $row [ 'gen_id' ] } " ); // Mail sent - delete from database
}
else
{
$send_fail ++ ;
$mail_info [ 'send_result' ] = 'Fail: ' . $mail -> ErrorInfo . $debug_message ;
$temp = serialize ( $mail_info );
// Log any error info we can
$sql2 -> db_Update ( 'generic' , " `gen_chardata`=' { $temp } ' WHERE gen_id= { $row [ 'gen_id' ] } " );
}
if ( $logenable )
{
fwrite ( $loghandle , date ( " H:i:s d.m.y " ) . " Send to { $mail_info [ 'user_name' ] } at { $mail_info [ 'user_email' ] } Mail-ID= { $mail_custom } - { $mail_result } \r \n " );
2006-12-02 04:36:16 +00:00
}
$mail -> ClearAddresses ();
2007-02-17 18:18:50 +00:00
$mail -> ClearCustomHeaders ();
2006-12-02 04:36:16 +00:00
2007-12-22 14:49:34 +00:00
// --------- One email sent
2006-12-02 04:36:16 +00:00
$cur = round ((( $c / $count ) * 100 ) + $unit );
2007-12-22 14:49:34 +00:00
// Do we need next line?
// echo str_pad(' ',4096)."<br />\n"; // Put out lots of spaces and a newline - works wonders for XHTML compliance!
// $d = ($c==0) ? 10 : round($width + $d); // Line doesn't do anything
2006-12-02 04:36:16 +00:00
2007-12-22 14:49:34 +00:00
// echo "<div class='percents'>".($c+1)." / ".$count." (" . $cur . "%) ".LAN_MAILOUT_117."</div>";
echo " <script type='text/javascript'>setnum(' " . ( $c + 1 ) . " ',' { $count } ',' { $cur } ');</script> \n " ;
2006-12-02 04:36:16 +00:00
2007-12-22 14:49:34 +00:00
/* if ( $cur != $prev )
{ // Update 'completed' segment of progress bar
echo " <script type='text/javascript'>inc(' " . $cur . " %');</script> \n " ;
2006-12-02 04:36:16 +00:00
}
$prev = $cur ;
2007-12-22 14:49:34 +00:00
*/ ob_flush ();
2006-12-02 04:36:16 +00:00
flush ();
2007-12-22 14:49:34 +00:00
if ( $pause_count > $pause_amount )
{
sleep ( $pause_time );
$pause_count = 1 ;
2006-12-02 04:36:16 +00:00
}
// Default sleep to reduce server-load: 1 second.
sleep ( 1 );
$c ++ ;
$pause_count ++ ;
}
ob_end_flush ();
echo " <div style='position:absolute;left:10px;top:50px'><br /> " ;
2007-12-22 14:49:34 +00:00
echo LAN_MAILOUT_62 . " " . $send_ok . " <br /> " ;
echo LAN_MAILOUT_63 . " " . $send_fail . " <br /> " ;
echo LAN_MAILOUT_64 . " " . stopwatch () . " " . LAN_MAILOUT_65 . " <br /> " ;
2006-12-02 04:36:16 +00:00
echo " </div> " ;
2007-12-22 14:49:34 +00:00
// Complete - need to log something against the mailshot entry, and maybe write an admin log entry.
$log_string = date ( " H:i:s d.m.y " ) . " End of " . ( $logenable == 1 ? 'debug ' : '' ) . " mail run by " . USERNAME . " - { $send_ok } succeeded, { $send_fail } failed. Subject: " . $mail_subject ;
if ( ! is_array ( $email_info [ 'send_results' ])) $email_info [ 'send_results' ] = array ();
$email_info [ 'send_results' ][] = $log_string ;
$sql -> db_Update ( 'generic' , " `gen_chardata`=' " . serialize ( $email_info ) . " ' WHERE `gen_id` = { $mail_text_id } AND `gen_type`='savemail' and `gen_datestamp` = ' " . $mail_id . " ' " );
2006-12-02 04:36:16 +00:00
$mail -> ClearAttachments ();
2007-12-22 14:49:34 +00:00
if ( $pref [ 'mailer' ] == 'smtp' )
{
$mail -> SmtpClose ();
2006-12-02 04:36:16 +00:00
}
echo " <div style='text-align:center;margin-left:auto;margin-right:auto;position:absolute;left:10px;top:110px'>
< br />< input type = 'button' class = 'button' name = 'close' value = 'Close' onclick = \ " window.close() \" />
</ div > " ;
echo " </body></html> " ;
2007-12-22 14:49:34 +00:00
if ( $logenable )
{
fwrite ( $loghandle , $log_string . " \r \n " );
fclose ( $loghandle );
}
2006-12-02 04:36:16 +00:00
function headerjs (){
$text = "
< style type = 'text/css' ><!--
div . percents div . blocks img . blocks {
margin : 1 px ;
height : 20 px ;
padding : 1 px ;
border : 1 px solid #000;
width : 199 px ;
background : #fff;
color : #000;
float : left ;
clear : right ;
z - index : 9 ;
position : relative ;
}
. percents {
background : #FFF;
border : 1 px solid #CCC;
margin : 1 px ;
height : 20 px ;
position : absolute ;
vertical - align : middle ;
width : 199 px ;
z - index : 10 ;
left : 10 px ;
top : 38 px ;
text - align : center ;
color : black ;
}
. blocks {
margin - top : 1 px ;
height : 21 px ;
position : absolute ;
z - index : 11 ;
left : 12 px ;
top : 38 px ;
}
. bar {
background : #EEE;
background - color : red ;
filter : alpha ( opacity = 50 );
height : 21 px ;
- moz - opacity : 0.5 ;
opacity : 0.5 ;
- khtml - opacity : . 5
}
-->
</ style > " ;
$text .= "
< script type = 'text/javascript' >
2007-12-22 14:49:34 +00:00
function inc ( amount )
{
document . getElementById ( 'bar' ) . style . width = amount ;
}
function setnum ( v1 , v2 , v3 )
{
this_el = document . getElementById ( 'numbers' );
if ( this_el ) this_el . innerHTML = v1 + ' / ' + v2 + ' (' + v3 ;
document . getElementById ( 'bar' ) . style . width = v3 + '%' ;
2006-12-02 04:36:16 +00:00
}
</ script > " ;
return $text ;
}
function stopwatch (){
static $mt_previous = 0 ;
list ( $usec , $sec ) = explode ( " " , microtime ());
$mt_current = ( float ) $usec + ( float ) $sec ;
if ( ! $mt_previous ) {
$mt_previous = $mt_current ;
return " " ;
} else {
$mt_diff = ( $mt_current - $mt_previous );
$mt_previous = $mt_current ;
return round ( sprintf ( '%.16f' , $mt_diff ), 2 );
}
}
?>