Shopping cart v1.1 Part 15: contact page and sending email message

Contact page allows visitors to send a message through a form. It has a reCaptcha in order to avoid spam. I will cover the contact page and sending email message to an administrator in this article.

Download Page

The contact page uses a recaptcha which is built in the BackendPro.

recaptcha.php

Please open modules/recaptcha/config/recaptcha.php.
This folder comes with the BackendPro.

// other codes here

$config['recaptcha'] = array(
    'public'=>'YOUR PUBLIC KEY HERE',
    'private'=>'YOUR PRIVATE KEY HERE',
    'RECAPTCHA_API_SERVER' =>'http://api.recaptcha.net',
    'RECAPTCHA_API_SECURE_SERVER'=>'https://api-secure.recaptcha.net',
    'RECAPTCHA_VERIFY_SERVER' =>'api-verify.recaptcha.net',
    'theme' => 'white'
); 

// and more here.

You need to register at http://recaptcha.net/ and obtain public and private key. You can use these code for your local development as well.
You need to replace ‘public’=>’YOUR PUBLIC KEY HERE’ and ‘private’=>’YOUR PRIVATE KEY HERE’ with your keys.

Function contact

We add the following in modules/webshop/controllers/webshop.php

function contact(){

		$data['title'] = $this->lang->line('webshop_shop_name')." | "."Contact us";
		$data['cap_img'] = $this->_generate_captcha();
		$data['page'] = $this->config->item('backendpro_template_shop') . 'contact';
		$data['module'] = $this->lang->line('webshop_folder');
		$this->load->view($this->_container,$data);
  	}

	function _generate_captcha(){
		$this->bep_assets->load_asset('recaptcha');
		$this->load->module_library('recaptcha','Recaptcha');
		return $this->recaptcha->recaptcha_get_html();
	}

function contact is nothing special, except calling function _generate_captcha function.

function _generate_captcha load module library ‘recaptcha’ which comes with BackendPro and generates captcha automatically.

We will use this function _generate_captcha in registration page as well.

Contact view

<div id="content">
	<div id="omossleft">
		<div id="logo">
		</div>
		<div class="contentleft">
	<?php print displayStatus();?>
			<?php
			if ($this->session->flashdata('subscribe_msg')){
				echo "<div class='status_box'>";
				echo $this->session->flashdata('subscribe_msg');
				echo "</div>";
			}
			?>

			<div id="contactform">
				<?php echo form_open( $this->lang->line('webshop_folder')."/message"); ?>
				<?php echo form_fieldset('<h3>Send Message</h3>'); ?>

				<p id="name">
					<label for="name">*<?php echo $this->lang->line('orders_name'); ?>: </label>
					<input type="text" name="name" id="name" value="<?php echo set_value('name'); ?>" maxlength="30" size="30"  />

				</p>

				<p id="email">
					<label for="email">*<?php echo $this->lang->line('orders_email'); ?>: </label>
					<input type="text" name="email" id="email" value="<?php echo set_value('email'); ?>" maxlength="30" size="30"  />

				</p>

				<p>
					<label for="message">*<?php echo $this->lang->line('contact_your_message'); ?>: </label><br />
					<?php
					  $data = array(
				      'name'  => 'message',
				      'id'    => 'message',
				      'rows'  => '10',
				      'cols'  => '3',
				      'style' => 'width:60%',
				    );	

				echo form_textarea($data);
				?>
				</p>
				<p>*<?php echo $this->lang->line('contact_captcha'); ?></p><br />
				<?php echo "<p>$cap_img</p>" ;?>
				<div id="contactsubmit"><input type="submit" value="<?php echo $this->lang->line('contact_send'); ?>" />
				</div>
				<?php echo form_fieldset_close(); ?>
				<?php echo form_close(); ?>
			</div>
		</div>
	</div>
</div>

As you can see the form will be sent to the message function.

form_open( $this->lang->line('webshop_folder')."/message")

So obviously the next one is function message.

Controller: function message

As you can see in the comments, the BackendPro is using Codeigniter’s validation library instead of Form validation library. The validation library is deprecated and the author mentioned that he is going to update to CI’s Form validation library in the next version.

Load the validation library in the constructor, define rules for each field, set rules.
Then define each field and set these fields.
Run validation, if it is FALSE, outputs errors and display another recaptcha and load the page.
If validation has passed, store all the input values in variables.
Get the admin email from preference.
Load Codeigniter’s email library, and set email preferences including setting up from, to, subject and message, and send the email to admin.
Set flash message and redirect to the contact page again and display the thank you message.

–UPDATE March 1st 2010–
This should work for your local server. However your host may have different email protocols. Please read more details here.

class Webshop extends Shop_Controller {

  function  Webshop(){
    parent::Shop_Controller();
    // load the validation library
		$this->load->library('validation');

  }

// other functions here

function message(){

		$rules['name'] = 'trim|required|max_length[32]';
		$rules['email'] = 'trim|required|max_length[254]|valid_email';
		$rules['message'] = 'trim|required';
		$rules['recaptcha_response_field'] = 'trim|required|valid_captcha';

		$this->validation->set_rules($rules);

		$fields['name']	= lang('general_name');
		$fields['email']	= lang('webshop_email');
		$fields['message']	= lang('message_message');
		$fields['recaptcha_response_field']	= 'Recaptcha';

		$this->validation->set_fields($fields);
	    /**
		 * form_validation, next version of Bep will update to form_validation
		 */
		//$this->form_validation->set_rules('name', 'Name', 'required');
		//$this->form_validation->set_rules('email', 'Email',  'required|valid_email');
		//$this->form_validation->set_rules('message', 'Message', 'required');
		//$this->form_validation->set_rules('captcha', 'Captcha', 'required');

        if ($this->validation->run() == FALSE)
		{
			// if any validation errors, display them
			$this->validation->output_errors();

			$captcha_result = '';
			$data['cap_img'] = $this->_generate_captcha();

			$data['title'] = lang('webshop_shop_name')." | ". lang('webshop_message_contact_us');
			$data['page'] = $this->config->item('backendpro_template_shop') . 'contact';
			$data['module'] = lang('webshop_folder');
			$this->load->view($this->_container,$data);
		}
		else
		{
		    // you need to send email
		    // validation has passed. Now send the email
			$name = $this->input->post('name');
			$email = $this->input->post('email');
			$message = $this->input->post('message');
			$myemail = 'admin(at)gmail.com';
			$this->load->library('email');
			$this->email->from($email.$name);
			$this->email->to($myemail);
			$this->email->subject(lang('webshop_message_subject'));
			$this->email->message(lang('webshop_message_sender').
			$name."\r\n".lang('webshop_message_sender_email').": ".
			$email. "\r\n".lang('webshop_message_message').": " . $message);
			$this->email->send();
			flashMsg('success', lang('webshop_message_thank_for_message'));
		    // $this->session->set_flashdata('subscribe_msg', lang('webshop_message_thank_for_message'));
		    redirect(lang('webshop_folder').'/contact');
		}
  	}  

Setting up Webshop Configuration for admin_email

I wrote about how to add preferences in BackendPro yesterday. The line, $myemail = ‘admin(at)gmail.com’; can be improved by using preferences instead of rewriting row php each time when you change email messages.

Add the following to system/application/controllers/admin/settings.php. (Please read the article for details)

'webshop'=> array('name'=> $this->lang->line('preference_webshop_configuration'), 'fields'=>'main_nav_parent_id,categories_parent_id,admin_email'),		

...

$config['field']['admin_email'] = array('rules'=>'trim|valid_email');

Add the following to modules/preferences/language/english/preferences_lang.php.

$lang['preference_label_admin_email'] = 'Admin Email to receive email messages';
$lang['preference_desc_admin_email'] = 'Enter admin email address to receive email messages from customers';

Go to DB, be_preferences field and insert a new field called ‘admin_email’.
Go to the back-end, System > Settings > Webshop Configuration and add your email address.

Now we can use $this->preference->item(‘admin_email’) instead of ‘admin(at)gmail.com’.
Change the line in the function message().

$myemail = $this->preference->item('admin_email');

Please test it by not filling some fields or filling up all. If your local server is not sending email, this article may help you if you are using XAMPP or similar development environment.

4 comments to Shopping cart v1.1 Part 15: contact page and sending email message

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>