| Publishing Newsletter Using PHP & MySQL - 4 |
For a quick wrap of the previous articles in the current series, go to:
http://www.bytesworth.com/learn/php00001.asp
http://www.bytesworth.com/learn/php00002.asp
http://www.bytesworth.com/learn/php00003.asp
The time has arrived to launch your publishing career at last. The subscribers
have subscribed and your emails have been validated. Now all you have to do
is, write the newsletter, copy/pase it in the box and click the submit button
We can publish text newsletter as well as HTML newsletter. We'll see how.
First the form:
<form method="post" action="send_newsletter.php">
Subject:<br>
<input type="text" name="sub" size="40"><br>
Body<br>
<textarea cols="80" rows="20" name="bdy" wrap="hard"></textarea><br>
HTML: <input type="checkbox" name="html" value="0"><br>
<input type="submit" name="s1" value="Submit">
</form>
The form has three fields, namely, Subject, Body and a checkbox to decide
whether you want to send a plain newsletter or an HTML newsletter. This form
uses the file "send_newsletter.php" to send the newsletter. Here's
send_newsletter.php:
<?php
$html=$_POST[html];
$db=mysql_connect("localhost", "wwwdate_betty", "soandso") or die("I cannot
connect to the database because " . mysql_error());
mysql_select_db("wwwdate_others", $db);
$query="select name, email from subscribers where active=1";
$result=mysql_query($query);
$subject=$_POST[sub];
$tb=stripslashes($_POST[bdy]);
if($html==NULL)
{
$headers="From: \"DATEONLINE-U.COM\" <dc@dateonline-u.com>";
}
else
{
$headers="From: \"DATEONLINE-U.COM\" <dc@dateonline-u.com>";
$headers.="MIME-Version: 1.0";
$headers.="Content-type: text/html; charset=iso-8859-1";
}
?>
Most of the initial code must be clear by now. If not, please refer to the
links mentioned at the beginning of the article. We use the function
stripslashes() to take care of all the characters that PHP finds unpalatable
(such as double-quotes and single-quotes). The if condition checks whether you
have selected the HTML checkbox. If yes, then the necessary encoding is added
to the variable $headers.
<?php
while($row=mysql_fetch_row($result))
{
$tbody="Dear " . $row[0] . ".\r\r";
$tbody.="Here's the current issue of DATE IS THE CASE dated " . date
("m-d-Y", time()) . "\r\r";
$tbody.=$tb;
mail($row[1], $subject, $tbody, $headers);
}
echo "<h1>Done!</h1>";
?>
The while loop uses mysql_fetch_row() function to extract data from the array
$result. Then for each record, an email is generated and sent to individual
recipients.
This finishes the "Publishing Newsletter Using PhP & MySQL" series. I hope I
was lucid enough to enable you to maintain your own newsletter. This was just
a framework. I suggest you keep tweaking the code according to your
requirement and taste. You are always welcome to write to me using the form at
http://www.bytesworth.com.
Stay tuned for more articles.
== Article ends ==
About the Author
==========================================================
Amrit Hallan is a freelance web developer. You can
checkout
his website at http://www.bytesworth.com.
For more such articles join BYTESWORTH REACHOUT at
http://www.bytesworth.com/br/default.asp or if you
have
a web dev related question then post it at
http://www.business180.com/index.php
==========================================================
|
Back to Articles Index
|
|
 |
|