2017-06-27 54 views
0

我是PHP新手,试图制作一个简单的页面,用户将请求他们想要的一些服务。通过验证后,我将表单数据保存到数据库中。当有人要求某些服务时,该程序还应发送电子邮件通知。所以,直到这我的代码工作。使用codeigniter成功提交表单数据后发送给电子邮件

但是,我希望在表单成功提交时获取电子邮件正文/消息中的表单数据,但到目前为止,我无法做到这一点。我Google搜索并尝试了一些,但没有工作。

如何使用codeigniter来做到这一点?有人可以指导我如何解决这个问题?如果您需要完整的HTML代码,请告诉我。我很感谢你的帮助。

Contact.php文件(控制器)

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

class Contact extends CI_Controller { 


function __construct() 
{ 

    parent::__construct(); 

    //load the contact_model 
    $this->load->model('contact_model'); 

} 


public function index() 
{ 

    //load view pages 
    $this->load->view('Template/header'); 
    $this->load->view('contact'); 
    $this->load->view('Template/footer'); 

} 


public function validate_form() 
{ 

    $this->load->library('form_validation'); 

    $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 

    //FORM VALIDATION 

    //First Name 
    $this->form_validation->set_rules('first_name', 'First Name', 'trim|required|min_length[3]|max_length[17]'); 

    //Last Name 
    $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|min_length[3]|max_length[17]'); 

    //User Name 
    $this->form_validation->set_rules('user_name', 'User Name', 'trim|required|alpha|strtolower|min_length[3]'); 


    //Email 
    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email'); 

    //Manager's Full Name 
    $this->form_validation->set_rules('m_name', 'Full Name', 'trim|required'); 

    //Comment 
    $this->form_validation->set_rules('comment', 'Comment', 'trim|required'); 


    if ($this->form_validation->run() == FALSE) 

      { 

       //echo validation_errors(); 
       $this->load->view('Template/header'); 
       $this->load->view('contact'); 
       $this->load->view('Template/footer'); 

       //$redirect = $this->input->post('url'); 
       //$this->session->set_flashdata('errors',validation_errors());  
      } 

      else 
      { 

       $specialised_category = $this->input->post('checkbox_cat'); 

       $data = array(

       'f_name' => $this->input->post('first_name'), 
       'l_name' => $this->input->post('last_name'), 
       'user_name' => $this->input->post('user_name'), 
       'email' => $this->input->post('email'), 
       'fullname' => $this->input->post('m_name'), 
       'comment' => $this->input->post('comment'), 
       'city' => $this->input->post('city'), 
       //encoding to JSON and comma sepated values 
       'services_list' => json_encode(implode(",", $specialised_category)) 

       ); 

       //inserting data 
       //$this->db->insert('sysops_tbl', $data); 
       $this->contact_model->insert_into_db($data); 

       //load the form success message once submitted correctly 
       $this->load->view('formsuccess'); 

       $this->send($data); 

       //redirect to the main page 
       //redirect(base_url()); 

      } 

    } 


public function send($value='') 

{ 
    //load the email library 
    $this->load->library('email'); 

    //Sending email 
       $config_email = array(
       'mailtype' => 'html', 
       'charset' =>'iso-8859-1', 
       'wordwrap' => TRUE 
       ); 
       //debug 
       //print_r($value); 

       //override the config from text to html 
       $this->email->initialize($config_email); 

       //printing manager email and name 
       $this->email->from($this->input->post('email'),$this->input->post('m_name')); 
       //receipant email 

       $this->email->to('[email protected]'); 
       //header 
       //$this->email->set_header(json_encode(implode(",", $this->input->post('checkbox_cat'))),'binded'); 
       //email subject 
       $this->email->subject('We need user access for'); 
       //want to inject values here in the email message 
       $this->email->message('Check above !!:'); 
       //print the message if email is sent 
       if($this->email->send()) 
       { 
        return TRUE; 
        //echo "Email is sent"; 
       }else 
       { 
        return FALSE; 
        //echo $this->email->print_debugger(); 
       } 

} 

} 
?> 

为复选框部分码给出

contact.php(视图)

<div class="form-group"> 
        <label class="col-md-4 control-label">Which ?</label> 
        <div class="col-md-4"> 


         <div class="checkbox"> 
          <label> 
           <input type="checkbox" name="checkbox_cat[]" class="get_value" value="option4" /> option4 
          </label> 

          <label> 
           <input type="checkbox" name="checkbox_cat[]" class="get_value" value="option5" /> option5 
          </label> 

          <label> 
           <input type="checkbox" name="checkbox_cat[]" class="get_value" value="option6" /> option6 
          </label> 

          <label> 
           <input type="checkbox" name="checkbox_cat[]" class="get_value" value="option7" /> option7 
          </label> 

         </div> 
</div> 

contact_model .php(m奥德尔)

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 


class contact_model extends CI_Model 
{ 

function __construct() 
{ 
    parent::__construct(); 
} 

function insert_into_db($data) 
{ 
    // Inserting into database table 
    $this->db->insert('sysops_tbl', $data); 
} 

} 

?> 
+1

你是不是在这 - $发送数据设计它>发送();使用$ this-> send($ data);因此数据将以$ value(发送函数的参数)的形式获取,您可以从$ value中获取数据并发送邮件内容 – hrishi

+0

我会尝试并更新您的数据。谢谢 – bhordupur

+0

@hrishi,根据你的提示,我尝试过,现在我的问题是,我如何注入这些值在电子邮件正文和正常的foreach循环似乎不起作用的控制器。我更新了代码。 – bhordupur

回答

1
public function send($value='') 
{ 
    //load the email library 
    $this->load->library('email'); 

    //Sending email 
    $config_email = array(
    'mailtype' => 'html', 
    'charset' =>'iso-8859-1', 
    'wordwrap' => TRUE 
    ); 

    $msg = $this->load->view('email_template',$value,true); 

    //override the config from text to html 
    $this->email->initialize($config_email); 

    //printing manager email and name 
    $this->email->from($this->input->post('email'),$this->input->post('m_name')); 
    //receipant email 

    $this->email->to('[email protected]'); 
    //header 
    //$this->email->set_header(json_encode(implode(",", $this->input->post('checkbox_cat'))),'binded'); 
    //email subject 
    $this->email->subject('We need user access for'); 
    $this->email->message($msg); 
    //print the message if email is sent 
    if($this->email->send()) 
    { 
     return TRUE; 
     //echo "Email is sent"; 
    }else 
    { 
     return FALSE; 
     //echo $this->email->print_debugger(); 
    } 

} 

email_template:当你需要

<html> 
    <div> 
     <label>Name</label> 
     <?php echo $f_name." ".$l_name; ?> 
    </div> 
    <div> 
     <label>Comment</label> 
     <?php echo $comment; ?> 
    </div> 
</html> 
+0

我使用了我的一个Gmail帐户,并且能够接收一次电子邮件。除了email_template.php文件以外,没有任何更改(添加更多的值,例如城市等等,它工作)。现在我不再收到任何电子邮件了,我在代码中没有任何错误,而且它有点奇怪。 – bhordupur

+0

现在似乎工作 – bhordupur

相关问题