Reputation: 167
I'm using the codeigniter php framework to build a standard website including a contact form, the user of the webpage should be able to enter basic contact form information.
I've set about and created the view file for the contact form and created a corresponding controller - this includes validation using the codeigniter validation library and then a private function to send the email to the webmaster regarding the enquiry etc.
I'm having issues getting past the form validation scenario, yet when submitting the form, I know for a fact that the data input should pass the validation tests and then pass the data to a private function to send away the email.
Below are my files laid bare, if anyone could spot a problem, and promtly put me in the correct direction I'd be most greatful!
<form method="POST" action="/contact/contactvalidate" name="contactform" id="contactform">
<?php echo validation_errors(); ?>
<label>Full Name:</label><br />
<?php echo form_error('fullname'); ?>
<input type="text" name="fullname" id="fullname" maxlength="100" size="50" />
<br />
<label>Email:</label><br />
<?php echo form_error('email'); ?>
<input type="text" name="email" id="email" maxlength="100" size="50" />
<br />
<label>Telephone:</label> <br />
<?php echo form_error('telephone'); ?>
<input type="text" name="telephone" id="telephone" maxlength="100" size="50" />
<br />
<label>What is your enquiry regarding</label> <br />
<?php echo form_error('regarding'); ?>
<select name="regarding" id="regarding">
<option value="General Enquiry">General Enquiry</option>
<option value="HR Consultancy Service">HR Consultancy Service</option>
<option value="Business Startup Service">Business Startup Service</option>
<option value="Solutions for Individuals">Solutions for Individuals</option>
<option value="Other">Other</option>
</select>
<br />
<label>Your Enquiry</label> <br />
<?php echo form_error('enquiry'); ?>
<textarea name="enquiry" id="enquiry"></textarea>
<br />
<label>What is 4 + 1?</label> <br />
<?php echo form_error('robot'); ?>
<input type="text" name="robot" id="robot" />
<br />
<input type="submit" value="Submit Post" class="button" />
</form>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Contact extends CI_Controller {
public function index()
{
$this->load->view('general/view_header');
$this->load->view('page/view_contact');
$this->load->view('general/view_footer');
}
public function contactvalidate()
{
$this->load->library('form_validation');
if ($this->input->post('form') == 'contactform'){
//Set the validation rules based on the page
$this->form_validation->set_rules('fullname', 'Name', 'required|max_length[50]|xss_clean|prep_for_form');
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|max_length[255]|xss_clean|valid_email|prep_for_form');
$this->form_validation->set_rules('telephone', 'Telephone', 'required|max_length[20]|xss_clean|prep_for_form');
$this->form_validation->set_rules('regarding', 'Regarding');
$this->form_validation->set_rules('enquiry', 'Enquiry', 'required|max_length[800]|xss_clean|prep_for_form');
$this->form_validation->set_rules('robot', 'Sum', 'required');
}
if ($this->form_validation->run() === true)
{
//Send the email
if($this->sendemail($_POST))
{
//If successful load the appropriate view
redirect('/thank-you');
}
}
else{
//If page exists load all necessary views
$this->load->view('general/view_header');
$this->load->view('page/view_contact');
$this->load->view('general/view_footer');
}
}
private function sendemail($content)
{
//Load the email library
$this->load->library('email');
//Initialise the email helper and set the "from"
$this->email->initialize(array("mailtype" => "html"));
$this->email->from("[email protected]", "Lesley Nowell HR Consultancy");
//Set the recipient, subject and message based on the page
//$this->email->to('[email protected]');
$this->email->to('[email protected]');
$this->email->subject('Website Enquirie');
$this->email->message("My name is: {$content["fullname"]}<br /><br />My email address is: {$content["email"]}<br /><br />My telephone number is: {$content["telephone"]}<br /><br />The enquiry is regarding: {$content["regarding"]}<br /><br />Enquiry: {$content["enquiry"]}");
//If the email is sent
if($this->email->send())
{
return true;
}
else
{
return false;
}
}
}
Hopfully that's enough information to see what it is I'm doing, I believe what I've been doing to be correct, I'm not exactly sure where the fall out is to be honest.
Upvotes: 0
Views: 18755
Reputation: 538
Here some of my codes of sending email....i just add this in controller
Code:
function send_email($id){
$this->emailformat($id,"10"); //i make another function that this will be call in view
}
function emailformat($c_id,$days){
$config['protocol'] = 'smtp'; // mail, sendmail, or smtp The mail sending protocol.
$config['smtp_host'] = '192.168.0.1'; // SMTP Server Address.
$config['smtp_user'] = '[email protected]'; // SMTP Username.
$config['smtp_pass'] = '12345'; // SMTP Password.
$config['smtp_port'] = '25'; // SMTP Port. 25 is for local host
$config['smtp_timeout'] = '5'; // SMTP Timeout (in seconds).
$config['wordwrap'] = TRUE; // TRUE or FALSE (boolean) Enable word-wrap.
$config['wrapchars'] = 76; // Character count to wrap at.
$config['mailtype'] = 'html'; // text or html Type of mail. If you send HTML email you must send it as a complete web page. Make sure you don't have any relative links or relative image paths otherwise they will not work.
$config['charset'] = 'utf-8'; // Character set (utf-8, iso-8859-1, etc.).
$config['validate'] = FALSE; // TRUE or FALSE (boolean) Whether to validate the email address.
$config['priority'] = 3; // 1, 2, 3, 4, 5 Email Priority. 1 = highest. 5 = lowest. 3 = normal.
$config['crlf'] = "\r\n"; // "\r\n" or "\n" or "\r" Newline character. (Use "\r\n" to comply with RFC 822).
$config['newline'] = "\r\n"; // "\r\n" or "\n" or "\r" Newline character. (Use "\r\n" to comply with RFC 822).
$config['bcc_batch_mode'] = FALSE; // TRUE or FALSE (boolean) Enable BCC Batch Mode.
$config['bcc_batch_size'] = 200; // Number of emails in each BCC batch.
$this->load->library('email');
$this->email->initialize($config);
$this->email->from('[email protected]', 'Robot');
$this->email->to('[email protected]');
$this->email->subject('Expiration Notification of '.$c_id);
$this->email->message('<html><body>This Message is to notify you that '.$c_id.' contract will expire in '.$days.' !</body></html>');
$this->email->send();
}
Upvotes: 0
Reputation: 167
It appears sadly, I should of held off making this post, as I eventually fixed and answered my own problem.
The issue was inside the contact controller
if ($this->input->post('form') == 'contactform')
if ($this->input->post()){}
It turns out after some more research if you leave the post() function empty, it selects all posted items from the contact form, before I had just simply created my own problem.
Upvotes: 1
Reputation: 2348
For sending mail you can try like this
<?Php
$this->load->library('email', $config);
$this->email->initialize($config);
$this->email->from('[email protected]', 'Name');
$message_body='Message Content';
$email = '[email protected]';
$this->email->to($email);
$this->email->subject('Your Subjec');
$this->email->message($message_body);
$suc=$this->email->send();
if($suc)
{
return true;
}
else
{
return false;
}
?>
Upvotes: 1