Welcome to WebmasterWorld Guest from 54.167.185.18

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Email Message Loop

loop through a email script

   
10:31 pm on Mar 8, 2012 (gmt 0)

5+ Year Member



Hi,

I am trying to come up with a script that emails tracking numbers to my customers that are stored in our database. When the email is sent I want it to update the line in the database showing that an email has been sent to that client so that I don't keep sending them emails daily.

I got the following script to work, but it only sends and updates the last row in the table. I need it to somehow loop through and send the email to each client that has not had an email sent to them instead of just the last row.

Any ideas? I would really appreciate the help.


<?php
include_once('config.php');

$send_db = 'db';
$send_table= 'table';

//query to pull names and email addresses from the db
$result = mysql_query("SELECT distinct(order_num),email,name,tracking,id,date FROM $send_db.$send_table where email_sent is NULL") or die ('Error: '.mysql_error());

if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to send, so I am exiting";
exit;
}

while ($row = mysql_fetch_array($result)) {
$email_id = $row['id'];
$order_num = $row['order_num'];
$name = $row['name'];
$email = $row['email'];
$date = $row['date'];
$tracking = $row['tracking'];
}


$to = "$email";
$headers = "From: donotreply@website.com\r\n";
$headers .= "Reply-To: donotreply@website.com\r\n";
$subject = "Your Order Number $order_num Has Shipped";
$body = "\n\n
---------------------------------------------\n
DO NOT REPLY TO THIS MESSAGE \n
---------------------------------------------\n
\n
$name\n
\n
Thank you for Ordering our product!\n
\n
Your Order Number $order_num has shipped on $date and should arrive to you shortly.\n
Your Order Shipped via the USPS First Class Mail. The tracking number for your package is $tracking \n
\n
For Customer Service please call phone number. For Returns, please contact Customer Service at the above phone number for Return Authorization. \n
\n
Thank you! \n
Company \n
\n
\n
\n
\n
";
mail($to, $subject, $body, $headers);

//update email_sent to Y
$result1 = mysql_query("UPDATE $send_db.$send_table set email_sent = 'Y' where order_num = '$order_num' and email = '$email';");

if (!$result1) {
echo "Could not successfully run query against the DB: " . mysql_error();
exit;
}

?>
10:40 pm on Mar 8, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Drag the "While" closing curly bracket "}" to the very end of the script - just before "?>" - and it should do what you just described.
10:44 pm on Mar 8, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




while ($row = mysql_fetch_array($result)) {
$email_id = $row['id'];
$order_num = $row['order_num'];
$name = $row['name'];
$email = $row['email'];
$date = $row['date'];
$tracking = $row['tracking'];

$to = "$email";
....



...
if (!$result1) {
echo "Could not successfully run query against the DB: " . mysql_error();
exit;
}
}
?>
10:58 pm on Mar 8, 2012 (gmt 0)

5+ Year Member



You are Genius Habtom! Thank you!
4:55 pm on Mar 9, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



When you do this

$body = "
---------------------------------------------
DO NOT REPLY TO THIS MESSAGE
---------------------------------------------
";

You're already adding newlines. Adding those \n's adds another newline. If that's what you really want, all you need is

$body = "

---------------------------------------------

DO NOT REPLY TO THIS MESSAGE

---------------------------------------------

";
5:02 pm on Mar 9, 2012 (gmt 0)

5+ Year Member



Good to know rocknbil. It did bug me that there was extra spaces in the emails but didn't know what to do about it. That will help me format my email better. Thank you!