使用codeigniter成功提交后,将表单数据发送到电子邮件(Sending form data to an email after a successful submission using co

编程入门 行业动态 更新时间:2024-10-26 20:23:00
使用codeigniter成功提交后,将表单数据发送到电子邮件(Sending form data to an email after a successful submission using codeigniter)

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

但是,当表单成功提交时,我希望在电子邮件正文/消息中获取表单数据,但到目前为止我无法完成。 我用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('xxx.xx@xxx.com'); //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(型号)

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

I am new to PHP and trying to make a simple page where users will request for some services they would like to have. After passing the validation I save the form data into a database. This program should also send an email notification when someone ask for some services. So, Until this my code is working.

But I want to get the form data in the email body/message when a form is successfully submited but so far I could not manage to do it. I googled and tried some but did not work.

How can I do it using codeigniter? Could someone guide me how can I fix this? If you need full HTML code please let me know. I would appreciate your help.

Contact.php file (Controller)

<?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('xxx.xx@xxx.com'); //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(); } } } ?>

for check boxes partial code is given

contact.php (views)

<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 (model)

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

最满意答案

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('xxx.xx@xxx.com'); //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> 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('xxx.xx@xxx.com'); //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 : Design it as you required

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

更多推荐

本文发布于:2023-07-30 00:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1320808.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   表单   电子邮件   数据   提交后

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!