gpt4 book ai didi

php - 使用 codeigniter 成功提交后将表单数据发送到电子邮件

转载 作者:行者123 更新时间:2023-11-29 10:44:53 25 4
gpt4 key购买 nike

我正在尝试制作一个简单的页面,用户将在其中请求他们想要的一些服务。通过验证后,我将表单数据保存到数据库中。当有人请求某些服务时,该程序还应该发送电子邮件通知。所以,在此之前我的代码可以正常工作。

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

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

Contact.php 文件( 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();
}

}

}
?>

对于复选框,给出了部分代码

contact.php( View )

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

}

?>

最佳答案

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>

关于php - 使用 codeigniter 成功提交后将表单数据发送到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44780889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com