从Mysql中抓取变量并在循环中放入电子邮件正文

时间:2016-05-02 22:38:18

标签: php mysql email phpmailer

所以我有一个循环,它发送电子邮件到hours_left <= .5的所有记录。

使用:$sql = 'SELECT first_name, email, last_emailed FROM clients WHERE hours_left <= .5';我抓住要发送的电子邮件。

在第4行,我认为我会将身体设置在那里,所以它可以说:&#34;你好<first_name>,检查一下。&#34;但是我不确定如何访问first_name以便身体随着每个人的变化而变化。我是php的新手,对于像我这样的假人来说,更容易解释会很棒。

while ($row = $result->fetch_assoc()) {
var_dump($row);
$mail->addAddress($row['email'], $row['first_name']);
$mail->Body = "hello" ; // <----this is where I'm clueless
if (!$mail->send()) {
    echo "Mailer Error (" . str_replace("@", "&#64;", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
    break; //Abandon sending
} else {
    echo "Message sent to :" . $row['first_name'] . ' (' . str_replace("@", "&#64;", $row['email']) . ')<br />';

    //preapre statement safer way of updating shit
    if (!($stmt = $mysqli->prepare("UPDATE clients SET last_emailed = ? WHERE email = ?"))) {
       echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
    }
    if (!$stmt->bind_param('ss', $today, $row['email'])) {
       echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
    }
    if (!$stmt->execute()) {
        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
    }
    $stmt->close();
}
// Clear all addresses and attachments for next loop
$mail->clearAddresses();
}

我无法准确地描述我应该谷歌的内容,所以现在我在这里。哦,我也使用PHPMailer,我正在发送和访问数据库,除了弄清楚如何插入将接收此消息的人first_name之外,一切都在工作。

1 个答案:

答案 0 :(得分:0)

替换此行:

$mail->Body = "hello" ; // <----this is where I'm clueless

<强>与

$mail->Body = "hello {$row['first_name']}";

$name = $row['first_name']; $mail->Body = "hello $name";

$name = $row['first_name']; $mail->Body = "hello ".$name;

PS:你必须在循环中做到这一点。