I am creating contact us page in my website which is in CodeIgniter. I want to send this contact form data in my mail address.
How to send email in CodeIgniter without using SMTP for website?
yes you can use codeIgniter simple mail function.
Example:-
public function send_mail() {
$from_email = "your#example.com";
$to_email = $this->input->post('email');
//Load email library
$this->load->library('email');
$this->email->from($from_email, 'Your Name');
$this->email->to($to_email);
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
//Send mail
if($this->email->send())
$this->session->set_flashdata("email_sent","Email sent successfully.");
else
$this->session->set_flashdata("email_sent","Error in sending Email.");
$this->load->view('email_form');
}
You can send a email.
Controller.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Inquiry extends CI_Controller
{
public function inquiry()
{
parent::__construct();
/* Model */
}
public function index()
{
$this->load->view('header', array("title" => "Inquiry"));
$this->load->view('your_view',$view);
$this->load->view('footer');
}
function inviteStaff()
{
$this->load->library('email');
$from_email = $_POST['email'];
$to_email = "your#email.com";
$body='이름 : '.$_POST['name'].'<br /><br />email : '.$_POST['email'].'<br /><br />contents : '.$_POST['contents'].'';
$this->email->from($from_email, $_POST['name']);
$this->email->to($to_email);
$this->email->set_mailtype("html");
$this->email->subject('TITLE');
$this->email->message($body);
$this->email->send();
}
function emailCheck()
{
$user_id = base64_decode($_POST['user_id']);
$email = $_POST['check_mail'];
$where = array('user_emailId' => $email,'user_id!=' => $user_id);
$view = $this->mdl_file->sel_where('user_details',$where);
echo count($view);
}
}
view.
<form class="form-horizontal form-label-left" method="POST" action="<?php echo base_url();?>Inquiry/inviteStaff">
<div class="item form-group" id="email_div">
<label class="control-label col-md-3 col-sm-3 col-xs-3 right" for="name">이름<span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-6" >
<input type="text" id="name" name="name" required="required" class="form-control col-md-7 col-xs-12" placeholder="이름" onblur="return emailCheck();" value="<?php echo #$staff_details[0]['user_emailId'] ?>">
</div>
<div class="alert">Your Name.</div>
<div class="alert_1" style="position: absolute;margin-left: 735px;width: 160px;display: none;margin-top: 5px;color: red;">
</div>
</div>
<div class="item form-group" id="email_div">
<label class="control-label col-md-3 col-sm-3 col-xs-3 right" for="email">E-mail<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-6" >
<input type="text" id="email" name="email" required="required" class="form-control col-md-7 col-xs-12" placeholder="Email" onblur="return emailCheck();" value="<?php echo #$staff_details[0]['user_emailId'] ?>">
</div>
<div class="alert">Your Email.</div>
<div class="alert_1" style="position: absolute;margin-left: 735px;width: 160px;display: none;margin-top: 5px;color: red;">
</div>
</div>
<div class="item form-group" id="email_div">
<label class="control-label col-md-3 col-sm-3 col-xs-3 right" for="contents">내용<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-6" >
<textarea id="contents" name="contents" required="required" class="form-control col-md-7 col-xs-12" placeholder="내용" onblur="return emailCheck();" value="<?php echo #$staff_details[0]['user_emailId'] ?>" style="height:200px">
</textarea>
</div>
<div class="alert">Contents.</div>
<div class="alert_1" style="position: absolute;margin-left: 735px;width: 160px;display: none;margin-top: 5px;color: red;">
</div>
</div>
</form>
Related
Hi I am wondering if anyone could give me some insight into why my Codeigniter contact form just times out on submit?
I understand this is a beginner error but I feel like it is something I am completely overlooking. I have provided the controller and view.
I do have email.php setup in the Config file as well, with the various helpers and libraries loaded. Thank you in advance!
Controller
<?php
class contactform extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation', 'email'));
}
function index()
{
//set validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|callback_alpha_space_only');
$this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
$this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');
//run validation on form input
if ($this->form_validation->run() == FALSE) {
//validation fails
$this->load->view('contact_form_view');
} else {
//get the form data
$name = $this->input->post('name');
$from_email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
//set to_email id to receive emails
$to_email = 'littleliongirldesigns#gmail.com';
//configure email settings
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'email';
$config['smtp_pass'] = 'password';
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes
$this->load->library('email', $config);
$this->email->initialize($config);
//send mail
$this->email->from($from_email, $name);
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send()) {
// mail sent
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>');
redirect('contactform/index');
} else {
//error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">There is error in sending mail! Please try again later</div>');
redirect('contactform/index');
}
}
}
//custom validation function to accept only alphabets and space input
function alpha_space_only($str)
{
if (!preg_match("/^[a-zA-Z ]+$/",$str)) {
$this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
return FALSE;
} else {
return TRUE;
}
}
}
?>
View
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 well">
<?php $attributes = array("class" => "form-horizontal", "name" => "contactform");
echo form_open("contactform/index", $attributes);?>
<fieldset>
<legend>Contact Form</legend>
<div class="form-group">
<div class="col-md-12">
<label for="name" class="control-label">Name</label>
</div>
<div class="col-md-12">
<input class="form-control" name"name" placeholder="Your Name" type="text" value="<?php echo set_value('name'); ?>"/>
<span class="text-danger"><?php echo form_error('name'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="email" class="control-label">Email ID</label>
</div>
<div class="col-md-12">
<input class="form-control" name="email" placeholder="Your email" type="text" value="<?php echo set_value('email'); ?>" />
<span class="text-danger"><?php echo form_error('email');?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="subject" class="control-label">Subject</label>
</div>
<div class="col-md-12">
<input class="form-control" name="subject" placeholder="Subject" type="text" value="<?php echo set_value('subject'); ?>"/>
<span class="text-danger"><?php echo form_error('subject');?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="message" class="control-label">Message</label>
</div>
<div class="col-md-12">
<textarea class="form-control" name="message" rows="4" placeholder="Your Message"><?php echo set_value('message');?></textarea>
<span class="text-danger"><?php echo form_error('message'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input name="submit" type="submit" class="btn btn-primary" value="Send" />
</div>
</div>
</fieldset>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
</div>
</div>
</div>
NOTE: the mail function itself works fine, I have a hosted site with mail configured professionally. Mail is sent via this script, but my simple empty field handler never catches empty fields. I'm sure this is something super obvious, but I'm very new to php and the logic of my script seems sound. The code follows:
edit: I've left the form call within the php script in case someone finds issue with the bootstrap form itself. The redirect is temporary until I can get better form error handling going.
<body>
<!-- jQuery and bootstrap -->
<script src="https://code.jquery.com/jquery.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script src="../js/counter.js"></script>
<div class="jumbotron-fluid" id="primary">
<?php
if (isset($_POST['submit'])) {
$from = 'Web_Form_Contact';
$to = 'example#example.com';
$subject = 'Job_Contact';
$c_type = $_POST["c_type"];
if (empty($_POST["c_name"])) {
$errName = 'Info missing: Name';
} else {
$c_name = $_POST["c_name"];
}
if (empty($_POST["c_email"] )) {
$errEmail = 'Please enter a valid email address';
} else {
$c_email = $_POST["c_email"];
}
if (empty($_POST["co_name"])) {
$errCoName = 'Info missing: Company Name';
} else {
$co_name = $_POST["co_name"];
}
if (empty($_POST["j_desc"])) {
$errjDesc = 'Info missing: Job Description';
} else {
$j_desc = $_POST["j_desc"];
}
$body = "From: $c_name\n E-Mail: $c_email\n Type: $c_type\n CoName: $co_name\n Description\n $j_desc";
if (empty($errName && $errEmail && $errCoName && $errjDesc)) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch\n This page will redirect in 5 seconds.</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
} else {
echo "$errName\n $errEmail\n $errCoName\n $errjDesc";
}
}
echo "$result";
?>
<div class="container">
<form class="form-horizontal" role="form" method="post" action="php/info.php"
<div class="row">
<div class="col-xs-10 col-sm-6 col-md-6 col-lg-offset-2 col-md-offset-2">
<<h2>Contact Me! <small></small></h2>
<form>
<div class="form-group row">
<label for="clientEmail" class="col-sm-2 form-control-label">Email</label>
<div class="col-sm-10">
<input type="email" name="c_email"class="form-control" id="clientEmail" placeholder="Email" >
</div>
</div>
<div class="form-group row">
<label for="clientName" class="col-sm-2 form-control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="c_name" id="clientName" placeholder="Your Name" >
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">Contact Type</label>
<div class="col-sm-10">
<div class="radio">
<label>
<input type="radio" name="c_type" id="gridRadios1" value="recruiter" checked>
I am a recruiter, and do not work directly for the company hiring.
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="c_type" id="gridRadios2" value="employee">
I work for the company in question, and wish to discuss an open position.
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="c_type" id="gridRadios3" value="other" >
Other
</label>
</div>
</div>
</div>
<div class="form-group row">
<label for="coName" class="col-sm-2 form-control-label">Company Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="co_name" id="coName" placeholder="Company Name" >
</div>
</div>
<!-- Job Description Text Box -->
<div class="form-group row">
<label for="Job Description ">Job Description: (1100 character max!)</label>
<div class="col-sm-10 col-lg-offset-2 col-md-offset-2 col-xl-offset-2 col-sm-offset-2" >
<div class="form-group">
<textarea class="form-control" rows="5" name="j_desc" id="jobDesc" placeholder="Job Description" ></textarea>
<p class="counter">1100</p>
</div>
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" id="send" name="submit" value="Send"class="btn btn-secondary">Submit</button>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<!--redirect script -->
<script>
function redirect(page) {
setTimeout(function () {
window.location.href = page;
}, 5000);
}
//sends to homepage
redirect("../index.html");
</script>
You need to have an empty check for each variable:
if (empty($errName) && empty($errEmail) && empty($errCoName) && empty($errjDesc)) {
You can also set an email flag at the beginning to true, and if you hit any of the errors, you can set it to false. Then when you're ready to email, check to see if that email flag is still true.
I'm trying to send an email via HTML5 with bootstrap + php.
HTML:
<form name="frmContacto" class="form-horizontal" method="post" action="mail/contact_me.php">
<fieldset>
<legend class="text-center header">Formulario de contacto</legend>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-user bigicon"></i></span>
<div class="col-md-8">
<input id="fname" name="name" type="text" placeholder="Nombre" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-user bigicon"></i></span>
<div class="col-md-8">
<input id="lname" name="name" type="text" placeholder="Apellidos" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-envelope-o bigicon"></i></span>
<div class="col-md-8">
<input id="email" name="email" type="text" placeholder="Correo electrónico" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-phone-square bigicon"></i></span>
<div class="col-md-8">
<input id="phone" name="phone" type="text" placeholder="Teléfono" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-pencil-square-o bigicon"></i></span>
<div class="col-md-8">
<textarea class="form-control" id="message" name="message" placeholder="Introduce aquí tu mensaje." rows="7"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-12 text-center">
<a href="mailto:xxx#example.com?Subject=Hello%20again" target="_top">
<button type="submit" class="btn btn-primary btn-lg">Enviar</button>
</a>
</div>
</div>
</fieldset>
</form>
PHP:
<?php
// Check for empty fields
if (empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)
) {
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'xxx#gmail.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Formulario de contacto web: $name";
$email_body = "Ha recibido un nuevo mensaje desde la web.\n\n" . "Detalles:\n\Nombre: $name\n\nEmail: $email_address\n\Teléfono: $phone\n\Mensaje:\n$message";
$headers = "From: noreply#yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to, $email_subject, $email_body, $headers);
return true;
?>
I don't know if I have to import something in the HTML. It seems that the php mail() function is not being called, since I have a breakpoint in the function line and nothing happens when I click the "Submit" button. Also, the email is the same in $to php function and in the HTML "mailto".
Thanks a lot.
Check the following things-
1. In your HTML you have declared Name Line of form twice, which is not required.
2. You should have setting in your server/PHP configuration file configured to a mail Server. Without Mail server connected, you can't send mails
Have you validate your settings in php.ini (sendmail parameter) ?
You might read this if needed:
https://blog.codinghorror.com/so-youd-like-to-send-some-email-through-code/
The Code is below is well commented so there is no point writing much. Just read through the Comments. It is quite self-explanatory:
PHP
<?php
// GET ALL POSTED FIELD-VALUES AND ASSIGN THEM DEFAULT VALUES OF NULL (IF THEY ARE NOT YET SET...
$lastName = isset($_POST['last_name']) ? htmlspecialchars(trim($_POST['last_name'])) : null;
$firstName = isset($_POST['first_name']) ? htmlspecialchars(trim($_POST['first_name'])) : null;
$email = isset($_POST['email']) ? htmlspecialchars(trim($_POST['email'])) : null;
$phone = isset($_POST['phone']) ? htmlspecialchars(trim($_POST['phone'])) : null;
$message = isset($_POST['message']) ? htmlspecialchars(trim($_POST['message'])) : null;
// VALIDATE THE FIELDS: BUT FIRST CREATE AN ARRAY
// TO HOLD ERROR MESSAGES FOR FIELDS THAT FAILED THE VALIDATION.
$arrErrorBag = array();
$strErrorMessage = "";
// VALIDATE E-MAIL:
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$arrErrorBag["E-Mail"] = "The Email you entered in not valid.";
}
// VALIDATE FIRST NAME:
if(!preg_match('#(^[a-zA-z])?([\w\.\-\ ])*\w*$#i', $firstName)){
$arrErrorBag["First Name"] = "First Name Should not be Empty and should contain only alpha-numeric Characters.";
}
// VALIDATE LAST NAME:
if(!preg_match('#(^[a-zA-z])?([\w\.\-\ ])*\w*$#i', $lastName)){
$arrErrorBag["Last Name"] = "Last Name Should not be Empty and should contain only alpha-numeric Characters.";
}
// VALIDATE PHONE:
if(!preg_match('#^(\+\d{1,3})?\s?(\d{2,4})\s?(\d{2,4})\s?(\d{2,4})\s?(\d{2,4})$#', $phone)){
$arrErrorBag["Phone"] = "Telephone Number should be Numeric and Should not be Empty.";
}
// VALIDATE MESSAGE:
if(!preg_match('#([\w\.\-\d\ ])*\w*$#si', $name)){
$arrErrorBag["Message"] = "Message Should not be Empty and should contain only alpha-numeric Characters only.";
}
if(!empty($arrErrorBag)){
// WE HAVE SOME ERRORS SO WE BUILD A STRING MESSAGE BASED ON THE ERRORS WE HAVE GATHERED ABOVE...
$strErrorMessage .= "<h6 class='error-heading'>There are Errors in the Form Please, correct them to continue.</h6>";
foreach($arrErrorBag as $fieldName=>$errorMessage){
$strErrorMessage .= "<div class='error-block'><strong>{$fieldName}: </strong>";
$strErrorMessage .= "<span>{$errorMessage}: </span></div>";
}
}else{
// THE FORM IS CLEAN AND VALID SO WE SEND THE E-MAIL:
$name = $firstName . " " . $lastName;
$to = 'xxx#gmail.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Formulario de contacto web: $name";
$email_body = "Ha recibido un nuevo mensaje desde la web.\n\n" . "Detalles:\n\Nombre: {$name}\n\nEmail: {$email}\n\Teléfono: {$phone}\n\Mensaje:\n{$message}";
$headers = "From: noreply#yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: {$email}";
mail($to, $email_subject, $email_body, $headers);
// THE MAIL WAS SENT SO YOU CAN NOW REDIRECT TO ANOTHER PAGE... MAYBE A THANK YOU PAGE...
$redirectURL = "thank-you.php"; //<== THIS IS ONLY AN EXAMPLE... THE URL SHOULD RATHER BE ANY PAGE OF YOUR CHOOSING.
header("location: " . $redirectURL);
}
?>
HTML
<!-- BEFORE THE FORM, CREATE A DIV TO HOLD ERROR MESSAGE, IN CASE ERRORS OCCUR... -->
<!-- IN THIS SCENARIO, IT IS IDEAL TO HAVE BOTH THE PHP AND THE HTML INSIDE THE SAME FILE... -->
<!-- THE PHP CODE SHOLD BE AT THE TOP OF THE FILE & THE HTML MARKUP BELOW... -->
<!-- NOTICE THAT THE ACTION ATTRIBUTE OF THE FORM IS NOW EMPTY... THIS FORCES THE FORM TO SUBMIT BACK TO ITSELF: THIS PAGE... -->
<div class="error-msg-wrapper"><?php echo $strErrorMessage; ?></div>
<form name="frmContacto" class="form-horizontal" method="post" action="">
<fieldset>
<legend class="text-center header">Formulario de contacto</legend>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center">
<i class="fa fa-user bigicon"></i>
</span>
<div class="col-md-8">
<input id="fname" name="first_name" type="text" placeholder="Nombre" value="<?php echo $firstName; ?>" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center">
<i class="fa fa-user bigicon"></i>
</span>
<div class="col-md-8">
<input id="lname" name="last_name" type="text" placeholder="Apellidos" value="<?php echo $lastName; ?>" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center">
<i class="fa fa-envelope-o bigicon"></i>
</span>
<div class="col-md-8">
<input id="email" name="email" type="text" placeholder="Correo electrónico" value="<?php echo $email; ?>" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center">
<i class="fa fa-phone-square bigicon"></i>
</span>
<div class="col-md-8">
<input id="phone" name="phone" type="text" placeholder="Teléfono" value="<?php echo $phone; ?>" class="form-control">
</div>
</div>
<div class="form-group">
<span class="col-md-1 col-md-offset-2 text-center">
<i class="fa fa-pencil-square-o bigicon"></i>
</span>
<div class="col-md-8">
<textarea class="form-control" id="message" name="message" placeholder="Introduce aquí tu mensaje." rows="7"><?php echo $message; ?></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-12 text-center">
<input type="submit" class="btn btn-primary btn-lg" value="Enviar" />
</div>
</div>
</fieldset>
</form>
I have the Contact Me form that I can't get to send me an email. I am still a newbie but I don't see anything wrong. Help me out guys.
Here's the contactform controller
<?php
class contactform extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation', 'email'));
}
function index() {
//set validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|callback_alpha_space_only');
$this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
$this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');
//run validation on form input
if ($this->form_validation->run() == FALSE) {
//validation fails
//$this->load->view('contact_view');
$this->load->view('header_view');
$this->load->view('nav_view');
$this->load->view('contact_view');
$this->load->view('footer_view');
}
else {
//get the form data
$name = $this->input->post('name');
$from_email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
//set to_email id to which you want to receive mails
$to_email = 'mygmail#gmail.com';
//configure email settings
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'mygmail#gmail.com';
$config['smtp_pass'] = 'mypass';
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes
//$this->load->library('email', $config);
$this->email->initialize($config);
//send mail
$this->email->from($from_email, $name);
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send())
{
// mail sent
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>');
redirect('contactform/index');
}
else
{
//error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">There is error in sending mail! Please try again later</div>');
redirect('contactform/index');
}
}
}
//custom validation function to accept only alphabets and space input
function alpha_space_only($str) {
if (!preg_match("/^[a-zA-Z ]+$/",$str)) {
$this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
return FALSE;
}
else {
return TRUE;
}
}
}
?>
and here is my contact_view
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 well">
<?php $attributes = array("class" => "form-horizontal", "name" => "contactform");
echo form_open("contactform/index", $attributes);?>
<fieldset>
<legend>Contact Form</legend>
<div class="form-group">
<div class="col-md-12">
<label for="name" class="control-label">Name</label>
</div>
<div class="col-md-12">
<input class="form-control" name="name" placeholder="Your Full Name" type="text" value="<?php echo set_value('name'); ?>" />
<span class="text-danger"><?php echo form_error('name'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="email" class="control-label">Email ID</label>
</div>
<div class="col-md-12">
<input class="form-control" name="email" placeholder="Your Email ID" type="text" value="<?php echo set_value('email'); ?>" />
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="subject" class="control-label">Subject</label>
</div>
<div class="col-md-12">
<input class="form-control" name="subject" placeholder="Your Subject" type="text" value="<?php echo set_value('subject'); ?>" />
<span class="text-danger"><?php echo form_error('subject'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="message" class="control-label">Message</label>
</div>
<div class="col-md-12">
<textarea class="form-control" name="message" rows="4" placeholder="Your Message"><?php echo set_value('message'); ?></textarea>
<span class="text-danger"><?php echo form_error('message'); ?></span>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input name="submit" type="submit" class="btn btn-primary" value="Send" />
</div>
</div>
</fieldset>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
</div>
</div>
I already checked my codes. But the problem is, I can't see why it cant send. I even tried to upload it to my website, but it still won't let me send an email.
I am unable to identify the mistake
model (here I am matching the id to update)
function get_account_record($a_id)
{
$this->db->where('a_id', $a_id);
$this->db->from('account_info');
$query = $this->db->get();
return $query->result();
}
controller (receiving everything via post accept the ID i-e a_id )
function update($a_id)
{
$data['a_id'] = $a_id;
$data['view'] = $this->sf_model->get_account_record($a_id);
$this->form_validation->set_rules('a_name', 'Account Name', 'trim|required|xss_clean|callback_alpha_only_space');
$this->form_validation->set_rules('a_web', 'Website', 'trim|required|xss_clean');
form Validation
if ($this->form_validation->run() == FALSE)
{
$this->load->view('viewUpdate', $data);
}
else {
$data = array(
'a_name' => $this->input->post('a_name'),
'a_website' => $this->input->post('a_web'),
'a_billingStreet' => $this->input->post('a_billingStreet'),
'a_mobile' => $this->input->post('a_mobile'),);
$this->db->where('a_id', $a_id);
$this->db->update('account_info', $data);
display success message
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Successfully Updated!</div>');
//redirect('salesforce' . $a_id);
}
}
view (ID and Name)
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_id" class="control-label">Account ID</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_id" name="a_id" placeholder="" disabled="disabled" type="text" class="form-control" value="<?php echo $a_id;?>" />
<span class="text-danger"><?php echo form_error('a_id'); ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="a_name" class="control-label">Account Name</label>
</div>
<div class="col-lg-8 col-sm-8">
<input id="a_name" name="a_name" placeholder="Enter Account Name" type="text" class="form-control" value="<?php echo $view[0]->a_name; ?>" />
<span class="text-danger"><?php echo form_error('a_name'); ?></span>
</div>
</div>
</div>
If you don't want to allow the user to edit then add readonly instead of disabled attribute
<div class="col-lg-8 col-sm-8">
<input id="a_id" name="a_id" placeholder="" readonly="readonly" type="text" class="form-control" value="<?php echo $a_id;?>" />
<span class="text-danger"><?php echo form_error('a_id'); ?></span>
</div>
If you want to disable then add the hidden element
<input id="a1_id" name="a1_id" type="hidden" value="<?php echo $a_id;?>" />
Controller Function
function update($a_id)
{
//read the value using input library
$a_id = $this->input->post('a_id');
//$a1_id = $this->input->post('a1_id'); your hidden element value can be get here.
$data['a_id'] = $a_id;
$data['view'] = $this->sf_model->get_account_record($a_id);
$this->form_validation->set_rules('a_name', 'Account Name', 'trim|required|xss_clean|callback_alpha_only_space');
$this->form_validation->set_rules('a_web', 'Website', 'trim|required|xss_clean');
}
My problem Solved after editing the where clause in update query
$this->db->where('a_id', $_POST['a_id']);