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 this script will only work with a small amount of emails. It is basically a quick and small solution to get in touch with your members. However, if you have over 50,000 emails in your database, you should consider using something advanced, like PHP Mailer.
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.
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.
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'])):
?>
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. If you need help of any kind, please visit the PHP section of SitePoint Forums.