【问题标题】:Send a mail to multiple recipients in codeigniter在 codeigniter 中向多个收件人发送邮件
【发布时间】:2015-10-21 16:10:03
【问题描述】:

在我的控制器中有一个用于向一个收件人发送电子邮件的代码,但我想向多个收件人发送邮件。不使用 cc、bcc 或直接分配。我想通过前端输入逗号分隔的邮件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>

【问题讨论】:

  • this->email->to('abc@gmail.com','xyz@gmail.com');
  • 不像这样。我想通过文本框输入视图中的每个 id
  • 不是这样..通过前端输入电子邮件 ID
  • 如果你用谷歌搜索过一次,你会在前 3 个链接上找到它

标签: php codeigniter email


【解决方案1】:

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

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

 $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');

这将向您发送多封邮件。

根据 Robins 评论编辑

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

如果是单个文本框,您可以要求用户通过“,”分隔多个电子邮件。

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

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

如果您有多个文本框,请为所有文本框提供相同的名称,例如 'email[]'

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

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

【讨论】:

  • 我想使用 foreach 将每个 id 从视图带到控制器 ..没有直接分配 ..通过前端输入
【解决方案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);
    }

【讨论】:

  • 此代码满足我的所有需求..我将其发布以供将来参考
  • 我认为@php-coder 的答案在没有循环的情况下会做同样的事情,你试过了吗?
【解决方案3】:

查看

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

控制器

公共函数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

【讨论】:

  • 我只有一个文本框。将每个电子邮件 ID 以逗号分隔,通过一个文本框
  • 那么没有问题 var_dump($this->input->post('to_email')) 如果你会得到 xyz@gmail.com, prasd@gmail.com
  • 我这样得到 array(1) { [0]=> string(25) "aaa@gmail.com,bb@gmailcom" }
  • 仅在名称中使用 to_email 否则它将是数组
  • 现在得到这个字符串(27) "aaaa@gmail.com,bb@gmail.com"
【解决方案4】:

查看thisstackoverflow 的回答。

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

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

【讨论】:

    【解决方案5】:

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

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

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

    【讨论】:

    • 从前端获取电子邮件 ID 并将其设为数组或逗号分隔列表。
    • 如果它是单个文本框并且如果用户输入多个电子邮件,则通过','分隔。那么你可以试试这个 $mail_list = $this->input->post('email'); this->email->to($mail_list);
    • 我也试试这个..rite?
    • @Suvashsarker 如何发送 one@example.com,two@example.com 这些是从数据库中作为变量检索的,例如 $one = one@example.com, $two = two@example.com
    • 你可以创建一个像 $list[] = $one;$list[] = $two; 这样的数组
    猜你喜欢
    • 2012-10-23
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 2012-05-18
    相关资源
    最近更新 更多