homepage Welcome to WebmasterWorld Guest from 54.167.96.124
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Email Message Loop
loop through a email script
airpirate




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

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;
}

?>

 

Habtom




msg:4426688
 10:40 pm on Mar 8, 2012 (gmt 0)

Drag the "While" closing curly bracket "}" to the very end of the script - just before "?>" - and it should do what you just described.

Habtom




msg:4426689
 10:44 pm on Mar 8, 2012 (gmt 0)


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;
}
}
?>

airpirate




msg:4426692
 10:58 pm on Mar 8, 2012 (gmt 0)

You are Genius Habtom! Thank you!

rocknbil




msg:4427046
 4:55 pm on Mar 9, 2012 (gmt 0)

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

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

";

airpirate




msg:4427050
 5:02 pm on Mar 9, 2012 (gmt 0)

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!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved