.
Developer Spot - Web Development Tutorials
 


Web Hosting Directory
Budget Web Hosting Linux Web Hosting Small Business Hosting
Windows Web Hosting Reseller Web Hosting Web Hosting Articles

Publishing Newsletters Using PHP & MySQL - 4

By Amrit Hallan
2004-03-29
Reader Rating: 3 out of 5
Bookmark Print Version
Publishing Newsletters Using PHP & MySQL - 4

For a quick wrap of the previous articles in the current series, go to:
Part 1
Part 2
Part 3

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>\n";
  $headers.="MIME-Version: 1.0\n";
  $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\n\r\n";
 $tbody.="Here's the current issue of DATE IS THE CASE dated " . date ("m-d-Y", time()) . "\r\n\r\n";
 $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 Pages:
Publishing Newsletters Using PHP & MySQL - 4

 Rate this article:   Poor          Excellent 


If you found this article interesting, you may want to read these as well:

» Protecting your PHP and HTML Source Code

» Publishing Newsletters Using PHP & MySQL - 3

» Publishing Newsletter Using PHP & MySQL - 2

» Publishing Newsletters Using PHP & MySQL

» Unix Webserver Crontab Basics

» Setting Up Apache, PHP & MySQL On Windows



 
Development Tutorials
ASP
CGI & Perl
CSS
HTML
Java
JavaScript
Linux
PHP
XML




More Resources
Web Hosting Articles
Development Tutorials: CGI & Perl - CSS - HTML - Java - JavaScript - Linux - PHP - XML