发送存储在数据库中的多封电子邮件

时间:2015-04-05 08:49:41

标签: php email mysqli

我需要发送存储在数据库中的多封电子邮件

$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我没有在这里添加代码

所以我如何发送每封电子邮件?

3 个答案:

答案 0 :(得分:0)

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;
}

答案 1 :(得分:0)

你在寻找这样的东西:

请在问题中添加您未提及的$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;
        }
    }
}
?>

答案 2 :(得分:0)

您还可以将所有电子邮件捕获到数组中

$email = array();
while ($resulta = $dati->fetch_array()) { //while to show
    $email[] = $resulta['email']; //each mail
}

然后将它们全部发送到一封电子邮件中(或者至少在一封电子邮件中),将它们作为CC添加到标题中。阅读此article