|
|
|
|
|
|
| |
Managing users in a tell-a-friend type site can be challenging. You may not want to create a separate user for each 'friend' being told about the site. In this case a small simple table that holds the user_id of the user doing the 'telling' and the recipients (friend's) email is all we need. This type of structure makes it very easy to determine a distinct list of user and friends to whom emails should be sent.
So assume two tables
users:
users_id
users_first_name
users_last_name
users_email
[whatever else you want here]
tell_friends
record_id
users_id
friend_email
[whatever else you want here]
The benefit of this design is that you can easily have more than
four emails for the tell a friend part and the data is correctly stored as (where the first one is that student joining your site)
1 | studenta@school.com | studenta@school.com
2 | studenta@school.com | studentb@school.com
3 | studenta@school.com | studentc@school.com
4 | studenta@school.com | studentd@school.com
5 | studenta@school.com | studente@school.com
The trick here is that when a new student signs up with you, you automatically add him/her to the tell-friends table. Then you have one table as your mailing list and its very easy to create a distinct list of emails to send your mail to.
Db purists will argue that its not a normalized design for the two tables since we are repeating the student_email, and they would be correct. Its not. It is, however, speed and performance friendly since there are no joins in the table to adversely affect script/server
performance and there is no need for additional processing to determine a distinct list of emails to be mailed. In pure web environments, there are many cases where dnormalizing the table can bring about great increases in performance, so its an acceptable sin.
Now the below script will send 100 emails out at a run, one at a time in a loop. The page reloads itself every 30 seconds after completion to send out the next batch. You may want to monitor the server load to see if you need to increase this timeout to limit the load on the mailserver.
| <?
$limit = 100; //number of records per go
$offset = 0; //starting point in the database to avoid rerunning records
if (!empty($_COOKIE['offset'])) {
$offset = $_GET['offset'];
setcookie("offset",$offset+100); //we increase the count by 100 so we know where to start getting the records from
}
$headers = 'From: email@example.com' . "\r\n" ;
$headers .= 'Reply-To: email@example.com' . "\r\n" ;
$headers .= 'X-Mailer: php/' . phpversion();
$headers .= "MIME-Version: 1.0\n"; //send html email (req'd for image)
$headers .= "Content-type: text/html; charset=iso-8859-1\n"; //send html email (req'd for image)
$headers .= "X-Priority: 1\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "From: \"".$your_name."\" <".$your_email.">\n";
mysql_select_db($database_authenticate, $authenticate);
$result = mysql_query("SELECT distinct(friend_email) FROM tell_friends LIMIT $offset, $limit");
if(mysql_num_rows($result) > 0) {
$count = 0;
while ($row = mysql_fetch_array ($result, mysql_ASSOC)) {
$to = $row['friend_email1'];
$subject = "Email"; //enter your subject here
//enter your message here (use as many lines as required)
$message = "<body><table><tr><td><img src='http://www.path/to/image.gif'></td></tr>
<tr><td>message body goes here</td></tr>
<tr><td><img src='http://www.path/to/image1.gif'>
<img src='http://www.path/to/image2.gif'>
<img src='http://www.path/to/image3.gif'></td></tr></table></body";
//$headers .= "Bcc: $to \n" //use this to send Blind Carbon Copies (ie each user only sees his address in when sending to multiple emails)
mail($to, $subject, $message, $headers);
$count++;
}
echo "Preparing to reload the page to continue emailing the records.";
echo "<head>
<script language=javascript>
var x = setTimeout(\"document.location.reload();\", 30000); //30 second timeout to let the server settle before it reloads the page
</script>
</head>
<body><br /><br /><a href='#' onclick='clearTimeout(x);'>Kill the program</a></body>"
}else{
echo "myResult=Email Submissions Failed.";
}
?> | | |
|
| email new items in db Categories : PHP, Email, Databases, MySQL, Beginner Guides | | | This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases, MySQL, Complete Programs, PHP, Databases | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | simple script to send emails via a html-form to different users Categories : Email, MySQL, PHP, Databases | | | Email a user with out exposing email address Categories : PHP, Databases, MySQL, Email | | | Broadcast HTML Email Categories : PHP, Email, MySQL, Databases | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | | | Point and Click Interface ala MS Access for creating SQL statements. Categories : MySQL, Complete Programs, General SQL, PHP, Databases | | | Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | | | Alternating background color for HTML table rows Categories : PHP, Databases, MySQL, HTML and PHP | | | color codes for positive and negative numbers Categories : PHP, MySQL, Databases, HTML | | | A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL | | | Tropicalm Genetree Family (MySQL based family tree) Categories : PHP, Interfaces, Databases, MySQL, Complete Programs | | | mysql_escape_string Categories : PHP, MySQL, Databases, Strings | |
|
|
|