我需要检索数据库中的行然后通过电子邮件发送该列表

时间:2015-09-01 20:48:46

标签: php mysql email

如何从数据库中检索行,然后使用mail()函数将该列表发送到我的php文件中。

我使用的查询有效:

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

但我遇到的问题是列出所有数据然后发送电子邮件,我只从数据库中获得一个结果,但不是所有行。我知道它的循环设置方式,这就是我需要帮助的地方。

以下是完整代码减去数据库连接。

$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);

1 个答案:

答案 0 :(得分:1)

试试

$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);