在codeigniter中向多个收件人发送邮件

时间:2015-07-30 10:35:23

标签: php codeigniter email

在我的控制器中有一个代码,用于向一个收件人发送电子邮件,但我想向多个收件人发送邮件。不使用cc,密送或直接分配。我想通过前端输入逗号分隔的邮件ID

如何以逗号分隔的形式获取每个邮件ID?

控制器:

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

查看:

<label>
    <span>To:</span>
    <input id="to_email" type="text" name="to_email[]" placeholder="To Email Address">
</label>

5 个答案:

答案 0 :(得分:4)

您可以使用收件人数组,或者如果他们存储在数据库中,您可以再次检索并将所有收件人存储在数组中,而不是通过&#39;,&#39;

进行内爆。

例如,如果您从数据库创建数组或获取数组结果,

 $recipients = Array('user1@gmail.com','user2@gmail.com''user3@gmail.com');

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

或者也可以提供多封电子邮件

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

这会向您发送多封邮件。

根据罗宾斯评论编辑

当您评论您希望从前端文本框中输入多个条目时,

如果是单个文本框,您可以要求用户输入多封电子邮件,&#39;,&#39;分开。

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

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

如果您有多个文本框,则为所有文本框命名相同的名称,例如&#39; email []&#39;

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

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

答案 1 :(得分:2)

你可以这样做$this->email->to('one@example.com, two@example.com, three@example.com');

您还可以传递一组电子邮件地址,如

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


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

答案 2 :(得分:2)

查看

<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">

控制器

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

单个文本框

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

控制器

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

您可以使用valid_email()进行电子邮件验证http://www.codeigniter.com/user_guide/helpers/email_helper.html

答案 3 :(得分:2)

请参阅stackoverflow的this答案。

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

根据上面的代码,您需要使用电子邮件清除,然后您的代码才能正常工作。

答案 4 :(得分:2)

我使用下面的代码得到答案

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