Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send a mail to multiple recipients in codeigniter

In my controller there is a code for sending email to one recipient, But I want to send mail to multiple recipients. Not use cc,bcc or direct assignment. I want to enter comma separated mail ids through the front end

how to take each mail id in comma separated form?

Controller:

public function shopshare($userid,$shopname,$shop_id)
{
    $from_email=$this->input->post('from_email');
    $to_email=$this->input->post('to_email');
    $subject_url=$this->input->post('url');
    $message=$this->input->post('message');
    $this->email->from($from_email);
    $this->email->to($to_email); 
    $this->email->subject($url);
    $this->email->message($message);    
    $this->email->send();

    redirect(base_url().'shop/shopDetail/'.$shop_id.'/'.$shopname);
}

View:

<label>
    <span>To:</span>
    <input id="to_email" type="text" name="to_email[]" placeholder="To Email Address">
</label>
like image 747
robins Avatar asked Dec 05 '22 20:12

robins


2 Answers

You can use array of recipients, or if they stored in database you can retrieve and store all again in array and than implode them by ','.

for example if you create array or get array result from database,

 $recipients = Array('[email protected]','[email protected]''[email protected]');

     this->email->to(implode(', ', $recipients));

Or also can give multiple emails as it is

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

This will send you multiple mail.

EDIT as per Robins Comment

as you comment that you want to have multiple entry from front-end text box,

if it is a single text box you can ask user to have multiple email by ',' separate.

 $mails = $this->input->post('email');

 this->email->to($mails);

if you have multiple text boxes give all text boxes same name like 'email[]'

 $mails = $this->input->post('email');

 this->email->to(implode(', ', $mails));
like image 97
php-coder Avatar answered Dec 19 '22 13:12

php-coder


You can do it this way $this->email->to('[email protected], [email protected], [email protected]');

You can also pass an array of email addresses Like

$list = array('[email protected]', '[email protected]', '[email protected]');


$this->email->to($list);
like image 28
Suvash sarker Avatar answered Dec 19 '22 12:12

Suvash sarker