-3

How can I retrieve rows from database then use a mail() function to then email that list in my php file.

The query that I am using which this works:

$sql = "SELECT * FROM leads WHERE date_stamp BETWEEN '$currentdate' - INTERVAL 7 DAY AND '$currentdate'";`

But the problem I am having is listing out all the data to then email, I am only getting the one result from database, but not all the rows. I know its the way my loop is setup, that is what I am needing help with.

Below is the full code minus the database connect.

$to = 'email@email.com';

// email subject     
$subject = 'Email leads beginning '.$newdate." through ".$currentdate;

// Construct email body     
$result = mysqli_query($conn, $sql);
$recipients = array();
        while($rows = mysqli_fetch_assoc($result)) {
            foreach ($result as $row) {
                $content = $row['first_name']." ".$row['last_name']."<br>";
        }
            };

$body_message = $content;

// email headers     
$headers = 'From: ' . $email_from . "\r\n";
$headers .= "Content-type: text/html\r\n";  

mail($to, $subject, $body_message, $headers);
Shehary
  • 9,926
  • 10
  • 42
  • 71
Anthony
  • 131
  • 1
  • 11

1 Answers1

1

Try that

$to = 'email@email.com';

// email subject     
$subject = 'Email leads beginning '.$newdate." through ".$currentdate;

// Construct email body     
$result = mysqli_query($conn, $sql);
$recipients = array();
$body_message = '';

while($row = mysqli_fetch_assoc($result)) {
    $body_message .= $row['first_name']." ".$row['last_name']."<br>";
}

// email headers     
$headers = 'From: ' . $email_from . "\r\n";
$headers .= "Content-type: text/html\r\n";  

mail($to, $subject, $body_message, $headers);
Maverick
  • 905
  • 8
  • 23
  • That didnt seem to work, but I was able to get it working with a simple change of my $content to $content .= – Anthony Sep 01 '15 at 21:01
  • Do you recommend your change or what I did? – Anthony Sep 01 '15 at 21:03
  • There's some strange things in your code, like `$body_message = $content;` why do you do that when you can use only one variable? Or the semicolon after the the curly brace `};` – Maverick Sep 01 '15 at 21:08
  • The semicolon was a typo. I actually replaced my code with your edit and that seems to work just fine. – Anthony Sep 01 '15 at 21:12
  • Furthermore, why you used the `foreach` with the `$results` inside the `while` cycle when you already have the `$row`? – Maverick Sep 01 '15 at 21:18
  • I'm not sure, I just used the general loop I have for something else I am using. But I changed it to your code and it is working great. – Anthony Sep 01 '15 at 21:23
  • Real quick if I wanted to take that same list and attach it in a spreadsheet, how would you recommend I do that? – Anthony Sep 01 '15 at 21:41
  • Check http://stackoverflow.com/questions/4741416/how-to-populate-excel-spreadsheet-with-mysql-data-using-php AND http://stackoverflow.com/questions/12301358/send-attachments-with-php-mail – Maverick Sep 01 '15 at 21:49