PHP Code To Send Email Using MySQL Database

There are multiple ways you can send an email to customers retrieved from database. In this article I’ll explain the code that I wrote to bulk send an email to the list of customers retrieved from MySQL database.

PHP script to send an Email

Smallest code you can write to send an email using PHP is below

<?php
   $to = "recipient email";
   $subject = "Subject for the email";
   $body = "Hi test person, This is test email.";
   $header = "From: from@email";
 
   if ( mail($to, $subject, $body, $header)) {
      echo("Success");
   } else {
      echo("Failed");
   }
?>

Now lets extend this code to send the email to list of customers. We will retrieve the list of customers from MySQL databse.

<?php 
$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = 'password'; 
$dbname = 'dbName'; 

$today = date("m\/d\/Y");  // todays system date
$date_ = mktime(0, 0, 0, date("m"), date("d")+1, date("Y")); // adding specific number of days to date to get reminder of subscription expiration.
$tomorrow = date("m/d/Y", $date_);  //formatting the date time according to your database format
$Renewal = (string)$tomorrow;

$ordernumber="";
$conn = mysql_connect($dbhost, $dbuser, $dbpass); 
if(!$conn) { 
die('Failed to connect to server: ' . mysql_error()); 
} 
mysql_select_db($dbname); 
$sql="SELECT * FROM tableName WHERE Renewal = '".$Renewal."'"; 
$result = mysql_query($sql); 

$email = "[email protected]"; 
$emailto = "[email protected]"; 
$emailcc = "[email protected]"; 
$emailcc = "";
$subject = "Customer Subscription information"; 
$headers = "From: $email" . "\r\n" . "CC: $emailcc"; 

while($row=mysql_fetch_array($result)) { 
$body .= " \n "
."Following are the people whoes subscription will expire tomorrow"." \n "." \n "
."First Name : ".$row['FirstName']." \n "
."Last Name : ".$row['LastName'] ." \n "
."Email : ".$row['Email'] ." \n "
."Address : ".$row['Address'] ." \n "
."Country : ".$row['Country'] ." \n "
."Phone No. : ".$row['PhoneNo'] ." \n "
."Product : ".$row['Product'] ." \n "
."Version : ".$row['Version'] ." \n "
."Renwal Date : ".$row['Renewal']. "\n"
."\n". "\n". "==============================". "\n"; 
} 

// send email 
if(mysql_num_rows($result) > 0)
{
$send = mail($emailto, $subject . ' ' . $ordernumber, $body, $headers); 
}
mysql_close($conn); 
?>

Automating the PHP script to send an email on a specific schedule

  • SSH or telnet into your account.
  • At the prompt, type in ‘crontab -e’. This will open up your crontab file, or create a new one if it doesn’t exist.
  • You will see other cron jobs listed in the file, or if you haven’t any – you’ll see a bunch of lines with ‘~’.
  • Go to the bottom of the file. This is where you start your new line.
  • Press ‘o’ to insert a new line. If you want to edit a line, press ‘i’.
  • Copy the code you just wrote, then right-click into your SSH or telnet to paste the code in. This should all go in as a new line.
  • save the file.

Understanding cron job

# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0 2 12 * 0,6 /usr/bin/php /vr/www/html/vpe/mail.php
30 23 * * * /usr/bin/php /var/www/html/vpe/mail.phpuse PHP and MySQL to send an email

that means above cron will run daily at 23:30 i.e., 11:30 PM

Written by

I am a software engineer with over 10 years of experience in blogging and web development. I have expertise in both front-end and back-end development, as well as database design, web security, and SEO.

Leave a Reply

Your email address will not be published. Required fields are marked *