CodeIgniter Forums
Am i correctly sending email? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Am i correctly sending email? (/showthread.php?tid=49599)



Am i correctly sending email? - El Forum - 02-26-2012

[eluser]Uplift[/eluser]
I am rather new to CI and have built this contact form, i'm just wondering if i have done it correctly, all 'seems' to work ok, so this might be a pointless post, but i would be greatful if you guys would check over my code before i use it in production.. it is for my business website so it needs to work in all cases.

The idea is that the form and handling and view is done in 1 controller

Code:
public function index() {
  
  $this->load->library('form_validation');
  $this->load->helper(array('form', 'url'));
  
  $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
  $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');
  $this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');
  
  $this->form_validation->set_rules('phone', 'Phone', 'trim|alpha_numeric|xss_clean');
  $this->form_validation->set_rules('company', 'Company', 'trim|xss_clean');
  
  $this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
  
  if ($this->form_validation->run() == FALSE) {
  
   $data['mail_sent'] = false;
  
   $this->template->write_view('contents', 'contact', $data);
              $this->template->render();
    
  } else {
  
   $data['mail_sent'] = false;

   $this->load->library('email');
   $config['mailtype'] = "html";
   $this->email->initialize($config);
  
   $name = $this->input->post('name');
   $email = $this->input->post('email');
   $phone = $this->input->post('phone');
   $company = $this->input->post('company');
   $subject = $this->input->post('subject-hidden');
   $budget = $this->input->post('budget-hidden');
   $message = $this->input->post('message');
  
   $body = "Name: ".$name."<br /><br />Email: ".$email."<br /><br />Phone: ".$phone."<br /><br />Company: ".$company."<br /><br />Subject: ".$subject."<br /><br />Budget: ".$budget."<br /><br />Message:<br />".$message;
  
   $this->email->from($email, $name);
   $this->email->to('[email protected]');
   $this->email->subject($name.' has emailed you');
   $this->email->message($body);
  
   if ($this->email->send()) {

                              $data['mail_sent'] = true;
                
                              $this->template->write_view('contents', 'contact', $data);
             $this->template->render();
            
                    }
  
  }

}



Am i correctly sending email? - El Forum - 02-26-2012

[eluser]CroNiX[/eluser]
If you are sending as HTML, it should have a head, body, etc, just like a regular html page.


Am i correctly sending email? - El Forum - 02-27-2012

[eluser]aquary[/eluser]
You'll never know if it's going to work on the real server. A test is required there, unless you use the server's real SMTP information. Most of the time in my previous projects, sending mail without using a real SMTP never works on the production's....


Am i correctly sending email? - El Forum - 02-27-2012

[eluser]Chathuranga Tennakoon[/eluser]
please refer belo post on my blog where i have successfully done an example on sending email using Codeigniter with gmail.
http://chathurangat.blogspot.com/2011/11/codeigniter-mcv-based-user-registration.html

(here i have done a user registration example. once the user is successfully registered, he is notified vi an email)



Am i correctly sending email? - El Forum - 02-27-2012

[eluser]Uplift[/eluser]
[quote author="CroNiX" date="1330294290"]If you are sending as HTML, it should have a head, body, etc, just like a regular html page.[/quote]

So when an mail is sent as HTML the actual body is like a complete html page?

&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;

.. did not know that.


Am i correctly sending email? - El Forum - 02-27-2012

[eluser]CroNiX[/eluser]
Yes, the main difference is in html email, any images need to be full paths to the server (http://yoursite.com/images/image.jpg), you can't use external stylesheets (styles need to be in-line) and javascript should be avoided (email reader most likely will ignore it, or flag it as spam). Some email readers won't read the html email unless its properly formatted html, or it might display it incorrectly.