CodeIgniter Forums
Good Email Form Tutorial or Code - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Good Email Form Tutorial or Code (/showthread.php?tid=18885)



Good Email Form Tutorial or Code - El Forum - 05-21-2009

[eluser]clintonbeattie[/eluser]
Hi,

I'm working my way through a form which will simply take details and email them to me.

I've done a search for Codeigniter forms but Godbit was the main one which is based on an older version of codeigniter.

Not like me to ask straight off the bat, but anyone have a link to a good modern tutorial or code that does validation etc. It's an emergency!

Many thanks.

C


Good Email Form Tutorial or Code - El Forum - 05-21-2009

[eluser]Cro_Crx[/eluser]
I havn't seen any tutorials for that sort of thing. The form validation has lots of examples on the library page here --> http://ellislab.com/codeigniter/user-guide/libraries/validation.html

After you've validated it just need to email it off which is really easy, you can find the info here --> http://ellislab.com/codeigniter/user-guide/libraries/email.html

You can put the posted variables in your email by using something like this:

Code:
$this->email->message('Some Field:'.$this->input->post('form_field_name'));



Good Email Form Tutorial or Code - El Forum - 05-22-2009

[eluser]got 2 doodle[/eluser]
@modelreject

As far as putting your mail message together I've found this technique useful
Code:
$message = $this->load->view('contents_email_cart',$data,true);

$data['user_data']= $this->userdata->get_user_data($this->user_id);
$order_email = $this->sitedata->get_site_key('order_email');
$this->email->to($order_email);
$this->email->cc($data['user_data']['email']);
$this->email->from($order_email);
$this->email->subject('Website order');
$this->email->message($message);
$this->email->send();

The first line sends the data to a view with the 'true' parameter set, in my case it's a confirmation of an online order.

Here's a chunk of the view code.
Code:
Website generated order.
<?php
$today = getdate();
$year = $today['year'];
$month = $today['month'];
$daynum = $today['mday'];
$dayname = $today['weekday'];
echo "This order was placed on $dayname, $month $daynum, $year\n";
$the_code = random_string('numeric',8)."\n";
echo "ID Number:$the_code\n\n";
//etc.

This allows an easy way to build a text message for emailing.

I know it doesn't directly answer your question and it could be considered a hack but it wasn't immediately obvious to me that you could do this in CI, now that I know you can it makes formatting the email real easy. I haven't tried it with html emails, but it should be no problem.

I thought this might save you some time.
doodle