WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Solution to those 'tell-a-friend' type email issues
Categories : PHP, Email, Databases, MySQL Click here to Update Your Picture
bastien koert
Date : Jul 15th 2005
Grade : 2 of 5 (graded 6 times)
Viewed : 5501
File : No file for this code example.
Images : No Images for this code example.
Search : More code by bastien koert
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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
Email a user with out exposing email address
Categories : PHP, Databases, MySQL, Email
bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
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
Broadcast HTML Email
Categories : PHP, Email, MySQL, Databases
simple script to send emails via a html-form to different users
Categories : Email, MySQL, PHP, Databases
Message of the Day - Random Message (Needs MySQL!)
Categories : Databases, HTML and PHP, PHP, MySQL
A Complete table(ADD,EDIT,VIEW,DELETE) management System PHP,MYSQL, JAVASCRIPT
Categories : PHP, MySQL, Java Script, 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
MySQL database class
Categories : PHP, MySQL, Databases, PHP Classes
List people whose birthdays fall on the current Day and Month
Categories : Databases, Date Time, MySQL, PHP
phpAds, a complete banner and ad management system with detailled tracking and stats.
Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases
A Simple sign up script with PHP and JavaScript validations.
Categories : PHP, Java Script, MySQL, Databases
Point and Click Interface ala MS Access for creating SQL statements.
Categories : MySQL, Complete Programs, General SQL, PHP, Databases
phpEasySQL - Easily connect to your MySQL database with just 1 php file and 3 easy steps!
Categories : Databases, PHP, MySQL, General SQL