Having a contact form but not able to send an email through contact form in codeigniter PHP.Here is my code.Not getting any error messages and not getting any emails if i am filing the form and sending the request through contact form then.tried with changing the code it is redirecting to failure condition displaying as "failure"
controller:
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
$this->form_validation->set_rules('fullname','First Name' , 'required');
$this->form_validation->set_rules('email','Email','required|valid_email');
$this->form_validation->set_rules('phone','Mobile Number','required|numeric');
$this->form_validation->set_rules('text','Description','required');
$this->form_validation->set_rules('subject','Description','required');
if($this->form_validation->run()== FALSE)
{
$data['mainpage']='contact';
$this->load->view('templates/template',$data);
}
else
{
//echo "hi";
//get the form data
$name = $this->input->post('fullname');
$from_email = $this->input->post('email');
$phone = $this->input->post('phone');
$description = $this->input->post('text');
$subject=$this->input->post('subject');
//set to_email id to which you want to receive mails
$to_email = 'email@gmail.com';
$config=Array(
'protocol'=> 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com', //smtp host name
'smtp_port' => '465', //smtp port number
'smtp_user' => 'gmail@gmail.com',
'smtp_pass' => 'PASSword1@3', //$from_email password
'mailtype' =>'html',
'newline' =>"\r\n",
'crlf' =>"\r\n",
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$message = array();
$message[] = 'Username : '.trim($name).' ';
$message[] = 'Phone Number : '.trim($phone).' ';
$message[] = 'Description : '.trim($description).' ';
$message = implode(PHP_EOL, $message);
//send mail
$this->load->library('email',$config);
$this->email->from($from_email);
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send())
{
echo "sent";
//$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>');
// redirect('welcome');
}
else
{
echo "failure";
}
}
View:
<div class ="contactus">
<div class="contactname">
<input type="text" class="form-control names" name="fullname" id="fullname" placeholder="FullName" >
<?php echo form_error('fullname', '<div class="error">', '</div>'); ?>
</div>
<div class="contactemail">
<input type="email" class="form-control emails" id="email" name="email" placeholder="Email">
<?php echo form_error('email', '<div class="error">', '</div>'); ?>
</div>
<div class="contactsubject">
<input type="text" class="form-control subjects" id="subject" name="subject" placeholder="Subject">
</div>
<div class="contactphone">
<input id="phone" type="tel">
<input type="text" class="form-control phones" name="phone" placeholder="Mobile">
</div>
<div class="contactmessage">
<textarea id="text" name="text" class="textarea" placeholder="Message"></textarea>
</div>
<div class="">
<div class="contactpagecaptchas">
<img src="/captcha.php?rand=<?php echo rand();?>" class="contactpagecaptchass" id='captchaimg'/>
<p class="change"><a href="javascript: refreshCaptcha();" class="clickto">click to change</a></p>
</div>
<div class="contactcaptcha">
<input type="text" class="form-control" name="captcha" placeholder="Captcha" style="background-color: #f4f4f4;border: none;">
</div>
</div>
<button type="submit" class="btn btn-success contact1">Submit</button>
</div>