Build a Contact Form in CodeIgniter with Email
Contact form is an easy way to communicate with the website owner and admin. It is a standard web page on a website used to allow the visitor can submit their questions, feedback, and suggestions to the site administrator.
In this tutorial, we will learn you how to create a Contact Form in CodeIgniter with Email. We will cover this tutorial in easy steps with a live demo to create a Contact Form in CodeIgniter.
Before started to implement the Contact Form in CodeIgniter, look files structure:
- build-contact-form-in-codeIgniter-with-email
- application
- config
- autoload.php
- constants.php
- config.php
- routes.php
- controllers
- Contact.php
- views
- contact
- index.php
- mailTemplate
- contactUsForm.php
- templates
- header.php
- footer.php
- contact
- config
- system
- index.php
- assets
- css
- style.css
- application
Load CodeIgniter’s Email Class
// Load the email library
$this->load->library('email');
Step 1: Create a controller
Create a controller file named
Contact.php inside “application/controllers” folder.The Contact controller handles the contact form submission process. Return success or failure
load->view('contact/index', $data);
}
// send information
public function sendEmail() {
// Load the email library
$this->load->library('email');
// Load the validation library
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'required');
$this->form_validation->set_rules('message', 'Message', 'required');
// check form validation
if($this->form_validation->run() == TRUE) {
$name = $this->input->post('name');
$email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
if(!empty($email)) {
// send mail
$config = array (
'mailtype' => 'html',
'charset' => 'utf-8',
'priority' => '1'
);
$fromMail = 'info@webhaunt.com';
$fromName = 'WebHaunt';
$message='';
$bodyMessage = ''.$message.'
';
$delimeter = $name."
";
$mailData = array('topMsg'=>'Hi Team', 'bodyMessage'=>$bodyMessage, 'thanksMsg'=>'Best regards,', 'delimeter'=> $delimeter);
$this->email->initialize($config);
$this->email->from($email, $name);
$this->email->to($fromMail);
$this->email->subject($subject);
$message = $this->load->view('mailTemplate/contactUsForm', $mailData, TRUE);
$this->email->message($message);
$this->email->send();
// confirmation function
$this->confirmationEmail($email, $fromMail, $fromName);
}
$this->session->set_flashdata('success_msg', 'Thank you for contacting us.');
redirect('/');
} else {
$this->index();
}
}
// confirmationEmail
public function confirmationEmail() {
// confirmation mail
$bodyMessage = 'Thank you for contacting us – we will get back to you soon!
';
$mailData = array('topMsg'=>'Dear '.$name, 'bodyMessage'=>$bodyMessage, 'thanksMsg'=>'Regards,', 'delimeter'=> 'Webhaunt Team');
$this->email->initialize($config);
$this->email->from($fromMail, $fromName);
$this->email->to($email);
$this->email->subject('Thank you for contacting us');
$message = $this->load->view('mailTemplate/contactForm', $mailData, TRUE);
$this->email->message($message);
$this->email->send();
}
}
?>
Step 2: Create a view
Create a view file named
index.php inside “application/views/contact” folder.This file contains contact form HTML that allows the user to submit a contact request.
load->view('templates/header');
?>
Build a Contact Form in CodeIgniter with Email
session->flashdata('success_msg'))){ ?>
session->flashdata('success_msg'); ?>
load->view('templates/footer');
?>
Step 3: Create a mailtemplate view
Create a view file named
contactUsForm.php inside “application/views/mailTemplate” folder.
|
Create
header.php and footer.php section of the webpage. The Bootstrap and jQuery library is used to provide a better UI, so, include it in the header and footer section.header.php
Compress Image Before Upload using PHP | Web Haunt
footer.php