简体   繁体   English

如何一次发送电子邮件给PHP中包含多个电子邮件地址的选定列表

[英]How to send email at once for selected list of email addresses having more than one in php

This is my code for sending emails to only one email address. 这是我的代码,用于仅将电子邮件发送到一个电子邮件地址。 As I'm sending birthday wishes to customers from my project I need send it all together not as selecting one by one. 当我从我的项目向客户发送生日祝福时,我需要将其全部发送出去,而不是一一挑选。 The below code sends emails to customers successfully when there is only one birthday event. 当只有一个生日事件时,以下代码将电子邮件成功发送给客户。 Please help me to change this code to do so. 请帮助我更改此代码。

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

You are trying to pass an array to email->to . 您正在尝试将数组传递给email->to But it should be a comma , separated values like this. 但这应该是逗号,像这样的分隔值。

$this->email->to('one@example.com, two@example.com, three@example.com');

For more reference check here 欲了解更多参考,请点击这里

To send email at once, 要一次发送电子邮件,

 implode(",",$emailarray);

else 其他

concat() in your query.

Seems like you use CodeIgniter 好像您使用CodeIgniter

First, it's a realy bad idea to send birthday greetings to the multiple recepients at once(in to header of one email) because it's privacy-less - all recepients will see email addresses of each other. 首先,它是一个真的坏主意,生日问候一次发送到多个受助(在to一个电子邮件的标题),因为它的私密性,更少的- 所有的,收件人看到对方的电子邮件地址。

As i understood the prbolem is that your getEmail function returns email only for one(first in sql resultset) customer, but you need to send greetings to all customers. 据我了解,prbolem是您的getEmail函数仅返回一个(第一个SQL结果集)客户的email ,但您需要向所有客户发送问候。

So you should fetch not one, but all birthday boys and then iterate over them. 因此,您应该获取不是所有生日男孩,而是所有生日男孩,然后遍历它们。

1 First replace your getEmail function to the getBirthdayBoys one which will return an array of customers. 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 And then use updated function within your mail sending function like this: 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();
}

Attention : But you still have a few problems with your sendMailToComplete($ID) function. 注意 :但是sendMailToComplete($ID)函数仍然存在一些问题。

  1. $row is undefined variable. $row是未定义的变量。
  2. $ID argument never used 从未使用过$ID参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM