我需要发送存储在数据库中的多封电子邮件
$mysqli = $this->connection(); //connect to db
$dati = $mysqli->query("SELECT * FROM feed where active='1'");//select emails
while ($resulta = $dati->fetch_array()) { //while to show
$email = $resulta['email']; //each mail
}
上面的代码就是我使用的但我需要在外面用邮件功能发送电子邮件如果发送正确,则返回true函数:
这就是功能:
function feed_mail($id){
$mysqli = $this->connection(); //connect to db
$dati = $mysqli->query("SELECT * FROM feed where active='1'");//select emails
while ($resulta = $dati->fetch_array()) { //while to show
$email = $resulta['email']; //each mail
}
if(mail($email, $asunto, $html,$header)){
return true;
return false;
}
}
$email将是存储在数据库中的每封邮件$asunto,$html,$header我没有在这里添加代码
那么我如何发送每封电子邮件呢?
function feed_mail($id){
$mysqli = $this->connection(); //connect to db
$dati = $mysqli->query("SELECT * FROM feed where active='1'");//select emails
while ($resulta = $dati->fetch_array()) { //while to show
$email = $resulta['email']; //each mail
if(mail($email, $asunto, $html,$header)){
continue;
}else{
return false;
}
}
return true;
}
你在寻找这样的东西吗:
请添加你的$asunto,$html,$headeras
——你的问题中没有提到它们。
<?php
function feed_mail($id){
$mysqli = $this->connection(); //connect to db
$dati = $mysqli->query("SELECT * FROM feed where active='1'");//select emails
$emailArr = array();
while ($resulta = $dati->fetch_array()) { //while to show
array_push($emailArr,$resulta['email']);
}
foreach($emailArr as $emails){
if(mail($emails, $asunto, $html,$header)){
return true;
}else{
return false;
}
}
}
?>
您还可以将所有电子邮件捕获到一个数组中
$email = array();
while ($resulta = $dati->fetch_array()) { //while to show
$email[] = $resulta['email']; //each mail
}
然后在一封电子邮件中发送所有邮件(或至少在一封邮件中发送多封),并在邮件头中以CC的形式附上这些邮件。阅读这篇文章。