Hello,

How to make my email form function in CI ?

I have a contact form which consist of :

  1. Your Name
  2. Your Email
  3. Subject
  4. Messages

I have read: http://ellislab.com/codeigniter/user-guide/libraries/email.html

Yet, I still do not know in which file do I have to write the codes and how to?

Thanks before.

IF you are emailing the contents of a form. In your form handler:-

$this->load->library('email');

$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com'); 
$this->email->cc('another@another-example.com'); 
$this->email->bcc('them@their-example.com'); 

$this->email->subject($yourFormSubject);
$this->email->message($concatOfFormFieldsYouWantToSend);    

$this->email->send();

Should I write this in view or controller?

Controller. A view is just for displaying a web page, or partials.

controller/ccontactus.php

<?php

include 'page.php';

class ccontactus extends Page {

    public function __construct(){

        parent::__construct();

        $this->data['assetscont'] = array(
            'contactcss' => base_url().'assets/css/contact.css'         
        );

    }

    public function index(){

        $this->load->view('templates/navigation', $this->data['navigation']);           
        $this->load->view('contactus',$this->data['assetscont']);
        $this->load->view('templates/footer', $this->data['footer']);
    }

    public function contact(){

            $this->load->library('email');
            $this->email->from('your@example.com', 'Your Name');
            $this->email->to('someone@example.com');
            $this->email->cc('another@another-example.com');
            $this->email->bcc('them@their-example.com');
            $this->email->subject($yourFormSubject);
            $this->email->message($concatOfFormFieldsYouWantToSend);
            $this->email->send();
    }   
}

?>

views/contactus.php

<div id="carticle3">

<div id="inputID">
<div id="title">Your Message: </div><br><br>
<input type="text" name="name" placeholder="Your name" value=""><br>
<input type="text" name="email" placeholder="Your email" value=""><br>
<input type="text" name="subject" placeholder="subject" value=""><br>
<textarea rows="4" cols="20" placeholder="Message"></textarea><br><br>
<input type="submit" name="submit" value="kirim" class="button">
</div>

</div>

Hello,

Can anyone help me makes this code works?

There's no form in contactus.php

what do you mean? how to create the form in controllers/contactus.php?

where is your <form action=""> in contactus.php ?

What if I add this: <form name="contact" action="contactaction.php" method="POST"> under Your Message: ?

views/contactus.php

    <div id="carticle3">
    <div id="inputID">
    <div id="title">Your Message: </div><br><br>
    <form name="contact" action="contactaction.php" method="POST">
    <input type="text" name="name" placeholder="Your name" value=""><br>
    <input type="text" name="email" placeholder="Your email" value=""><br>
    <input type="text" name="subject" placeholder="subject" value=""><br>
    <textarea rows="4" cols="20" placeholder="Message"></textarea><br><br>
    <input type="submit" name="submit" value="kirim" class="button">
    </div>
    </div>

Then, what else should I do?

If I'm guessing correctly, this function: public function contact() has to accept paramaters.. Then you could do something like this:

<?php

  if(!isset($_POST['form_submitted']))
  {
       // output the form
  }else{

    $c = new ccontctus(); // constructor 
    $c->contact($_POST['uname'], blah, blah, blah);

Something like this?

A quick question davyg. What is the web address to your contact form.

e.g. www.mywebsite.com/contactus ?

Also,

Have you looked at the configurations for CI email?

Do you have an email server? either from you host or installed on localhost?

davy_yg,

Your HTML document is lacking structure. I think you should spend time looking at HTML primers. You page doesn't have a single paragraph tag.

Your webserver doesn't appear to be set up correctly. Your PHP code is not being interpreted.

Yet, I still do not know in which file do I have to write the codes and how to?

This is how your controller should look like:

$this->load->library('email');

$this->email->from('your@example.com', 'Your Name'); 
$this->email->to('someone@example.com'); 
$this->email->cc('another@another-example.com'); 
$this->email->bcc('them@their-example.com'); 

$this->email->subject('Email Test');
$this->email->message('Testing the email class.');  

if (!$this->email->send())
{
    $this->load->view('contact_view');
} else
{
    $this->load->view('mail_successfully_sent_view');
}

Also, I would warmly suggest you see these lessons and read user guide again.
I learnt from few good video series from YT and it helps much.
There is much more things should be corrected in your code. I suppose you don't have set suffix in config neither. However, pay attention on difference between CI versions 1.7.2 and 2.x.x. to make all tasks from video clips work.

This flow might help
---controller
1. controller :(loads the form)

--- view
2. form : you can start the form by this
"echo form_open('[redirect to contact function for sending email]')"
perhaps contact funtion accepts parameters like "email" for from,"subject" and "body" on submit

note be sure you loaded the form helper to initiate form_open function else you can do it manually like

"<form method='POST' action='yourhost/controller/function'>"

---controller
3. then you can follow what "Tpojka" as the content or your contact function.

This is what I have for the controller now:

controllers/ccontactus.php

class ccontactus extends Page {

public function contact(){

        $this->load->library('form_validation');

        $this->form_validation->set_rules('name', 'name', 'required');
        $this->form_validation->set_rules('email', 'email', 'required');
        $this->form_validation->set_rules('subject', 'subject', 'required');
        $this->form_validation->set_rules('message', 'message', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('contactus');
        }
        else
        {
            $this->load->view('contactus');
        }


        $this->load->helper(array('form', 'url'));

        $this->load->library('email');
        $this->email->from('your@example.com', 'Your Name');
        $this->email->to('someone@example.com');
        $this->email->cc('another@another-example.com');
        $this->email->bcc('them@their-example.com');
        $this->email->subject('Email Test');
        $this->email->message('Testing the email class.');
        if (!$this->email->send())
            {
            $this->load->view('contactus');
            } else
            {
            $this->load->view('contactus_emailsent');
            }   
    }

}

views/contactus.php

<?php echo form_open('contact', array('id'=>'contactform')); ?>

<div id="title">Your Message: </div><br><br>

<form name="contact" action="contactaction.php" method="POST">
<input type="text" name="name" placeholder="Your name" value=""><br>
<input type="text" name="email" placeholder="Your email" value=""><br>
<input type="text" name="subject" placeholder="subject" value=""><br>
<textarea rows="4" cols="20" placeholder="Message"></textarea><br><br>
<input type="submit" name="submit" value="kirim" class="button">
</div>

</div>

How to make the form works by sending email to the intended receipient when I press the submit button?

This form does not works yet.

Fatal error: Call to undefined function form_open() in C:\xampp\htdocs\IndonusaCI\application\views\contactus.php on line 70

line 70: <?php echo form_open('contact', array('id'=>'contactform')); ?>

try to extend your class to CI_controller

ok, I did change somethings. The same error still appears:

Fatal error: Call to undefined function form_open() in C:\xampp\htdocs\IndonusaCI\application\views\contactus.php on line 70

Is my line 70 code correct?

line 70: <?php echo form_open('contact', array('id'=>'contactform')); ?>

Autoload form helper in autoload.php file.

ok, I did autoload helper:

autoload.php

$autoload['helper'] = array('url', 'form');

I still have error:

A PHP Error was encountered

Severity: Notice

Message: Undefined index: name

Filename: helpers/form_helper.php

Line Number: 264

That error you should solve with adding name parameter to your textarea [em]dataMessage[/em] array.
It would be good to add name parameter to every input field.

Hello,

The following codes produce correct views. It just don't function correctly. If I press the kirim / submit button assuming that I do not fill in the required fields, no error appears.

What should I do so that the form function correctly, by showing errors whenever I do not fill in the required field and also sent the messages to the intended address if all the required field have been filled and the kirim / submit button has been pressed.

ccontrollers/contactus.php

<?php

include 'page.php';

class Ccontactus extends Page {

    public function __construct(){

        parent::__construct();

        $this->data['assetscont'] = array(
            'contactcss' => base_url().'assets/css/contact.css'         
        );

    }

    public function index(){

        $this->load->view('templates/navigation', $this->data['navigation']);           
        $this->load->view('contactus',$this->data['assetscont']);
        $this->load->view('templates/footer', $this->data['footer']);
    }

    public function contact(){

             $this->load->library('form_validation');
             $this->form_validation->set_rules('name', 'name', 'required');
             $this->form_validation->set_rules('email', 'email', 'required');
             $this->form_validation->set_rules('subject', 'subject', 'required');
             $this->form_validation->set_rules('message', 'message', 'required');

             if ($this->form_validation->run() == FALSE)
                {
                $this->load->view('contactus');
                }
             else
                {



             $this->load->library('email');
             $this->email->from('davy_yg@yahoo.com', 'Your Name');
             $this->email->to('davy_yg@yahoo.com');
             $this->email->cc('another@another-example.com');
             $this->email->bcc('them@their-example.com');
             $this->email->subject('Email Test');
             $this->email->message('Testing the email class.');

             if (!$this->email->send())
                {
                $this->load->view('contactus');
                } else
                {
                $this->load->view('contactus_emailsent');
                } 

                }
             }  

}

?>

views/contactus.php

<div id="title">Your Message: </div><br><br>

<?php echo form_open('ccontactus/contact', array('id'=>'contactform')); 

      $dataName = array(
              'name'        => 'name',
              'id'          => 'name',
              'value'       => '',
              'maxlength'   => '50',
              'size'        => '20',
              'style'       => 'width:32%',
              'placeholder' => 'Your name'
            );

      echo form_input($dataName); ?><br>
<?php      

      $dataEmail = array(
              'name'        => 'email',
              'id'          => 'email',
              'value'       => '',
              'maxlength'   => '50',
              'size'        => '20',
              'style'       => 'width:32%',
              'placeholder' => 'Your email'
            );

      echo form_input($dataEmail); ?><br>
<?php      

      $dataSubject = array(
              'name'        => 'subject',
              'id'          => 'subject',
              'value'       => '',
              'maxlength'   => '50',
              'size'        => '20',
              'placeholder' => 'Your subject'
            );

      echo form_input($dataSubject);

?><br>
<?php      

      $dataMessage = array(
              'name'        => 'message',
              'id'          => 'message',
              'value'       => '',
              'cols'        => '40',
              'rows'        => '4',
              'style'       => 'width:37%',
              'placeholder' => 'message'
            );

      echo form_textarea($dataMessage);

?><br>
<?php      

      echo form_submit(array('class' => 'button', 'name' => 'kirim'), 'kirim');

?><br>

</div>

<?php form_submit() ?>

</div>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.