0

I have this script to send an email containing information from my database. The user can have 1+ items in the database with it's location. So when I empty the rows that match the user the amount of emails sent equals the number of rows they have. So if they have 8 items in the database it send 8 emails. Each adds an item. So the first email has one item, the second with two items, and so on. I am trying to find a simple way to make it get all the information before sending the email so the customer only gets one email. The logical way would be to echo the information but I can't do that with a php variable. I didn't include the query and database connection in the code below. Any help would be loved.

while ($row = mysql_fetch_array($query)) {
$Items .=  $row['Items']. " - Aisle " .$row['Loc']. "<p>&nbsp;</p>";
$to = "[email protected]";
$from = "[email protected]";
$subject = "Test";
$message = "$Items";
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "From: [email protected]";
mail($to, $subject, $message, $headers);
}
3
  • 1
    Why is $Items concatenated? Commented May 17, 2013 at 17:18
  • With out the "." it only sends one of the items in the database. It appears that the dot must be included to send multiple items. Also I figured it out thanks to Eugene. Thanks to anyone that replied with help. Commented May 17, 2013 at 17:28
  • You should start it with a normal equal and then concatenated otherwise it won't work correctly Commented May 17, 2013 at 17:29

3 Answers 3

1

Just move send function out of cycle:

while ($row = mysql_fetch_array($query)) {
     $Items .=  $row['Items']. " - Aisle " .$row['Loc']. "<p>&nbsp;</p>";

 }
 if ($Items != '') {
      $to = "[email protected]";
      $from = "[email protected]";
      $subject = "Test";
      $message = "$Items";
      $headers = "MIME-Version: 1.0 \r\n";
      $headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
      $headers .= "From: [email protected]";
      mail($to, $subject, $message, $headers);
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Worked great. I appreciate the help.
@BobStone You should flag the answer as correct to give him his rep.
0

When you iterate over the items, you should only build a message and not the entire email.. It's hard to do much more than the following without knowing more about your query. I'll give it a shot anyway:

$message = '';

while ($row = mysql_fetch_array($query)) {
     $Items =  $row['Items']. " - Aisle " .$row['Loc']. "<p>&nbsp;</p>";
     $message .= "$Items";
}

$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "From: [email protected]";
mail($to, $subject, $message, $headers);

Note: I wouldn't implement this code as is. It's meant to serve as an example on how to structure your code.

Comments

0

Your concatenation is wrong, you should first declare your variable as empty outside your loop

$Items = '';

Then start your loop and get all data you need concatenating your variable

while ($row = mysql_fetch_array($query)) 
{
    $Items .=  $row['Items']. " - Aisle " .$row['Loc']. "<p>&nbsp;</p>";
}

Now you are ready for send email, outside your loop or you will end up with one email for each cicle. So your code would look like this

$Items = '';
while ($row = mysql_fetch_array($query)) 
{
    $Items .=  $row['Items']. " - Aisle " .$row['Loc']. "<p>&nbsp;</p>";
}
$from = "[email protected]";
$subject = "Test";
$message = "$Items";
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "From: [email protected]";
mail($to, $subject, $message, $headers);

Then I would like you to remember that mysql_* functions are deprecated so i would advise you to switch to mysqli or PDO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.