我尝试使用此代码向具有相同类别的人发送电子邮件。
if ($action = 'message_til_flere') {
$vagt_ansat_email = $_POST['vagt_ansat_email'];
$select_alle_mails = " SELECT vagt_ansat_email FROM vagt_ansat_med_kategori WHERE vagt_kategori_id = $id ";
$til_vagter = array();
while ($row = mysql_fetch_array($select_alle_mails)) {
$til_vagter[] = $row['vagt_ansat_email'];
$to = $til_vagter;
$subject = "E-mail subject";
$headers = 'From: mywebpage' . "\r\n";
$headers .= 'Reply-To: mywebpage' . "\r\n";
$headers .= 'BCC: ' . implode(', ', $til_vagter) . "\r\n";
}
mail($to, $subject, $headers);
答案 0 :(得分:1)
$ to不能是一个数组。它必须是一个字符串。多个mailadresses必须以逗号分隔。为此,您可以使用php函数implode
$to = implode(',', $til_vagter);
示例:强>
if ($action = 'message_til_flere') {
$vagt_ansat_email = $_POST['vagt_ansat_email']; // never used in this part
$select_alle_mails = " SELECT vagt_ansat_email FROM vagt_ansat_med_kategori WHERE vagt_kategori_id = $id ";
$til_vagter = array();
$result = mysql_query($select_alle_mails); // a connection to db is required
while ($row = mysql_fetch_array($result)) { // fetch results as array
$til_vagter[] = $row['vagt_ansat_email'];
}
$to = "myemailadress@mywebsite.tld";
$subject = "E-mail subject";
$headers = 'From: mywebpage' . "\r\n";
$headers .= 'Reply-To: mywebpage' . "\r\n";
$headers .= 'BCC: ' . implode(', ', $til_vagter) . "\r\n";
$message = "Test Body";
mail($to, $subject, $message, $headers); // a message is required
}
答案 1 :(得分:1)
我个人会在循环中一次发送一封邮件,如下所示:
if ($action = 'message_til_flere') {
// $vagt_ansat_email = $_POST['vagt_ansat_email']; //not used?
$select_alle_mails = " SELECT vagt_ansat_email FROM vagt_ansat_med_kategori WHERE vagt_kategori_id = $id ";
//RUN QUERY
$sel_result = mysql_query($select_alle_mails);
while ($row = mysql_fetch_array($sel_result)) {
$subject = "E-mail subject";
$headers = 'From: mywebpage' . "\r\n";
$headers .= 'Reply-To: mywebpage' . "\r\n";
$message="dear bob, having fun?";
mail($row['vagt_ansat_email'], $subject, $message, $headers);
}
答案 2 :(得分:1)
您在获取之前未执行查询,并且您将查询字符串传递给mysql_fetch_array()
,这是不正确的。它是您需要获取的查询的结果:
$select_alle_mails = "SELECT vagt_ansat_email
FROM vagt_ansat_med_kategori
WHERE vagt_kategori_id = $id ";
$result = mysql_query($select_alle_mails);
$til_vagter = array();
while ($row = mysql_fetch_array($result)) {...}
mail()
是否对收件人有限制,但@vonHergen
和@dagon