1
0
mirror of https://github.com/e107inc/e107.git synced 2025-07-30 19:30:25 +02:00

Bulk mailer - phase 1 - support add-in email address sources plus other features

This commit is contained in:
e107steved
2007-12-22 14:49:34 +00:00
parent 5937feaeb3
commit e1f3688bf3
5 changed files with 1576 additions and 564 deletions

View File

@@ -11,57 +11,116 @@
| GNU General Public License (http://gnu.org).
|
| $Source: /cvs_backup/e107_0.8/e107_handlers/phpmailer/mailout_process.php,v $
| $Revision: 1.5 $
| $Date: 2007-08-02 20:37:10 $
| $Revision: 1.6 $
| $Date: 2007-12-22 14:49:28 $
| $Author: e107steved $
|
| 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
|
+----------------------------------------------------------------------------+
*/
require_once("../../class2.php");
if(!getperms("W")){ header("location:".e_BASE."index.php"); }
include_lan(e_LANGUAGEDIR.e_LANGUAGE."/admin/lan_mailout.php");
// Directory for log (if enabled)
define('MAIL_LOG_PATH',e_PLUGIN."log/logs/");
$HEADER = "";
$FOOTER = "";
define("e_PAGETITLE",MAILAN_60);
define("e_PAGETITLE",LAN_MAILOUT_60);
require_once(HEADERF);
set_time_limit(18000);
session_write_close();
// $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'));
if($_POST['cancel_emails']){
if($_POST['cancel_emails'])
{
$sql -> db_Delete("generic", "gen_datestamp='".intval($_POST['mail_id'])."' ");
$text = "<div style='text-align:center;width:220px'><br />".MAILAN_66; // Cancelled Successfully;
$text = "<div style='text-align:center;width:220px'><br />".LAN_MAILOUT_66; // Cancelled Successfully;
$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>";
$ns -> tablerender(MAILAN_59, $text);
$ns -> tablerender(LAN_MAILOUT_59, $text);
echo "</body></html>";
exit;
}
ob_implicit_flush();
/*
if (ob_get_level() == 0) {
ob_start();
}
*/
// -------------------- Configure PHP Mailer ------------------------------>
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)
//--------------------------------------------------
require(e_HANDLER."phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = ($_POST['email_from_email'])? $_POST['email_from_email']: $pref['siteadminemail'];
$mail->FromName = ($_POST['email_from_name'])? $_POST['email_from_name']: $pref['siteadmin'];
$mail->From = varsettrue($email_info['sender_email'],$pref['siteadminemail']);
$mail->FromName = varsettrue($email_info['sender_name'], $pref['siteadmin']);
// $mail->Host = "smtp1.site.com;smtp2.site.com";
if ($pref['mailer']== 'smtp')
{
$mail->Mailer = "smtp";
$mail->SMTPKeepAlive = (isset($pref['smtp_keepalive']) && $pref['smtp_keepalive']==1) ? TRUE : FALSE;
$mail->SMTPKeepAlive = varsettrue($pref['smtp_keepalive']) ? TRUE : FALSE;
if($pref['smtp_server'])
{
$mail->Host = $pref['smtp_server'];
@@ -84,37 +143,40 @@ if($_POST['cancel_emails']){
$mail->Mailer = "mail";
}
$mail->AddCC = ($_POST['email_cc']);
$message_subject = stripslashes($tp -> toHTML($email_info['email_subject'],FALSE,RAWTEXT));
$mail->WordWrap = 50;
$mail->CharSet = CHARSET;
$mail->Subject = $_POST['email_subject'];
$mail->IsHTML(TRUE);
$mail->SMTPDebug = (e_MENU == "debug") ? TRUE : FALSE;
if($_POST['email_cc'])
if($email_info['copy_to'])
{
$tmp = explode(",",$_POST['email_cc']);
$tmp = explode(",",$email_info['copy_to']);
foreach($tmp as $addc)
{
$mail->AddCC($addc);
$mail->AddCC(trim($addc));
}
}
if($_POST['email_bcc'])
if($email_info['bcopy_to'])
{
$tmp = explode(",",$_POST['email_bcc']);
foreach($tmp as $addbc)
$tmp = explode(",",$email_info['bcopy_to']);
foreach($tmp as $addc)
{
$mail->AddBCC($addbc);
$mail->AddBCC(trim($addc));
}
}
if($pref['mail_bounce_email'] !='')
{
$mail->Sender = $pref['mail_bounce_email'];
}
$attach = trim($_POST['email_attachment']);
$attach = trim($email_info['attach']);
if(is_readable(e_DOWNLOAD.$attach))
{
@@ -132,9 +194,8 @@ if($_POST['cancel_emails']){
if ($attach != "" && !$mail->AddAttachment($attach_link, $attach))
{
$mss = MAILAN_58."<br />$attach_link"; // problem with attachment.
$mss = LAN_MAILOUT_58."<br />{$attach_link}->{$attach}"; // problem with attachment.
$ns->tablerender("Error", $mss);
// require_once(e_ADMIN."footer.php");
exit;
}
@@ -143,29 +204,26 @@ if($_POST['cancel_emails']){
// ---------------------------- Setup the Email ----------------------------->
$message_subject = stripslashes($tp -> toHTML($_POST['email_subject']));
$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";
$mail_head .= "<head><meta http-equiv='content-type' content='text/html; charset=utf-8' />\n";
$mail_head .= "<head><meta http-equiv='content-type' content='text/html; charset=".CHARSET."' />\n";
if (isset($_POST['use_theme']))
if (varsettrue($email_info['use_theme']))
{
$theme = $THEMES_DIRECTORY.$pref['sitetheme']."/";
// $mail_head .= "<link rel=\"stylesheet\" href=\"".SITEURL.$theme."style.css\" type=\"text/css\" />\n";
$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";
$message_body .= $tp -> toEmail($_POST['email_body'])."</div></div></body></html>";
$message_body .= $tp -> toEmail($email_info['email_body'])."</div></div></body></html>";
}
else
{
$message_body = $mail_head;
$message_body .= "</head>\n<body>\n";
$message_body .= $tp -> toEmail($_POST['email_body'])."</body></html>";
$message_body .= $tp -> toEmail($email_info['email_body'])."</body></html>";
$message_body = str_replace("&quot;", '"', $message_body);
$message_body = str_replace('src="', 'src="'.SITEURL, $message_body);
}
@@ -177,13 +235,16 @@ if($_POST['cancel_emails']){
// ---------------- Display Progress and Send Emails. ----------------------->
echo "<div class='fcaption'>&nbsp;".MAILAN_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'])."' ";
echo "<div class='fcaption'>&nbsp;".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."' ";
$count = $sql -> db_Select_gen($qry);
// 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 />";
if(!$count)
{
echo "<div style='text-align:center;width:200px'><br />".MAILAN_61."</div>";
echo "<div style='text-align:center;width:200px'><br />".LAN_MAILOUT_61."</div>";
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()\" />
@@ -193,67 +254,132 @@ if($_POST['cancel_emails']){
$c = 0; $d=0;
$cur = 0;
$send_ok = 0; $send_fail = 0;
$pause_count = 1;
$pause_amount = ($pref['mail_pause']) ? $pref['mail_pause'] : 10;
$pause_time = ($pref['mail_pausetime']) ? $pref['mail_pausetime'] : 1;
$sent = array();
$failed = array();
$unit = (1/$count)* 100;
echo "<div class='blocks' style='text-align:left;width:199px'><div id='bar' class='bar' style='border:0px;;width:".$cur."%' >&nbsp;</div></div>";
$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."%' >&nbsp;</div></div>";
echo "<div class='percents'><span id='numbers'>".($c+1)." / ".$count." (" . $cur . "</span>%) &nbsp;".LAN_MAILOUT_117."</div>";
stopwatch();
// 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");
}
}
while($row = $sql-> db_Fetch())
{
//-------------------------------
// Send one email
//-------------------------------
$mail_info = unserialize($row['gen_chardata']); // Has most of the info needed
$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>" : "";
// Allow username in subject
$mail_subject = str_replace(array('|USERNAME|','{USERNAME}'),$mail_info['user_name'],$message_subject);
$mail->Subject = $mail_subject;
// ---------------------- Mailing Part. -------------------------------------->
// 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);
$activator = (substr(SITEURL, -1) == "/" ? SITEURL."signup.php?activate.".$row['user_id'].".".$row['user_sess'] : SITEURL."/signup.php?activate.".$row['user_id'].".".$row['user_sess']);
$signup_link = ($row['user_sess']) ? "<a href='$activator'>$activator</a>" : "";
$mes_body = str_replace($search,$replace,$message_body);
$alt_body = str_replace($search,$replace,stripslashes($tp->toText($email_info['email_body'])));
$search = array("|USERNAME|","|USERID|","|SIGNUP_LINK|");
$replace = array($row['user_name'],$row['user_id'],$signup_link);
$mail->Body = $mes_body;
$mail->AltBody = $alt_body;
$mes_body = str_replace($search,$replace,$message_body);
$alt_body = str_replace($search,$replace,stripslashes($tp->toText($_POST['email_body'])));
$mail->Body = $mes_body;
$mail->AltBody = $alt_body;
$mail->AddAddress($row['user_email'], $row['user_name']);
$mail->AddCustomHeader("X-e107-id: ".$row['user_id']);
$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);
if ($mail->Send()) {
$sent[] = $row['user_id'];
} else {
$failed[] = $row['user_id'];
$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");
}
$mail->ClearAddresses();
$mail->ClearCustomHeaders();
// --------- End of the mailing. --------------------------------------------->
// --------- One email sent
$cur = round((($c / $count) * 100) + $unit);
echo str_pad(' ',4096)."<br />\n";
$d = ($c==0) ? 10 : round($width + $d);
// Do we need next line?
// echo str_pad(' ',4096)."<br />\n"; // Put out lots of spaces and a newline - works wonders for XHTML compliance!
echo "<div class='percents'>".($c+1)." / ".$count." (" . $cur . "%) &nbsp;complete</div>";
// $d = ($c==0) ? 10 : round($width + $d); // Line doesn't do anything
if($cur != $prev){
echo "<script type='text/javascript'>inc('".$cur."%');</script>\n";
// echo "<div class='percents'>".($c+1)." / ".$count." (" . $cur . "%) &nbsp;".LAN_MAILOUT_117."</div>";
echo "<script type='text/javascript'>setnum('".($c+1)."','{$count}','{$cur}');</script>\n";
/* if($cur != $prev)
{ // Update 'completed' segment of progress bar
echo "<script type='text/javascript'>inc('".$cur."%');</script>\n";
}
$prev = $cur;
ob_flush();
*/ ob_flush();
flush();
if($pause_count > $pause_amount){
sleep($pause_time);
$pause_count = 1;
if($pause_count > $pause_amount)
{
sleep($pause_time);
$pause_count = 1;
}
// Default sleep to reduce server-load: 1 second.
@@ -265,16 +391,22 @@ if($_POST['cancel_emails']){
ob_end_flush();
echo "<div style='position:absolute;left:10px;top:50px'><br />";
echo MAILAN_62." ".count($sent)."<br />";
echo MAILAN_63." ".count($failed)."<br />";
echo MAILAN_64." ".stopwatch()." ".MAILAN_65."<br />";
echo LAN_MAILOUT_62." ".$send_ok."<br />";
echo LAN_MAILOUT_63." ".$send_fail."<br />";
echo LAN_MAILOUT_64." ".stopwatch()." ".LAN_MAILOUT_65."<br />";
echo "</div>";
$message = $sql -> db_Delete("generic", "gen_datestamp='".intval($_POST['mail_id'])."' ") ? "deleted" : "deleted_failed";
// 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."' ");
$mail->ClearAttachments();
if ($pref['mailer']== 'smtp') {
$mail->SmtpClose();
if ($pref['mailer']== 'smtp')
{
$mail->SmtpClose();
}
echo "<div style='text-align:center;margin-left:auto;margin-right:auto;position:absolute;left:10px;top:110px'>
@@ -282,6 +414,11 @@ echo "<div style='text-align:center;margin-left:auto;margin-right:auto;position:
</div>";
echo "</body></html>";
if ($logenable)
{
fwrite($loghandle,$log_string."\r\n");
fclose($loghandle);
}
@@ -342,8 +479,16 @@ function headerjs(){
$text .= "
<script type='text/javascript'>
function inc(amount){
document.getElementById('bar').style.width= amount;
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+'%';
}
</script>";