Okay you have a db with a table and a field named 'email' that stores your visitors email address. So in this example your table name is 'contacts' and the field name is 'email'.
First the config file for mysql connection:
config.php
PHP Code:
<?
$dbhost = "localhost";
$dbname = "yourdatabasename";
$dbuser = "yourusername";
$dbpass = "yourpassword";
?>
Now the form you will use to send the email or newsletter:
sendto.php
PHP Code:
<?
if (!$_POST[send]) {
echo "
<html>
<head>
<title>Send a Newsletter</title>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
</head>
<body>
<font face='Verdana, Arial, Helvetica, sans-serif'><strong>Send a Newsletter</strong><br>
</font>
<form name='form1' id='form1' method='post' action='$_SERVER[PHP_SELF]'>
<table width='51%' border='0' cellpadding='5'>
<tr valign='top'>
<td width='47%'><strong><font size='2' face='Verdana, Arial, Helvetica, sans-serif'>Subject:</font></strong></td>
<td width='53%'><input name='subject' type='text' size='25'></td>
</tr>
<tr valign='top'>
<td><strong><font size='2' face='Verdana, Arial, Helvetica, sans-serif'>Message
Body:</font></strong></td>
<td><textarea name='message' cols='50' rows='15'></textarea></td>
</tr>
</table>
<input name='send' type='hidden' id='send' value='ok'>
<br>
<input type='submit' name='Submit' value='Send'>
<input type='reset' name='Submit2' value='Reset'>
</form>
</body>
</html>
";
exit;
} else {
include 'config.php';
// Checking validity
if (!$_POST[subject] || !$_POST[message]) {
header("Location: $_SERVER[PHP_SELF]");
exit;
}
mysql_connect($dbhost,$dbuser,$dbpass) or die(mysql_error());
mysql_select_db($dbname);
$query = mysql_query("SELECT email from contacts");
$headers = "From: Yourname <your@emailaddress.com>\n";
while ($result = mysql_fetch_array($query)) {
mail ($result[email],$_POST[subject],$_POST[message],$headers);
echo "Newsletter has been sent to: $result[email]<br>";
}
}
?>
You only need to edit two lines with your own info in the sendto.php file:
your table
$query = mysql_query("SELECT email from contacts");
your email
($headers = "From: Yourname <your@emailaddress.com>\n";
Hope this helps