Welcome Guest, Not a member yet? Register   Sign In
Am i correctly sending email?
#1

[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();
            
                    }
  
  }

}
#2

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

[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....
#4

[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...ation.html

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

[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.
#6

[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.




Theme © iAndrew 2016 - Forum software by © MyBB