0

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>
user8001297
  • 173
  • 1
  • 4
  • 18

1 Answers1

0

First parameter of from() must be email address. If you like add name, pass it as second parameter. Try following from():

$from_email = 'yourfrom@example.com'; //fill with valid email that authorized in your email host
$this->email->from($from_email, $name);

I can't find set_newline() in codeigniter documentation. Maybe worked if remove it!

Mohammad Hamedani
  • 3,304
  • 3
  • 10
  • 22
  • the person who is filling the contact form from his email the mail should go to the email mentioned in "To" – user8001297 May 31 '17 at 09:33
  • If you want to use gmail and your `smtp_user` is **YYY@gmail.com**, so your from field must be **YYY@gmail.com**, because gmail check it! `$this->email->from('YYY@gmail.com', 'YYY');`. Are you try it? – Mohammad Hamedani May 31 '17 at 09:40
  • i have the same format which i posted above for other section it is working fine but in contact us it is not working – user8001297 May 31 '17 at 09:58