Jump to content

Placing Users First name in the e-mail message!


demeritrious

Recommended Posts

I believe the solution the my problem should be simple I feel that it's staring me right in the face. I have a Cron Job that sends an email message to users who's bill "due date" falls on the current date. I want to make the email more personalized and say:

 

Dear John Doe:

 

You have the following bills due today:

 

Rent

Cable

Internet

 

Please login to pay your bills

 

Thanks,.

 

Here's my following PHP code

<?php
header("Content-type: text/plain");
 
// OPEN DATA BASE
define("HOSTNAME","localhost");
define("USERNAME","");
define("PASSWORD","");
define("DATABASE","");
 
mysql_connect(HOSTNAME, USERNAME, PASSWORD) or die("Connetion to database failed!");
mysql_select_db(DATABASE); 
 
joinDateFilter();
 
// BILL QUERY
function joinDateFilter(){
     $query = mysql_query("SELECT bills.billname, bills.duedate, bills.firstname, bills.email, bills.paid FROM bills JOIN users ON bills.userid=users.userid WHERE DATE(bills.duedate) = CURDATE() AND bills.PAID != 'YES'");
     $mail_to = "";
     while ($row = mysql_fetch_array($query)){
      echo $row['firstname']." - ".$row['email']."\n";
  $mail_to = $row['email'].", ";
     }
     if (!empty($mail_to)){
      sendEmail($mail_to);
     }
}
 
// SEND EMAIL
function sendEmail($mail_to) {
$from    = "[email protected]";
$message = "Dear " . $row['firstname'].", <br><br>"
 
."You have the following bills due today.<br><br>"
.$row['billname']. "<br><br>"
."Please login to pay your bills";
 
$headers = 'From: '.$from."\r\n" .
'Reply-To:'.$_POST['email']."\r\n" .
"Content-Type: text/html; charset=iso-8859-1\n".
'X-Mailer: PHP/' . phpversion();
mail($mail_to, "Today is your due date", $message, $headers);
}
?>

$row isn't automatically available inside sendEmail. You have to pass it as an argument, just like you did with $mail_to. Even better would be passing the individual pieces of data: the firstname and "billname" separately.

I'm not sure what you're describing but your code should look something like

sendEmail($mail_to, $row['firstname'], $row['billname']);
function sendEmail($mail_to, $firstname, $billname) {
$message = "Dear " . $firstname.", <br><br>"
 
."You have the following bills due today.<br><br>"
.$billname. "<br><br>"
."Please login to pay your bills";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.