Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中为具有多个电子邮件地址的选定电子邮件地址列表一次发送电子邮件_Php_Mysql_Sql_Email - Fatal编程技术网

如何在php中为具有多个电子邮件地址的选定电子邮件地址列表一次发送电子邮件

如何在php中为具有多个电子邮件地址的选定电子邮件地址列表一次发送电子邮件,php,mysql,sql,email,Php,Mysql,Sql,Email,这是我只向一个电子邮件地址发送电子邮件的代码。当我从我的项目中向客户发送生日祝福时,我需要一起发送,而不是逐个选择。当只有一个生日活动时,以下代码将成功向客户发送电子邮件。请帮助我更改此代码以执行此操作 function getEmail() { $query = $this->db->query("select ci.* from customer_info ci where month(b_day) = month(curdate()) an

这是我只向一个电子邮件地址发送电子邮件的代码。当我从我的项目中向客户发送生日祝福时,我需要一起发送,而不是逐个选择。当只有一个生日活动时,以下代码将成功向客户发送电子邮件。请帮助我更改此代码以执行此操作

function getEmail()
    {       
        $query = $this->db->query("select ci.* from customer_info ci where month(b_day) = month(curdate()) and day(b_day) = day(curdate());");
        $data = $query->row_array();
        $value = $data['email'];
        return $value;
    }

public function sendMailToComplete($ID)
    {
        $email = $this->getEmail();
        foreach ($this->get_db->getEmailConfig() as $row) {
            $config = Array(
                            'protocol' => $row->protocol,
                            'smtp_host' => $row->host,
                            'smtp_user' => $row->user,
                            'smtp_pass' => $row->pass,
                            'smtp_port' => $row->port,
                            'smtp_timeout'=>$row->timeout,
                            'mailtype'  => $row->mailtype, 
                            'charset'   => $row->charset
                            );
            $this->load->library('email', $config);
            $this->email->set_newline("\r\n");
            $this->email->from($row->from, 'ABC');
            $this->email->to($email);
            $this->email->subject('Birthday Greetings');
            $this->email->message('Many Happy Returns Of The Day!!! Have A Wonderful Birthday... .');
        }

       $this->email->send();
    }

您正试图将数组传递到
电子邮件->发送到
。但是它应该是一个逗号
像这样分隔的值

$this->email->to('one@example.com, two@example.com, three@example.com');
更多参考检查

要立即发送电子邮件

 implode(",",$emailarray);
否则


似乎您使用了
CodeIgniter

首先,一次向多个收件人发送生日祝福(在一封电子邮件的
标题中)是一个非常糟糕的主意,因为这样会减少隐私-所有收件人都会看到彼此的电子邮件地址。

据我所知,问题是您的
getEmail
函数只为一个(sql结果集中的第一个)客户返回
email
,但您需要向所有客户发送问候

function getBirthdayBoys()
{
    $query = $this->db->query("select ci.* from customer_info ci where month(b_day) = month(curdate()) and day(b_day) = day(curdate());");
    return $query->result_array();
}
所以你应该把所有的生日男孩,而不是一个,然后迭代他们

1首先将
getEmail
函数替换为
getBirthdayBoys
函数,该函数将返回一系列客户

function getBirthdayBoys()
{
    $query = $this->db->query("select ci.* from customer_info ci where month(b_day) = month(curdate()) and day(b_day) = day(curdate());");
    return $query->result_array();
}
2然后在邮件发送功能中使用更新的功能,如下所示:

public function sendMailToComplete($ID)
{
    $customers = $this->getBirthdayBoys();
    if (empty($customers)) {
        return;
    }

    $config = array(
        'protocol' => $row->protocol,
        'smtp_host' => $row->host,
        'smtp_user' => $row->user,
        'smtp_pass' => $row->pass,
        'smtp_port' => $row->port,
        'smtp_timeout'=>$row->timeout,
        'mailtype'  => $row->mailtype,
        'charset'   => $row->charset

    );
    $this->load->library('email', $config);

    foreach ($customers as $customer)
    {
        $email = $customer['email'];
        $this->email->clear();  
        $this->email->set_newline("\r\n");
        $this->email->from($row->from, 'ABC');
        $this->email->to($email);
        $this->email->subject('Birthday Greetings');
        $this->email->message('Many Happy Returns Of The Day!!! Have A Wonderful Birthday... .');
        $this->email->send();
    }

    $this->email->clear();
}
注意: 但是您的
sendMailToComplete($ID)
函数仍然存在一些问题

  • $row
    是未定义的变量
  • $ID
    从未使用参数

  • 您必须发送逗号
    分隔值。它应该作为数组接收,对吗?您使用的是codeigniter吗?是的,我使用的是CordeIgniter谢谢,我会尝试的。当然,让我知道结果。哦,是的,它正在工作。。它发送到我包含的电子邮件地址。。只要一个问题,如果你能帮忙的话..帮我把这个带到一个数组
    使用内爆(“,”,$email)
    或在查询中使用
    concat()
    。@Akisha你得到所需的行为了吗?噢,非常感谢你。。这是完美的工作…谢谢你指出我的错误以及..顺便说一句,我用了$ID作为进一步的参考,因为我认为我可能需要它。。但这是个错误,我不再需要它了。其次,$行是我上面发布的代码中定义的变量。。。非常感谢你的帮助..我真的非常感谢..@Akisha你不是一次发送电子邮件,而是在forloop中一个接一个地发送。这不是个好主意。@NiranjanNRaju你为什么这么认为?一次向多人发送一封邮件有以下缺点:1。侵犯隐私-最近的电子邮件地址将被披露给对方2。单个电子邮件的文本应该是一般性的,不能根据个人的喜好进行个性化name@PavelSadchenko我的评论是基于这个问题。