通过Codeigniter PHP发送电子邮件

时间:2015-07-14 12:29:19

标签: php codeigniter

我需要在提交表格后发送邮件。下面是我的代码,我能够得到错误。它重定向到同一页面.Below是我的控制器。如果我使用email函数,它会成功插入数据库,但如果我使用此邮件则不会插入。

function addparents()
{
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');


    $this->form_validation->set_rules('first_name','First Name','required|alpha');
    $this->form_validation->set_rules('last_name','Last Name','required|alpha');
    $this->form_validation->set_rules('mobile_number','Mobile Number','required|is_numeric|max_length[10]');
    $this->form_validation->set_rules('email_list','email_list','required|valid_email');
    $this->form_validation->set_rules('message','Message');        

    if($this->form_validation->run()== FALSE)    
    {        
        $data['mainpage']='profile';
        $data['mode']='parents';
        $this->load->view('profile',$data);
    }

    else{
        $result=$this->category_model->send_mail($this->input->post('email'));
        if($result)
        {
            $this -> profile_model -> insertparents();
            $this->flash->success('<h2>Parents Added Successfully!</h2>');
            redirect('profile');
        }
        else
        {
            $this->flash->success('<h2 style="color:red">Sorry ! Message sending failed</h2>');
            redirect('profile');
        }
    }

}

这是我发送电子邮件的模型:

function send_mail($email)
{
    $name=$this->input->post('full_name');
    $mobileno=$this->input->post('mobile');

    $messagess1=$this->input->post('message');
    $this->email->set_mailtype("html");

    $messagess = "<html>\n";
    $messagess .= "<body>
    <div>Dear ,<div>
    <p>please find below the information User sent to you.</P><br> <br>
    <table collspan=\"5\">
        <tr>
            <td>Name</td>
            <td>: </td>
            <td><strong>".$name."</strong></td>
        </tr>
    <tr>
        <td>E-mail</td>
        <td>:</td>
        <td><strong>".$email."</strong>
    </tr>

    <tr>
        <td>Mobile No</td>
        <td>:</td>
        <td><strong>".$mobileno." </strong>
    </tr>

    <tr>
        <td>Message</td>
        <td>:</td>
        <td><strong>".$messagess1."</strong>
    </tr>
    </table>
    <br><br><p>Thanks &amp; Warm Regards,</p><p>".$name."</p>";

    $messagess .= "</body>\n";
    $this->email->from($email, "Kids2Play");
    $this->email->to("ashalatha.cse76@gmail.com");

    $this->email->message($messagess);

    if($this->email->send()){
        return true;
    }
    else 
        return false;
}

1 个答案:

答案 0 :(得分:-1)

这里有完整的代码。

    function addparents()
    {
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');


    $this->form_validation->set_rules('first_name','First Name','required|alpha');
    $this->form_validation->set_rules('last_name','Last Name','required|alpha');
    $this->form_validation->set_rules('mobile_number','Mobile Number','required|is_numeric|max_length[10]');
    $this->form_validation->set_rules('email_list','email_list','required|valid_email');
    $this->form_validation->set_rules('message','Message');        

    if($this->form_validation->run()== FALSE)    
    {        
        $data['mainpage']='profile';
        $data['mode']='parents';
        $this->load->view('profile',$data);
    }

    else{
        $name=$this->input->post('full_name');
        $mobileno=$this->input->post('mobile');

        $messagess1=$this->input->post('message');
        $messagess = "<html>\n";
        $messagess .= "<body>
        <div>Dear ,<div>
        <p>please find below the information User sent to you.</P><br> <br>
        <table collspan=\"5\">
            <tr>
                <td>Name</td>
                <td>: </td>
                <td><strong>".$name."</strong></td>
            </tr>
        <tr>
            <td>E-mail</td>
            <td>:</td>
            <td><strong>".$email."</strong>
        </tr>

        <tr>
            <td>Mobile No</td>
            <td>:</td>
            <td><strong>".$mobileno." </strong>
        </tr>

        <tr>
            <td>Message</td>
            <td>:</td>
            <td><strong>".$messagess1."</strong>
        </tr>
        </table>
        <br><br><p>Thanks &amp; Warm Regards,</p><p>".$name."</p>";

        $messagess .= "</body>\n";

        $to = 'ashalatha.cse76@gmail.com';
        $subject = 'Thanks &amp; Warm Regard';
        $result=$this->sendmail($to,$subject,$messagess);
        if($result)
        {
            $this -> profile_model -> insertparents();
            $this->flash->success('<h2>Parents Added Successfully!</h2>');
            redirect('profile');
        }
        else
        {
            $this->flash->success('<h2 style="color:red">Sorry ! Message sending failed</h2>');
            redirect('profile');
        }
    }

}

function sendmail($to,$subject,$fullmsg){
$config = Array(
       'mailtype' => 'html',
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.gmail.com',
        'smtp_port' => 465,
        'smtp_user' => 'ashalatha.cse76@gmail.com', // change it to yours
        'smtp_pass' => 'your gmail password', // change it to yours
        'mailtype' => 'html',
        'charset' => 'iso-8859-1',
        'wordwrap' => TRUE
      );


        $this->load->library('email', $config);
        $this->email->set_newline("\r\n");
        $this->email->from(''); // change it to yours
        $this->email->to($to);// change it to yours
        $this->email->subject($subject);
        $this->email->message($fullmsg);
        if($this->email->send())
       {
          return true;
       }
       else
      {
       show_error($this->email->print_debugger());
      }

    }