Mailing List in PHP
Now for the fruits of your labour , create a newsletter
and send it to all your subscribers. First of all we
create a form with some necessary fields in it .
<form action = "sendnewsletter.php"
method = "post">
<table>
<tr>
<td>Subject : </td>
<td>
<input type = "text" name = "subject"
size="70">
</td>
</tr><tr>
<td>Body : </td>
<td><textarea name = "body" cols
= "50" rows = "20" wrap = virtual></textarea></td>
</tr><tr><td>
<input type = "submit" value = "create
newsletter">
</td><td>
<input type = "reset">
</td></tr>
</table>
</form>
Pretty straightforward this now for the script to send
the newsletters
<?php
//sendnewsletter.php
//creates your newsletter an sends it.
if (($subject == "") || ($body == ""))
{
header("Location : sendaletter.php");
exit;
}
else
{
//make a connection
$connection = mysql_connect("localhost" ,
"username" , "password")
or die("cannot make connection");
//select the database
$db = mysql_select_db("email" , $connection)
or die("cannot find database");
$sql_query = "SELECT email FROM email";
$result = mysql_query($sql_query) or die("cannot
execute query");
$header = "From : Myscripting.Com";
//loop through rows
while ($row = mysql_fetch_array($result))
{
$address = $row[0];
//mail function requires email address($address)
//The subject($subject) , the message ($body) and
//the $header info
mail($address ,$subject , $body , $header);
//display a message confirming email has been sent
echo ("newsletter sent to : $address<br>");
}
echo ("Completed sending emails");
}
?>
Remember and add your user info at the start , the
hostname , username and password.
This script simply sends an email to an address forom
the database , it loops through all stored addresses
dispalying a confirmation message and when this is finished
you get a message .
Note the if else at the start is our first validation
this basicly checks whether you have entered and data
for the subject and the body , if you have not you are
redirected back to the form.
<Top
| Back
|