Email Subscription List (2 - Sending Emails) in PHP/HTML

Introduction: This is a second part to my previous tutorial - 'Creating an Email Subscription List in PHP/MySQLi', found here; http://www.sourcecodester.com/tutorials/php/7813/email-subscription-list-phphtml.html Mail Function: Before we can begin with our actual email subscription email sending script, we need to know exactly how we are going to be sending our emails. Luckily enough, there is a built in function in to PHP called 'mail', guess what it does... it sends an email! The documentation of the mail function can be found here; http://php.net/manual/en/function.mail.php The function takes a minimum of three parameters with an optional fourth and even fifth - I will not be covering those additional, optional parameters in this tutorial. So the three parameters required for this function are; The 'to' email address to send the emali to. The subject of the email. The main body/message of the email. Of course, all three parameters are going to be strings. Subscription Methods: There are a few different methods we could logically create our email subscription sending script. First of all, we could send an email to someone each time they perform an action (register, for example). Or, we could check to see if an email is pending to be sent to them, each time they perform an action, and if it is, we send the email. Another method we could use is using a Cron job. Cron jobs are PHP scripts which are ran with a set interval between runs (maybe once an hour, twice a day, four times a year, etc). Our cron job would be to loop through each of our email subcription emails located within our database table(s), and send an email to each of them. I will be using this method, except I am unable to perform the script as a crob job because I can not create cron jobs on my localhost - if you're using a web server, you can create them. The Script: So, on to the actual script. First we create the connection object to our database, if it doesn't already exist...
  1. $con = mysqli_connect('localhost', 'root', '', 'flnEmails');
Then we create a query to return all of the emails held within our 'emails' table...
  1. $q = mysqli_query($con, "SELECT * FROM `emails`");
Next we check the query performed ok, and if so, that there are more than zero (valid) rows returned. If not, we output an error (this is both for security, and user debugging)...
  1. if ($q && mysqli_num_rows($q) > 0) {
  2.  
  3. }else
  4. echo 'No rows of emails returned from the table \'emails\'';
Now that we have the emails, we want to iterate (loop) through them one by one...
  1. while ($row = mysqli_fetch_array($q)) {
  2.  
  3. }
And then, finally, we want to use the 'mail' function to send an email to the currently iterating email address/user...
  1. mail($row['email'], 'Subject', 'Message');
Finished! Here is the full source code...
  1. <?php
  2. $con = mysqli_connect('localhost', 'root', '', 'flnEmails');
  3. $q = mysqli_query($con, "SELECT * FROM `emails`");
  4. if ($q && mysqli_num_rows($q) > 0) {
  5. while ($row = mysqli_fetch_array($q)) {
  6. mail($row['email'], 'Subject', 'Message');
  7. }
  8. }else
  9. echo 'No rows of emails returned from the table \'emails\'';
  10. ?>

Add new comment