Mailing list

Firstly, we will create the form the will allow the user to add their name and email address to a database so that we can send emails to all users in the mailing list.

<form name="maillist" id="maillist" method="post" action="#">
   Mailing List<br />
   <input name="name" type="text" id="name" value="Name" /><br />
   <input name="email" type="text" id="email" value="Email Address" /><br />
   <input type="submit" name="submit" value="Submit" />
</form>

This simple form has fields for the user name and email address. We will now create a function to check the data before it is put into the database.

function clear($message)
{
   if(!get_magic_quotes_gpc())
      $message = addslashes($message);
   $message = strip_tags($message);
   $message = htmlentities($message);
   return trim($message);
}

Now that we have the function and the data from the user we need to add it to the database.

if($_POST['submit'])
{
   if(empty($_POST['name']))
      die('Name invalid.');
   if(empty($_POST['email']))
      die('Email invalid.');
   mysql_connect('localhost','username','password');
   mysql_select_db('db');
   $name = clear($_POST['name']);
   $address = clear($_POST['email']);
   if(mysql_query("INSERT INTO maillist (id , name , email) VALUES ('', '$name', '$address')"))
      echo 'You have been added to our mailing list.';
   else
      echo 'Database error';
}

This code checks that the data entered is not empty and if it is not then it is entered into the database.

Now that we have a list of email addresses and names we can send a message to each user.

<form name="send" id="send" method="post" action="#">
   Message<br />
   <input name="subject" type="text" id="subject" value="Subject of Email" /><br />
   <textarea name="message" id="message">Message</textarea><br />
   <input type="submit" name="submit" id="submit" value="Submit" />
</form>

This form has two fields, one for the subject of the email and one for the message to send to the users. If the user has submitted the form then we need to check the data.

mysql_connect('localhost','username','password');
mysql_select_db('db');
$sql = mysql_query('SELECT * FROM maillist');
$from = 'youremail@address.com';
$subject = str_replace(array("\n", "\r"), '', $_POST['subject']);
$headers = 'From: '.$from." \n\r";
$headers = 'Reply-To: '.$from."\n\r";
$message = $output['name'].",\n\r";
$message .= $_POST['message'];

A connection to a database has been created and various email headers have been created to add to the email. The subject line must not contain any new line characters so these are removed.

while ($output = mysql_fetch_assoc($sql))
{
   if(mail($output['email'], $subject, wordwrap($message, 70), $headers))
      echo 'Message sent to '.$output['name'];
}

This code sends the email with the subject, headers and message to each user. The email message must not be more than 70 characters in width so the wordwrap function is used. For more information visit the PHP mail function.

To remove a user we need to give them a form to enter their name and email address into and we will then remove this from the database.

<form name="maillist" id="maillist" method="post" action="#">
   Mailing List remove<br />
   <input name="name" type="text" id="name" value="Name" /><br />
   <input name="email" type="text" id="email" value="Email Address" /><br />
   <input type="submit" name="submit" id="submit" value="Submit" />
</form>

This form has the required fields for removing a user. We now need to check the information posted and try to delete it from the database.

if($_POST['submit'])
{
   mysql_connect('localhost','username','password');
   mysql_select_db('db');
   $name = clear($_POST['name']);
   $email = clear($_POST['email']);
   if(mysql_query("DELETE FROM maillist WHERE name = '$name' AND email = '$email'"))
      echo 'You have been removed from our mailing list.';
   else
      echo 'Name / email address incorrect';
}

A simple query is set up to try to delete the name and email address from the database.

Downloads

Categories

Tags

Social