Step 1: Load "email" class in controller.
$this->load->library('email');
?>
Step 2: Create a controller file
Next create a controller file named "Contact.php" inside "application/controllers" folder.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* @package Contact : CodeIgniter Contact Form
*
* @author TechArise Team
*
* @email info@techarise.com
*
* Description of Contact Controller
*/
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Contact extends CI_Controller {
public function __construct() {
parent::__construct();
// load email lib
$this->load->library('email');
}
// contact
public function index() {
$data['page'] = 'contact';
$data['title'] = 'Contact Form | TechArise';
$this->load->view('contact/index', $data);
}
// send information
public function send() {
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('email', 'Your Email', 'trim|required|valid_email');
$this->form_validation->set_rules('contact_no', 'Phone', 'trim|required');
$this->form_validation->set_rules('comment', 'Comments', 'trim|required');
if($this->form_validation->run() == FALSE) {
$this->index();
} else {
$name = $this->input->post('name');
$email = $this->input->post('email');
$contact_no = $this->input->post('contact_no');
$comment = $this->input->post('comment');
if(!empty($email)) {
// send mail
$config = array (
'mailtype' => 'html',
'charset' => 'utf-8',
'priority' => '1'
);
$message='';
$bodyMsg = ''.$comment.'
';
$delimeter = $name."
".$contact_no;
$dataMail = array('topMsg'=>'Hi Team', 'bodyMsg'=>$bodyMsg, 'thanksMsg'=>'Best regards,', 'delimeter'=> $delimeter);
$this->email->initialize($config);
$this->email->from($email, $name);
$this->email->to(TO_MAIL);
$this->email->subject('Contact Form');
$message = $this->load->view('mailTemplate/contactForm', $dataMail, TRUE);
$this->email->message($message);
$this->email->send();
// confirm mail
$bodyMsg = 'Thank you for contacting us.
';
$dataMail = array('topMsg'=>'Hi '.$name, 'bodyMsg'=>$bodyMsg, 'thanksMsg'=>'Best regards,', 'delimeter'=> 'Team TechArise');
$this->email->initialize($config);
$this->email->from(TO_MAIL, FROM_TEXT);
$this->email->to($email);
$this->email->subject('Contact Form Confimation');
$message = $this->load->view('mailTemplate/contactForm', $dataMail, TRUE);
$this->email->message($message);
$this->email->send();
}
$this->session->set_flashdata('msg', 'Thank you for your message. It has been sent.');
redirect('/');
}
}
}
?>
Step 3: Create a view
Create a view file named "index.php" inside "application/views/contact" folder
$this->load->view('templates/header');
?>
CodeIgniter Contact Form Example
session->flashdata('msg'))){ ?>
session->flashdata('msg'); ?>
$this->load->view('templates/footer');
?>
Step 4: Create a view(mailTemplate)
Create a view file named "contactForm.php" inside "application/views/mailTemplate" folder
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
?>
|
Demo [sociallocker] Download[/sociallocker]