[eluser]Référencement Google[/eluser]
I post you one that I use. There is maybe a better code to do here for sure, but at least it is working. I post it "as is" but you should make some adaption of the code because I am using a language file. If you come to a more nicer code, maybe should you post it on the wiki.
Controller:
Code:
<?php
require_once('main.php');
class Contact extends Main {
function Contact()
{
parent::Main();
$this->load->language('contactform');
}
function index()
{
$data['content'] = '';
/** ----------------------------------------------------
* Start contact form and validation
** ---------------------------------------------------- */
// Setup the html delimiter for errors
$this->validation->set_error_delimiters('<p class="form_error">', '</p>');
// Setup rules for the validation of the contact form
$rules = array( 'prenom' => 'trim|required|max_length[50]',
'nom' => 'trim|max_length[50]',
'telephone' => 'trim|max_length[25]',
'contactemail' => 'trim|required|valid_email|max_length[120]',
'message' => 'required');
$this->validation->set_rules($rules);
// Setup fiels to repopulate
$fields = array('prenom' => $this->lang->line('firstname'),
'nom' => $this->lang->line('surname'),
'telephone' => $this->lang->line('phone'),
'contactemail' => $this->lang->line('email'),
'message' => $this->lang->line('message'));
$this->validation->set_fields($fields);
if($this->validation->run())
{
// All ok, we send the email
if($this->_send_email())
{
redirect('contact/success');
}
else
{
$data['result_message'] = '<p class="form_error">Votre message n\'a pas pu être envoyé !<br >
Veuillez réessayer ou utiliser nos autres moyens de contacts.</p>';
$data['content'] = $this->load->view('contact_form', $data, true);
}
}
else
{
$data['content'] = $this->load->view('contact_form', '', true);
}
$this->load->view('contact', $data);
}
function _send_email()
{
$this->email->from($this->validation->value('contactemail'), $this->validation->value('nom').' '.$this->validation->value('prenom'));
$this->email->to($GLOBALS['site_email']);
$this->email->subject('Formulaire contact NIPX');
$data['toName'] = '';
$data['mailIntro'] = 'Un message vous a été envoyé depuis la page contact du site NIPX.';
// Prepare the message body
$data['message'] = "---------------- COORDONNEES ----------------\n";
$data['message'] .= "Nom: ".$this->validation->value('nom')."\n";
$data['message'] .= "Prénom: ".$this->validation->value('prenom')."\n";
$data['message'] .= "Téléphone: ".$this->validation->value('telephone')."\n";
$data['message'] .= "Email: {unwrap}".$this->validation->value('contactemail')."{/unwrap}\n\n";
$data['message'] .= "---------------- MESSAGE ----------------\n";
$data['message'] .= $this->validation->value('message')."\n";
$message = $this->load->view('emails/email_txt', $data, true);
$this->email->message($message);
if($this->email->send())
{
return true;
}
}
function success()
{
$data['content'] = '<p class="form_ok">Votre message a bien été envoyé !<br />
Une réponse vous parviendra prochainement.</p>';
$this->load->view('contact', $data);
}
}
?>