Adding upload field in contact form and send as attachment

October 27, 2012  |  26 Comments  |  by Raj (MagePsycho)  |  Latest, Magento


Nowadays most of the clients wants extra fields in contacts form of magento to fulfill their requirements. And adding custom fields other than upload field (for example: text, textarea, select etc) in contact form are easy in the sense you can easily include the field values in contact email template.
But adding upload field can be trickier as you have to process the file upload operation and attach it with your email contents.


1. Modify the contact form template
You need to modify the contact form template file: app/design/frontend/[your-interface]/[your-theme]/template/contacts/form.phtml:
a> Add enctype=”multipart/form-data” in <form> element as:

<form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post" enctype="multipart/form-data">

b> Add file upload field below ‘Comment’ field (or anywhere you want) as:

	<label for="attachment"><?php echo Mage::helper('contacts')->__('Attachment') ?></label>
	<div class="input-box">
		<input name="MAX_FILE_SIZE" type="hidden" value="2000000" />
		<input name="attachment" id="attachment" class="input-text" type="file" />

2. Create controller class for processing file upload
Next step is to override the Mage_Contacts_IndexController class.
For this you need to create a custom module. For tutorial purpose I am assuming Namespace to be ‘MagePsycho’ and Module to be ‘Customcontact’.
a> Add xml override code in your module’s config.xml as:

					<magepsycho_customcontact before="Mage_Contacts">MagePsycho_Customcontact</magepsycho_customcontact>

b> Create your custom controller
Create controller file in your module dir as: app/code/local/MagePsycho/Customcontact/controllers/IndexController.php
and copy the following code:

require_once Mage::getModuleDir('controllers', 'Mage_Contacts') . DS . 'IndexController.php';
class MagePsycho_Customcontact_IndexController extends Mage_Contacts_IndexController
	public function postAction()
        $post = $this->getRequest()->getPost();
        if ( $post ) {
            $translate = Mage::getSingleton('core/translate');
            /* @var $translate Mage_Core_Model_Translate */
            try {
                $postObject = new Varien_Object();

                $error = false;

                if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
                    $error = true;

                if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
                    $error = true;

                if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                    $error = true;

                if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                    $error = true;

				$fileName = '';
				if (isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] != '') {
				    try {
						$fileName		= $_FILES['attachment']['name'];
						$fileExt		= strtolower(substr(strrchr($fileName, ".") ,1));
						$fileNamewoe	= rtrim($fileName, $fileExt);
						$fileName		= preg_replace('/\s+', '', $fileNamewoe) . time() . '.' . $fileExt;

						$uploader		= new Varien_File_Uploader('attachment');
						$uploader->setAllowedExtensions(array('doc', 'docx','pdf', 'jpg', 'png', 'zip')); //add more file types you want to allow
						$path = Mage::getBaseDir('media') . DS . 'contacts';
							mkdir($path, 0777, true);
						$uploader->save($path . DS, $fileName );

				    } catch (Exception $e) {
		                $error = true;

                if ($error) {
                    throw new Exception();
                $mailTemplate = Mage::getModel('core/email_template');
                /* @var $mailTemplate Mage_Core_Model_Email_Template */

				//sending file as attachment
				$attachmentFilePath = Mage::getBaseDir('media'). DS . 'contacts' . DS . $fileName;
					$fileContents = file_get_contents($attachmentFilePath);
					$attachment   = $mailTemplate->getMail()->createAttachment($fileContents);
					$attachment->filename = $fileName;

                $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                        array('data' => $postObject)

                if (!$mailTemplate->getSentSuccess()) {
                    throw new Exception();


                Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));

            } catch (Exception $e) {

                Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));

        } else {

3> There you go.
Try to attach file and submit the contact form, you will get that file as attachment in contact email.


Default Contact Form

Contact Form With Upload Field

Hope this helps you.
Happy E-Commerce!

Create Dynamic CMS Navigation For Magento Frontend

October 15, 2012  |  6 Comments  |  by Raj (MagePsycho)  |  Latest, Magento


As you know in Magento, catalog has dynamic menu but cms doesn’t in frontend.
If you are curios about how to create dynamic menu for CMS which can either be included in left or right sidebar for all CMS pages, then you are in right place.


1. Create navigation.phtml file
Create nagivation.phtml file in folder: app/design/frontend/[your-interface]/[your-theme]/template/cms/
and add the following code:

$cmsPages = Mage::getModel('cms/page')->getCollection()
			->addFieldToFilter('identifier', array(
					'nin' => array(
<script type="text/javascript" src=""></script>
<style type="text/css">
	#cms-navigation { color: red; }‚Äč
<script type="text/javascript">
jQuery(document).ready(function() {
	var url = window.location.pathname,
    urlRegExp = new RegExp(url.replace(/\/$/,'') + "$");
	jQuery('#cms-navigation a').each(function(){
<div class="block">
	<div class="block-title">
		<strong><span>CMS Navigation</span></strong>
	<div class="block-content">
		<ul id="cms-navigation" style="padding: 10px;">
			foreach($cmsPages as $_cms):
				$page = $_cms->getData();
				<li><a href="<?php echo $this->getBaseUrl() . $page['identifier']; ?>"><?php echo $page['title']; ?></a></li>


You can simply download the navigation.phtml file from [here]

2> Include CMS Navigation in Layout
Copy/paste the following XML code in your layout XML file (preferably in your theme’s local.xml file):

	<reference name="left"><!--or right-->
		<block type="core/template" name="cms-navigation" template="cms/navigation.phtml" before="-" />

This will automatically include the CMS navigation in layout file.
Make sure you select the proper Layout template from CMS form (Design > Page Layout > Layout).
For example, in order to include CMS navigation in left column you either have to use: Layout = 2 columns with left bar or 3columns.

3> That’s all.

If you visit the CMS pages in frontend you will see navigation with all the CMS pages dynamically included with current one being highlighted (see below snapshot).
This will of course make your CMS pages look more beautiful and user friendlier.

CMS Navigation

Happy E-commerce!