Akisha
Akisha

Reputation: 123

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

Upvotes: 1

Views: 540

Answers (2)

Pavel Sadchenko
Pavel Sadchenko

Reputation: 131

Seems like you use 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.

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.

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.

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:

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.

  1. $row is undefined variable.
  2. $ID argument never used

Upvotes: 1

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

You are trying to pass an array to email->to. But it should be a comma , separated values like this.

$this->email->to('[email protected], [email protected], [email protected]');

For more reference check here

To send email at once,

 implode(",",$emailarray);

else

concat() in your query.

Upvotes: 1

Related Questions