demeritrious Posted December 27, 2014 Share Posted December 27, 2014 (edited) 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 = "MyEmail@myemail.com"; $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); } ?> Edited December 27, 2014 by demeritrious Quote Link to comment https://forums.phpfreaks.com/topic/293374-placing-users-first-name-in-the-e-mail-message/ Share on other sites More sharing options...
requinix Posted December 27, 2014 Share Posted December 27, 2014 $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. Quote Link to comment https://forums.phpfreaks.com/topic/293374-placing-users-first-name-in-the-e-mail-message/#findComment-1500769 Share on other sites More sharing options...
demeritrious Posted December 27, 2014 Author Share Posted December 27, 2014 Forgive me I feel dumb. I tried adding $Firstname = $row['firstname'] after the $mail_to Then adding function sendEmail($mail_to, $Firstname) It's still not pulling up in the email. What am I missing? Quote Link to comment https://forums.phpfreaks.com/topic/293374-placing-users-first-name-in-the-e-mail-message/#findComment-1500770 Share on other sites More sharing options...
Solution requinix Posted December 27, 2014 Solution Share Posted December 27, 2014 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"; 1 Quote Link to comment https://forums.phpfreaks.com/topic/293374-placing-users-first-name-in-the-e-mail-message/#findComment-1500773 Share on other sites More sharing options...
demeritrious Posted December 27, 2014 Author Share Posted December 27, 2014 Great it worked thank you very much! My last question is how can I loop $billname inside the email? Quote Link to comment https://forums.phpfreaks.com/topic/293374-placing-users-first-name-in-the-e-mail-message/#findComment-1500775 Share on other sites More sharing options...
requinix Posted December 27, 2014 Share Posted December 27, 2014 Loop what? There's only one billname in each row... Quote Link to comment https://forums.phpfreaks.com/topic/293374-placing-users-first-name-in-the-e-mail-message/#findComment-1500777 Share on other sites More sharing options...
demeritrious Posted December 27, 2014 Author Share Posted December 27, 2014 If the user has multiple bills due on the same day. Is there a way to loop the bill name inside the email? Quote Link to comment https://forums.phpfreaks.com/topic/293374-placing-users-first-name-in-the-e-mail-message/#findComment-1500778 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.