Location: PHPKode > projects > MailBlaster > mailblaster/preview.php
<?php

require("phpmailer/class.phpmailer.php");
require("config.php");

$auth = 0;
if ( ($_SERVER['PHP_AUTH_USER'] == $username) && ($_SERVER['PHP_AUTH_PW'] == $password) ) {!
  $auth = 1;
}
if ($auth != 1) {
  header('WWW-Authenticate: Basic realm="Mail Blaster"');
  header('HTTP/1.0 401 Unauthorized');
  echo 'You will need to login to blast mail';
  exit;
}

// Make sure we have a name to save the file under!!
if (empty($saveasname)) {
  print "<br><font face=verdana color=red>Error - File name at bottom must be filled out.  Please make sure you either type a new name in or load a file from the \"Saved Message\" menu.<br><a href=\"javascript:history.back();\">Click here to go back</a><br>\n";
  exit();
}

// Save the newly edited message ($newmessage) we are previewing as $ssveasname
$fp = fopen($messagedir.$saveasname, 'w');
if (fwrite($fp, stripslashes($newmessage))) {
  $savedmessage = $saveasname;
}
else {
  print "<br><font color=red>Error saving new file</font><br>\n";
}
fclose($fp);

// Check for missing information
if (empty($from)) {
  $fromblank = true;
}
if (empty($subject)) {
  $subjectblank = true;
}

// Load the mailing list and make sure it's clean
$maillistdirty = false;
if ($maillist != "") {
  $listfile = $listsdir . $maillist;
}
elseif (isset($maillist_new)) {
  $listfile = $listsdir . $_FILES['maillist_new']['name'];
  if (!move_uploaded_file($_FILES['maillist_new']['tmp_name'], $listfile)) {
    print "Error uploading file: ";
    print_r($_FILES);
    $maillistdirty = true;
  }
}
else {
  $maillistdirty = true;
}
$addresses = @file($listfile);

if (!empty($_FILES['uploadmessage']['name'])) {
  $messagefile = $messagedir . $_FILES['uploadmessage']['name'];

  if (move_uploaded_file($_FILES['uploadmessage']['tmp_name'], $messagefile)) {
    $fp = fopen($messagefile, "r");
    $message_contents = fread($fp, filesize($messagefile));
    fclose($fp);
  }
  else {
    print_r($_FILES);
  }
}
if (!empty($savedmessage)) {
  $messagefile = $messagedir . $savedmessage;
  $fp = fopen($messagefile, "r");
  $message_contents = fread($fp, filesize($messagefile));
  fclose($fp);
}
if (!empty($newmessage)) {
  $message_contents = $newmessage;
}

// all logic for displaying of the form elements should be done 
//  above this point.  This includes buttons AND hidden elements

?>

<form action="index.php" name="theform" method="post">

<input type="hidden" name="from" value="<?=$from?>">
<input type="hidden" name="maillist" value="<?=$maillist?>">
<input type="hidden" name="subject" value="<?=$subject?>">
<input type="hidden" name="type" value="<?=$type?>">
<input type="hidden" name="savedmessage" value="<?=$savedmessage?>">
<input type="hidden" name="uploadmessage" value="<?=$_FILES['uploadmessage']['name']?>">
<input type="hidden" name="saveasname" value="<?=$saveasname?>">

<input type="submit" value="EDIT" onClick="document.theform.action='index.php';"> <!-- send to index.php -->
<?php if (!$maillistdirty && !$fromblank && !$subjectblank) { ?>
<input type="submit" value="BLAST" onClick="document.theform.action='mailblaster.php';"> <!-- send to mailblaster.php -->
<?php } ?>

</form>


<?php

// Prepare to display the headers
$firstone = true;
$mail = new PHPMailer();
for ($i=0; $i<sizeOf($addresses); $i++) {
  $addresses[$i] = rtrim($addresses[$i]);
  $addresses[$i] = ltrim($addresses[$i]);

  // TODO - address validation - color code bad addresses
  //if (eregi("(\w+[\w|\.]*\w+)(@\w+[\w|\.]*\w+\.\w{2,3})", $addresses[$i])) {
  //if (ereg("^([A-Za-z_\.]*)@([A-Za-z_]*)\.([A-Za-z_\.]*)$", $string)) {
  if (true) {
    $mail->From = $from;
    $mail->FromName = $from;
    $mail->Host = "localhost";
    $mail->Mailer = "smtp";
    $mail->AddAddress($addresses[$i]);
    $mail->Subject = $subject;
    $mail->isHTML(true);
    $mail->Body = $message_contents;
    //$mail->AltBody = "This would be the text version of the HTML mail";
  }
  if ($firstone) {
    $to .= $addresses[$i];
    $firstone = false;
  }
  else {
    $to .= ", " . $addresses[$i];
  }
}

// Display the message headers
print "<table cellpadding=2 border=1>\n";
print "<tr><td><font face=Verdana>From: <td><font face=Verdana>\n";
print ($fromblank) ? "<font color=red>ERROR - no from address specified! Click the Edit button to fix this.</font>\n" : $from . "\n";
print "<tr><td><font face=Verdana>To: <td><font face=Verdana>\n";
print ($maillistdirty) ? "<font color=red>ERROR - no mail list was chosen! Click the Edit button to fix this.</font>\n" : $to . "\n";
print "<tr><td><font face=Verdana>Subject: <td><font face=Verdana>\n";
print ($subjectblank) ? "<font color=red>ERROR - no subject specified! Click the Edit button to fix this.</font>\n" : $subject . "\n";
print "</table>\n\n";
print "<br>\n";
print "<hr>\n";

// Display the message body
if ($type == "plaintext") {
  if ($include_removal) $removal_text = removal_text("address would go here", $maillist, "plaintext");
  print ereg_replace("\n", "<BR>\n", $message_contents . $removal_text);
}
elseif ($type == "html") {
  if ($include_removal) $removal_text = removal_text("address would go here", $maillist, "html");
  print stripslashes($message_contents . $removal_text);
}


?>

<hr>

<form action="index.php" name="theform2" method="post">

<input type="hidden" name="from" value="<?=$from?>">
<input type="hidden" name="maillist" value="<?=$maillist?>">
<input type="hidden" name="subject" value="<?=$subject?>">
<input type="hidden" name="type" value="<?=$type?>">
<input type="hidden" name="savedmessage" value="<?=$savedmessage?>">
<input type="hidden" name="uploadmessage" value="<?=$_FILES['uploadmessage']['name']?>">
<input type="hidden" name="saveasname" value="<?=$saveasname?>">

<input type="submit" value="EDIT" onClick="document.theform2.action='index.php';"> <!-- send to index.php -->
<?php if (!$maillistdirty && !$fromblank && !$subjectblank) { ?>
<input type="submit" value="BLAST" onClick="document.theform2.action='mailblaster.php';"> <!-- send to mailblaster.php -->
<?php } ?>

</form>



Return current item: MailBlaster