Location: PHPKode > projects > Simple PHP Mailing List > simplephpmaillist1.3/sendmessage.php
<?php
////////////////////////////////////////////////////
// maillist - Simple PHP email list
//
// Used to maintain a simple mailing list.
// Utilizes PHPmailer and stores the list in a CSV
// file, so no database is required.  Also provides
// callback capability for smooth integration
//
// Copyright (C) 2005 Jason York
//   http://www.yorkspace.com
//
// License: LGPL, see LICENSE
////////////////////////////////////////////////////

require "maillist-settings.inc.php";

$return_msg = "";

function get_subscribers()
{
	global $sListFile;
	global $return_msg;
	$emaillist = Array();

	if (file_exists($sListFile))
	{
		$handle = fopen($sListFile, "r");
		if ($handle)
		{
			while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
			{
				$emaillist[] = $data[0];
			}
			fclose($handle);
		}
		else
		{
			$return_msg = "Failed to open list file";
		}
	}
	return $emaillist;
}

if ($_POST['preview'])
{
    $email_array = $_POST['emaillist'];

    if (is_array($email_array))
    {
	    //Get the Title and Message
        $subject = htmlspecialchars(stripslashes($_POST['emailtitle']));
        $message = htmlspecialchars(stripslashes($_POST['emailmessage']));

	    $bcc_list = implode(", ", $email_array);

	    //Print the From, To, and Email List with the Subject and Message
        echo "<hr>\r\n";
        echo "<p class=\"MediumText\"><b>" . gettext("From:") . "</b> "  . $sFromEmailAddress . "<br>";
        echo "<b>Bcc:</b> "  . $bcc_list . "<br>";
        echo "<b>Subject:</b> "  . $subject . "<br>";
        echo "</p><hr><textarea cols=\"72\" rows=\"20\" readonly class=\"MediumText\" style=\"border:0px;\">";
        echo $message . "</textarea><br>";
        echo "<hr>";

	    echo "<table><tr><td>";
		    echo "<form action=\"" . $_SERVER['SCRIPT_URL'] . "\" method=\"POST\">";
		    echo "<input type=\"hidden\" name=\"emailtitle\" value=\"" . $subject . "\">";
		    echo "<input type=\"hidden\" name=\"emailmessage\" value=\"" . $message . "\">";
		    echo "<input type=\"submit\" name=\"redo\" value=\"Edit Email\"></form>";
		    echo "</td><td>";

		    echo "<form action=\"" . $_SERVER['SCRIPT_URL'] . "\" method=\"POST\">";
		    foreach ($email_array as $email_address)
		    {
			    echo "<input type=\"hidden\" name=\"emaillist[]\" value=\"" . $email_address . "\">";
		    }
		    echo "<input type=\"hidden\" name=\"emailtitle\" value=\"" . $subject . "\">";
		    echo "<input type=\"hidden\" name=\"emailmessage\" value=\"" . $message . "\">";
		    echo "<input type=\"submit\" name=\"sendemail\" value=\"Send Email\"></form>";

	    echo "</td></tr></table>";
    }
    else
    {
	    echo 'No email addresses specified!';
    }
}
else if ($_POST['sendemail'])
{
	LoadLib_PHPMailer();
	$mail = new MLMail;
	
	$email_array = $_POST['emaillist'];
	
	if ( is_array($email_array) == TRUE )
	{
		$mail->AddAddress($mail->From);
		$mail->Subject = stripslashes($_POST['emailtitle']);
		$mail->Body = stripslashes($_POST['emailmessage']);
								 
		
		foreach ($email_array as $email_address)
		{
			if(strlen($email_address) > 0)
			{
				$mail->AddBcc($email_address);
			}
		}
		if(!$mail->Send())
			echo "There has been an error sending email:<br><br><b>" . $mail->ErrorInfo . "</b>";
		else
			echo "Message has been sent successfully.<br><br><a href=\"" . $_SERVER['SCRIPT_URL'] . "\">Send another message</a>";
		$mail->ClearAddresses();
	}
	else
	{
		echo "No email addresses specified!";
	}
}
else
{
    $email_array = get_subscribers();

    echo "<h2>Compose a message for the " . $sListName . " Mailing List</h2><br>";

    echo "<form action=\"" . $_SERVER['SCRIPT_URL'] . "\" method=\"POST\">";

    foreach ($email_array as $email_address)
    {
	    echo "<input type=\"hidden\" name=\"emaillist[]\" value=\"" . $email_address . "\">";
    }
    echo "<input type=\"hidden\" name=\"emaillist[]\" value=\"" . $sToEmailAddress . "\">";

    // If editing, get Title and Message
    $sEditSubject = $_POST['emailtitle'];
    if (isset($sEditSubject))
	    $subject = $sEditSubject;
    else
	    $subject = "";

    $sEditMessage = $_POST['emailmessage'];
    if (isset($sEditMessage))
	    $message = $sEditMessage;
    else
    {
        $maillistloc = str_replace("sendmessage.php", "maillist.php", $_SERVER['SCRIPT_URL']);
		$message = "\n\n\n-------------------------------------\nTo unsubsubscribe, click here:\n" .
			$sProtocol  . $_SERVER["HTTP_HOST"] . $maillistloc . "?action=unsubscribe";
    }
    echo"Subject:";
    echo "<br><input type=\"text\" name=\"emailtitle\" size=\"80\" value=\"" . htmlspecialchars(stripslashes($subject)) . "\"></input>";
    echo "<br>Message:";
    echo "<br><textarea name=\"emailmessage\" rows=\"20\" cols=\"72\">" . htmlspecialchars(stripslashes($message)) . "</textarea>";
    echo "<br><input type=\"submit\" name=\"preview\" value=\"Preview\"></form>";
}

?>
Return current item: Simple PHP Mailing List