4
votes

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>
5
this->email->to('abc@gmail.com','xyz@gmail.com'); - Prashant Srivastav
not like this.i want to enter each id in view through the textbox - robins
not that way..enter email ids through front end - robins
if you have googled it for once, you'll find this on top 3 links - Tirthak Shah

5 Answers

6
votes

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('user1@gmail.com','user2@gmail.com''user3@gmail.com');

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

Or also can give multiple emails as it is

this->email->to('user1@gmail.com','user2@gmail.com''user3@gmail.com');

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));
3
votes

You can do it this way $this->email->to('one@example.com, two@example.com, three@example.com');

You can also pass an array of email addresses Like

$list = array('one@example.com', 'two@example.com', 'three@example.com');


$this->email->to($list);
2
votes

See this answer of stackoverflow.

public function shopshare($userid, $shopname, $shop_id) {

    $from_email = $this->input->post('from_email');
    $to_email = $this->input->post('to_email');
    foreach ($to_email as $key => $value) {
        $subject_url = $this->input->post('url');
        $message = $this->input->post('message');
        $this->email->from($from_email);
        $this->email->to($value);
        $this->email->subject($url);
        $this->email->message($message);
        $this->email->send();

        $this->email->clear();
    }
    redirect(base_url() . 'shop/shopDetail/' . $shop_id . '/' . $shopname);
}

As per above code you need to foreach with email clear then your code work proper .

2
votes

View

<input id="to_email" type="text" name="to_email[]" placeholder="To Email Address">
<input id="to_email1" type="text" name="to_email[]" placeholder="To Email Address">
<input id="to_email2" type="text" name="to_email[]" placeholder="To Email Address">

Controller

public function shopshare($userid,$shopname,$shop_id)
    {

        $from_email=$this->input->post('from_email');
        $to_email = implode(',',$this->input->get_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);
    }

For single Text box

<input id="to_email" type="text" name="to_email" placeholder="To Email Address">

Controller

public function shopshare($userid,$shopname,$shop_id) {

        $from_email=$this->input->post('from_email');
        $to_email = $this->input->get_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);
    }

You can use valid_email() for email validation http://www.codeigniter.com/user_guide/helpers/email_helper.html

2
votes

I got answer using the code below

    public function shopshare($userid, $shopname, $shop_id)
    {
         $from_email = $this->input->post('from_email');
         $to_email = $this->input->post('to_email');
         $to_mail = explode(',', $to_email);
         $mail_count= count($to_mail);
         for($i=0;$i<$mail_count;$i++)
         {
             $mail_id = TRIM($to_mail[$i]);
             $subject_url = $this->input->post('url');
             $message = $this->input->post('message');
             $this->email->from($from_email);
             $this->email->to($mail_id);
             $this->email->subject($url);
             $this->email->message($message);
             $this->email->send();
             $this->email->clear();
        }
        redirect(base_url() . 'shop/shopDetail/' . $shop_id . '/' . $shopname);
    }