使用带有codeigniter的smtp向具有不同消息的2个收件人发送电子邮件

时间:2018-05-16 14:54:05

标签: php codeigniter email

我使用SMTP with CodeIgniter成功向一位收件人发送电子邮件。第一个收件人从表单接收邮件。我现在的目标是在同一个功能中向另一个收件人发送另一条消息。 我的问题是发送另一条消息,如$this->email->message("Thank you for sending your info....");如何使用$this->email->message两次?因为我已经使用过$this->email->message($data); 。请帮我。 感谢

我向第一位收件人发送电子邮件的代码

public function send_cita_from_out()
{
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'xxxxx',
'smtp_port' => 25,
'smtp_user' => 'xxxxx', // change it to yours
'smtp_pass' => 'xxxx', // change it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
 );
$name = $this->input->post('name');
$id= $this->input->post('id');
$date= $this->input->post('date');
$tel = $this->input->post('tel');
$email= $this->input->post('email');
    $data =
    "
    <html>
    <body>

    <p>NAME</strong> : $name </p>
    <p>ID</strong>: $id</p>
    <p>DATE</strong> : $date</p>
    </body>
    </html>";
   $this->load->library('email', $config);
   $this->email->set_newline("\r\n");
   $this->email->set_mailtype("html");     
   $this->email->from($email); // change it to yours
   $this->email->to('xxxxxxx@gmail.com');// change it to yours
   $this->email->subject('xxx');
   $this->email->message($data);

    //HOW TO SEND THIS EMAIL WITH DIFFERENT MESSAGE
   //$this->email->to(email);// change it to yours
   //$this->email->subject('xxx');
   //$this->email->message('Thank you for contacting us..');
 if($this->email->send())
 {
   echo 'Email sent.';
 }
 else
 {
  show_error($this->email->print_debugger());
 }

1 个答案:

答案 0 :(得分:1)

<强> HTML

    <form id="subfrm">
        <div class="col s12 m3">
            <input type="text" class="inputBox" name="fname" placeholder="Enter First Name">
        </div>

       <div class="col s12 m4">
            <input type="text" class="inputBox" name="email" placeholder="Enter `enter code here`E-mail">
       </div>   
       <div class="col s12 m4">
            <input type="text" class="inputBox" name="msg" placeholder="Enter E-message">
      </div>    
      <div class="col s12 m2">
            <input type="button" name="subscribe" class="btn btn-flat btn-subscribe" value="Subscribe">
      </div>
    </form>

<强> JS

$(".btn-subscribe").on("click",function()
 {
    var subbfrm = new FormData($("#subfrm")[0]);
    $.ajax({
    url : baseurl+"Home/sendsubscriber",
    type :"POST",
    data :subbfrm,  
    contentType:false,
    processData:false,
    success:function(res)
    {
        alert("Good job!");
    }
    });
 });
  

如果要向不同的用户发送相同的消息,可以使用静态   消息控制器。

静态消息控制器

  public function sendsubscriber()
  {
    $data=array(
            'fname'=>$_POST['fname'],
            'email'=>$_POST['email']
        );

        $email=$data['email'];

        $send = $this->db->insert('tbl_name',$data);

        if($send > 0){

             $config = Array(
              'protocol' => 'smtp',
              'smtp_host' => 'ssl://smtp.googlemail.com',
              'smtp_port' => 465,
              'smtp_user' => 'xxxxxxxx', // change it to yours
              'smtp_pass' => 'xxxxxxxx', // change it to yours
              'mailtype' => 'html',
              'charset' => 'iso-8859-1',
              'wordwrap' => TRUE
            );

                   $message = '';
                  $this->load->library('email', $config);
                  $this->email->set_newline("\r\n");
                  $this->email->from($email); // change it to yours
                  $this->email->to('XXXXXX');// change it to yours
                  $this->email->subject('Welcome');
                  $this->email->message("Thank you for contacting us..");
                  if($this->email->send())
                 {
                  echo 'Email sent.';
                 }
                 else
                {
                  show_error($this->email->print_debugger());
                }


        }
    }
  

如果您想向不同的用户发送不同的消息,您可以使用动态   消息控制器。

动态讯息控制器

  public function sendsubscriber()
  {
    $data=array(
            'fname'=>$_POST['fname'],
            'email'=>$_POST['email']
        );

        $email=$data['email'];
        $msg=$data['msg'];


        $send = $this->db->insert('tbl_name',$data);

        if($send > 0){

             $config = Array(
              'protocol' => 'smtp',
              'smtp_host' => 'ssl://smtp.googlemail.com',
              'smtp_port' => 465,
              'smtp_user' => 'xxxxxxxx', // change it to yours
              'smtp_pass' => 'xxxxxxxx', // change it to yours
              'mailtype' => 'html',
              'charset' => 'iso-8859-1',
              'wordwrap' => TRUE
            );

                   $message = '';
                  $this->load->library('email', $config);
                  $this->email->set_newline("\r\n");
                  $this->email->from($email); // change it to yours
                  $this->email->to('XXXXXX');// change it to yours
                  $this->email->subject('Welcome');
                  $this->email->message($msg);
                  if($this->email->send())
                 {
                  echo 'Email sent.';
                 }
                 else
                {
                  show_error($this->email->print_debugger());
                }


        }
    }