MAILING TO MULTIPLE USERS

This is not a script to create a mailing list, it's just a simple script that will allow you to send email to multiple users already stored in your database. I created a date service site in which users need to sign up to become members, since members give their email when they sign up, there was no need to put a "Join my mailing list" on my site. So basically every email is stored on the database when a member signs up. Also since members have to select the country they live in, not only i needed a function to send emails to the entire list but also send emails to members belonging only to a single country.

Please take into consideration that i didn't write this script for beginners, so in other words, you need to have at least a basic knowdlege of PHP and MYSQL. I was actually looking for a script like this and i found some, but they were a bit too complicated for a simple task i wanted to do, i didn't have time to write my own but at the end i had no choice and guess what?? it only took me 10 minutes!


Intro

This script is quite simple and i used "PHP Multi-Purpose Pages" which means that it only takes one page to do everything. If you do not know what are "PHP Multi-Purpose Pages", i suggest you find a tutorial about it, but to give you a brief definition, "Multi-Purpose Pages" allows you to have two HTML pages in one and each page is shown depending on the condition executed.

The HTML

Here's a look at the HTML page that you will use to send emails to your list. Of course it doesn't looks quite pretty but this is just an example and besides you can modify it the way you like it.

MAILING LIST ADMIN



Send message to:

Country:


Title or Subject:

Message:




Ok... so as you may see, it's quite simple. The fist option "Send message to"gives you two choices, send message to the "Entire list" and send message By country. If you choose to send message to the "Entire list", then you wouldn't have to choose a country obviously... but even if you do, it wouldn't matter, you will see why. And of course if you choose send message By country, you need to select the country you want to send the email to. Then comes the subject of the email and the message you want to send to your list.


The Script

Now lets move to the script. Since the script is very simple, so i just wrote the finished script for those of you who just want to grabb it and change what needs to be changed. I will try to explain the script step by step very soon, i just don't have the time right now. Still if you have any doubts about something just send me an email to toly_sivik@hotmail.com.

Finished Script

The spaces in red are the ones you can modify to your own. Note that you don't have to include the country section or you can modify it to any other categories you have in mind.

<?php
include("dbconnection.php");
   if (!isset($_POST['submit'])):
?>

<html>
<head>
<title>Mailing List</title>
</head>

<body>

<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<table align="center" bgcolor="#D6DEE7" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
<b><h2>MAILING LIST ADMIN</h2></b>


Send message to:
<select name="to" size="1" style="background-color: #F7F7F7">
<option selected value="all">Entire list
<option value="notall">By country
</select>

Country:
<select name="country" size="5">
<OPTION value="Argentina">Argentina
<OPTION value="Bolivia">Bolivia
<OPTION value="Brazil">Brazil
<OPTION value="Chile">Chile
<OPTION value="Colombia">Colombia
<OPTION value="Costa Rica">Costa Rica
<OPTION value="Ecuador">Ecuador
<OPTION value="Guatemala">Guatemala
<OPTION value="Honduras">Honduras
<OPTION value="Mexico">Mexico
<OPTION value="Nicaragua">Nicaragua
<OPTION value="Panama">Panama
<OPTION value="Paraguay">Paraguay
<OPTION value="Peru">Peru
<OPTION value="Puerto Rico">Puerto Rico
<OPTION value="Rep. Dominicana">Rep. Dominicana
<OPTION value="Uruguay">Uruguay
<OPTION value="Venezuela">Venezuela
</select>

Title or Subject: <input name="subject" type=text maxlength=100 size=40>
Message:
<textarea wrap name="message" rows=10 cols=45></textarea>
<input type=submit name="submit" value="SUBMIT">
</td>
</tr>
</table>
</form>
</body>
</html>

<?php else:

  $to = $_POST['to'];
  $subject = $_POST['subject'];
  $message = $_POST['message'];
  $country = $_POST['country'];

  if ("all" == $to) {
   $x = 1;
   $hold = 50; // quantity of emails sent before 3 sec delay
   $emails = mysql_query("SELECT email FROM members");
   while ($sendemail = mysql_fetch_array($emails)) {
   $email = $sendemail["email"];
   mail($email, $subject,
   $message, "From:Your name <you@whatever.com>");

   $x++;
    if($x == $hold) { // When $x is equal to $hold, a 3 sec delay will occur avoiding php to timeout
    sleep(3);
    $x = 0;
    }
   } // end of while loop
  } else {
   $bycountry = mysql_query("SELECT email FROM members WHERE country = '$country'");
   while ($countmail = mysql_fetch_array($bycountry)) {
   $email = $countmail["email"];
   $okemail = mail($email, $subject,
   $message, "From:Your name <you@whatever.com>");

   $x++;
    if($x == $hold) { // When $x is equal to $hold, a 3 sec delay will occur avoiding php to timeout
    sleep(3);
    $x = 0;
    }
   } // end of while loop
  }
?>
<html>
<head>
</head>
<body>
SUCCESS!
</body>
</html>
<?php endif; ?>


Well that's it.... if you want to remove the country section, just erase that part from the HTML and take away the if/else statment from the php script and just leave the first condition. Remember that you can email me at toly_sivik@hotmail.com if you need any help.