Welcome Guest, Not a member yet? Register   Sign In
Formmail
#1

[eluser]europe72[/eluser]
Is there anything built into the CI framework similar to the age-old formmail.pl script? Meaning, what is the simplest method for posting a form and having its contents e-mailed?

Like many things in PHP, this is not difficult to script w/o CI, but if there is something that makes this even easier I would like to know.

Thanks.
#2

[eluser]Derek Allard[/eluser]
You're on track in your last sentence. To do this I've always built the forum (you can use the form helper or build it yourself) and then use the email class to send stuff out.

On darkhorse.to I do it like this

Code:
function index()
    {
        $this->load->library('validation');
        $rules['name'] = "trim|required|max_length[50]";
        $rules['email'] = "trim|required|valid_email|max_length[50]";
        $rules['message'] = "trim|required|htmlentities|max_length[700]";
        
        $this->validation->set_rules($rules);
        
        $fields['name'] = 'Name';
        $fields['email'] = 'Email';
        $fields['message'] = 'Message';
    
        $this->validation->set_fields($fields);
        $this->validation->set_error_delimiters('<p class="error">', '</p>');

        if ($this->validation->run() == FALSE) {
            $data['pageTitle'] = 'Contact';
            $this->load->view('contact/index', $data);
        } else {
            $this->load->library('email');

            $name = $this->input->post('name');
            $email = $this->input->post('email');
            $message = $this->input->post('message');

            $this->email->from($email, $name);
            $this->email->to('[email protected]');
            $this->email->subject('Darkhorse contact form');
            $this->email->message($message . "\n\n-------------------\nIP address: " . $this->input->ip_address() . "\nServer name: " . gethostbyaddr($this->input->ip_address()));
            
            if ($this->email->send()) {
                $data['pageTitle'] = 'Contact';
                $this->load->view('contact/success', $data);
            } else {
                $data['pageTitle'] = 'Contact';
                $this->load->view('contact/failure', $data);
            }
        }    
    }

And here is the view
Code:
&lt;?php
    $this->load->view("header");
?&gt;
<h2 class="page_topic">Contact</h2>
<p>I’m Derek, and I build and program rock-solid web applications.  If you’d like to discuss work, please feel free to contact me.  Also, feel free to drop me a line if you just want to chat about web development. I’m good like that.</p>
<p><img src="&lt;?= base_url();?&gt;img/email.png" width="16" height="16" alt="email" /> <span id="myemail">derek (at) darkhorse (dot) to </span></p>
&lt;form id="contact_form" action="&lt;?= site_url('contact');?&gt;" method="post"&gt;
&lt;?=$this->validation->error_string; ?&gt;
    <p><label for="name">Name</label>
    &lt;input type="text" name="name" id="name" size="40" class="required" value="&lt;?php if ($this-&gt;validation->name) {echo $this->validation->name;}?&gt;" />
    </p>
    <p><label for="email">Email</label>
    &lt;input type="text" name="email" id="email" size="40" class="required" value="&lt;?php if ($this-&gt;validation->email) {echo $this->validation->email;}?&gt;" />
    </p>
    <p><label for="message">Message</label><br />
    &lt;textarea name="message" id="message" rows="5" cols="40"&gt;&lt;?php if ($this->validation->message) {echo $this->validation->message;}?&gt;&lt;/textarea&gt;
    </p>
    <p>&lt;input type="submit" value="send message" /&gt;</p>
&lt;/form&gt;
&lt;?php
    $this->load->view('footer');
?&gt;

I've actually been wanting to make some additions... I'll do a blog entry about it in the near future if you want.
#3

[eluser]ricklee[/eluser]
Derek,
Thanks for the information. It's very helpful.

I just tried this today though and I'm wondering how you go about setting authentication information for sending the email.

I get this error (running on XAMPP):
Code:
A PHP Error was encountered

Severity: Warning

Message: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

Filename: libraries/Email.php

Line Number: 1328

And I dont' think you can put authentication information in php.ini, so how do you do this in CI?

Thanks for any help!

Rick
#4

[eluser]Derek Allard[/eluser]
You can set config options for email right before you send it.

Code:
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp1.mailserver.ca';
$config['smtp_user'] = 'username';
$config['smtp_pass'] = 'password';
$config['smtp_port'] = '25';
$this->email->initialize($config);

Another trick that I like to use is to create a file in application/config called "Email.php" and include it in there. Then it gets autoloaded, and no need for inititalize()?

I'm writing up a tutorial about this - I'll try to remember to put it back here, but it'll be on my blog if you want to watch that.
#5

[eluser]ricklee[/eluser]
That took care of it. Thanks!

I'm still working on this form though and I have this other question. How do you get data from checkboxes?

I have this for example:
Code:
&lt;input type="checkbox" name="fruit[]" value ="apples" /&gt; Apples

&lt;input type="checkbox" name="fruit[]" value="oranges" /&gt; Oranges

But when I do this:
$this->input->post('fruit');

It gives me a string (returning the last item selected) instead of an array. What am I doing wrong?

Thanks for any tips!




Theme © iAndrew 2016 - Forum software by © MyBB