First off, you need to have a field in your contact/email database that specifies if they are a subscriber - usually a Y/N field (varchar (1)) or 0/1 (tinyint)
Second, you need to create a page (preview.php) that you can use to design your e-mail - whether it be in plain text or more complex htm.
This page will have a form, with two inputs - one for the Subject (text field) and one for the message (text area) and, of course a submit button.
When you click on submit, the php code will post the data back to the same page and parse the contents - to give a good idea of what it will look like. (make sure this page has no css!)
i.e.
PHP:
<?php
echo stripslashes($_POST['subjectfield']);
echo stripslashes($_POST['messagefield']);
?>
Personally, I put the contents of the post variables into session variables for ease of passing between one page and the next.
PHP:
<?php
$_SESSION['sesssubject'] = $_POST['subjectfield']
$_SESSION['sessmessage'] = $_POST['messagefield']
?>
If you put the default value of the form fields as the posted values, you can keep editing your input until you get the desired effect.
Right at the bottom of the body, you then put in a standard link to the clever page.... (you don't need to post or pass the variables into the next page as they are in session memory)
mail.php
The first part of this page is a recordset (I'll call it subscriberlist), based on whether a contact has subscribed or not.
sql.... SELECT ID, EMAIL, SUBSCRIBED FROM CONTACTS WHERE SUBSCRIBED ="Y"
Then, what I do, is to echo the results so you can check your recipients.
So... in the page body, created a repeating table
PHP:
<form>
<table>
<?php do { ?>
<tr>
<td>
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row_subscriberlist['ID']; ?>" />
</td>
<td><?php echo $row_subscriberlist['EMAIL'];?></td>
<td><?php echo $row_subscriberlist['SUBSCRIBED'];?></td>
</tr>
<?php } while ($row_subscriberlist = mysql_fetch_assoc($subscriberlist)); ?>
</table>
<input name="rowstosend" type="hidden" value="<?php echo $totalRows_subscriberlist;?>" />
<input name="send" type="submit" id="send" value="Send E-mails" />
</form>
You'll note that I've wrapped a form tag round the whole table and entered a checkbox in the left table column with a weird name.
You can either then manually input ticks for each subscriber, or get some javascript to fill them all in for you.
I've also put in two field at the bottom - one of which carries a value for the number of rows in the recordset and the other is just a button to do the magic.
Next is the code to send them...
PHP:
<?php
// Check if edit button active, start this
if($send)
{
for($i=0;$i<$_POST['rowstosend'];$i++)
{
$edit_id = $checkbox[$i];
if (isset($edit_id))
{
$query_sendlist = sprintf("SELECT ID, EMAIL FROM CONTACTS WHERE ID LIKE '$edit_id'");
$sendlist = mysql_query($query_sendlist) or die(mysql_error());
$row_sendlist = mysql_fetch_assoc($sendlist);
$toemail = stripslashes($row_sendlist['EMAIL']);
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
$headers .= "Content-Transfer-Encoding: 7bit\n";
$headers .= 'From: yourname <'youremailaddress'>' . "\n";
$headers .= 'Reply-To: 'youremailaddress"\n";
$headers .= 'X-Mailers: PHP /'.phpversion() . "\n";
$subject = $_SESSION['sessionsubject'];
$message = $_SESSION['sessionmessage'];
$message = str_replace("<<<NAME>>>", $row_sendlist['NAME'], $message);
echo "</br>Mailing: ".$row_sendlist['EMAIL']." <".$row_sendlist['EMAIL']."></br>";
if (@mail($toemail,stripslashes($subject),stripslashes($message),stripslashes($headers)))
{
echo "Mail to:".$row_sendlist['EMAIL']." confirmed.</br>";
}
else
{
echo "Mail to".$row_sendlist['EMAIL']." failed.</br>";
}
}
}
}
?>
Phew!
I've also put in the option for merge fields (i.e. parts of the design that will change depending on who it goes to) in the form of <<<NAME>>>. When the php loops through each record, it replaces this set of characters with the value from each recordset...
It even tells you if they've worked!
Happy coding!