Create the Custom Form with Mail Template in Magento

Step1: Create the new modules

File path – app/etc/modules

<?xml version="1.0"?>
<config>
<modules>
<Custom_ContactForm>
<active>true</active>         
<codePool>local</codePool>
</Custom_ContactForm>
</modules>
</config>

Save as app/etc/modules/Custom_ContactForm.xml

Step2: Create the Controllers and Template File

Path:app/code/local/Custom/ContactForm/controllers

<?php class Custom_ContactForm_IndexController extends Mage_Core_Controller_Front_Action {
const XML_PATH_EMAIL_RECIPIENT = 'contacts/email/recipient_email';
const XML_PATH_EMAIL_SENDER = 'contacts/email/sender_email_identity';
const XML_PATH_EMAIL_TEMPLATE = 'contacts/email/email_template';
const XML_PATH_ENABLED = 'contacts/contacts/enabled';
public function indexAction() {
//Get current layout state
$this->loadLayout();
$block = $this->getLayout()->createBlock( 'Mage_Core_Block_Template', 'custom.contactform', array( 'template' => 'custom/contactform.phtml' ) );
$this->getLayout()->getBlock('content')->append($block);
$this->_initLayoutMessages('core/session');
$this->renderLayout();
}
public function sendemailAction() {
//Fetch submited params
$params = $this->getRequest()->getParams();
if ( $params ) {
$translate = Mage::getSingleton('core/translate');
/* @var $translate Mage_Core_Model_Translate */
$translate->setTranslateInline(false);
try {
$postObject = new Varien_Object();
$postObject->setData($params);
$error = false;
if (!Zend_Validate::is(trim($params['name']) , 'NotEmpty')) { $error = true; }
if (!Zend_Validate::is(trim($params['comment']) , 'NotEmpty')) { $error = true; }
if (!Zend_Validate::is(trim($params['email']), 'EmailAddress')){ $error = true; }
if (Zend_Validate::is(trim($params['hideit']), 'NotEmpty')) { $error = true; }
if ($error) { throw new Exception(); }
$mailTemplate = Mage::getModel('core/email_template')->loadByCode('Custom Mail Template Name');
// Load the Custom template from "Admin Template name" which under System ->Transactional Emails * @var $mailTemplate Mage_Core_Model_Email_Template */
$mailTemplate->setDesignConfig(array('area' => 'frontend')) ->setReplyTo($params['email']) ->sendTransactional( $mailTemplate,
// Changed above variabe as like this, while implementing Custom Template
Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER), Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT), null, array('data' => $postObject) );
if (!$mailTemplate->getSentSuccess()) {
throw new Exception();
}
echo $translate->setTranslateInline(true);
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.'));
//Redirect back to index action of (this) custom-contactform controller
$this->_redirect('redirectpagepath eg:home/?sent=success#contact');
return;
}
catch (Exception $e) {
$translate->setTranslateInline(true);
Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
//Redirect back to index action of (this) custom-contactform controller
$this->_redirect('redirectpagepath eg:home/');
return;
}
} else {
//Redirect back to index action of (this) custom-contactform controller
$this->_redirect('redirectpagepath eg:home/');
}
//Redirect back to index action of (this) custom-contactform controller
$this->_redirect('redirectpagepath eg:home/');
}
} ?> 

Save as app/code/local/Custom/ContactForm/controllers/IndexController.php

Create the Mail Template for Module

<?xml version="1.0"?>
 
<config>
    <modules>
        <Custom_ContactForm>
            <version>0.1.0</version>
        </Custom_ContactForm>
    </modules>   
 
    <frontend>
        <routers>
            <Custom_ContactForm>
                <use>standard</use>
                <args>
                    <module>Custom_ContactForm</module>
                    <frontName>custom-contactform</frontName>
                </args>
            </Custom_ContactForm>
        </routers>
    </frontend>    
    <global>      
    	<template>
    		<email>
		  <custom_form_email_template translate="label" module="contacts">
			<label>Custom Form</label>
			<file>custom_form.html</file>
			<type>html</type>
		   </custom_form_email_template>
		</email>
    	</template>
    </global>     
</config>

Save as app/code/local/Custom/ContactForm/etc/config.xml

Step3: Create the Frontend Form File

Path:app/design/frontend/yourtheme/default/template/custom

<div class="box simple_contact">
<form id="conversationform" name="offerform_form" action="<?php echo $this->getUrl('custom-contactform/') ?>index/sendemail" method="post">
	<input type="text" class="required-entry input-text conversation_input" placeholder="Your Name" name="name" id="yourname">
	<input type="text" class="required-entry input-text conversation_input"  placeholder="Company Name" name="companyname" id="companyname">
	<input type="text" class="required-entry input-text conversation_input"  placeholder="Email Address" name="email" id="email">
	<input type="text" class="required-entry input-text conversation_input"  placeholder="Telephone Number" name="telephone" id="telephone">
	<input type="text" class="required-entry input-text conversation_input"  placeholder="I'm Interested In" name="interested" id="interested">
	<textarea class="required-entry input-text conversation_input"  placeholder="Message" name="comment" id="message"></textarea>
    <div class="button-set">
       <input type="text" name="hideit" id="hideit" value="" style="display:none !important;" />
	   <button class="form-button" type="submit" id="conversubmit"><span>SEND MESSAGE</span></button>
    </div>
</form>
 
</div>

Save as app/design/frontend/yourtheme/default/template/custom/contactform.phtml

Step4: Create the Custom Transactional Mail Template File

Path: app/locale/en_US/template/email/

<!--@subject Your Subject eg:hai@-->
<!--@vars {"var data.name":"Sender Name", "var data.email":"Sender Email", "var data.telephone":"Sender Telephone", "var data.comment":"Comment"} @-->
Name: {{var data.name}}
Email: {{var data.email}}
Telephone: {{var data.telephone}}
Comment: {{var data.comment}}

save as app/locale/en_US/template/email/custom_form.html

Important Code

Set Transactional Mail Template

$mailTemplate = Mage::getModel('core/email_template')->loadByCode('Your Transactional Template Name');
$mailTemplate->setDesignConfig(array('area' => 'frontend')) ->setReplyTo($params['email']) ->sendTransactional(
//Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE), $mailTemplate,
// Changed above variabe as like this, while implementing Custom Template
Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER), Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT), null, array('data' => $postObject) );

Leave a Reply

Your email address will not be published. Required fields are marked *