//, PHP/Publishing Newsletters Using PHP & MySQL – 4

Publishing Newsletters Using PHP & MySQL – 4

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.

2010-05-26T17:04:47+00:00 March 29th, 2004|MySQL, PHP|0 Comments

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

Leave A Comment