[Magento] Create Contact Form with Dynamic Recipient

There are many ways of creating contact forms to email to multiple recipient. I'm going to describe a way that uses Javascript a the frontend to change the target recipient.

Advantage of this method:
  • Easy to add in new emails in the future.
Disadvantage of this method:
  • For those who are particularly concern email privacy, you might not want to use this method. You can modify the email checking to the backend (controller) instead.


Okay enough talking, lets get started.

Step 1: Set up HTML form fields

I'm going to use an example of drop down select to change the email. Of course, this is fairly easy to modify with other forms as well.

Open up app/design/frontend/default/<theme>/template/contacts/contacts.phtml
Insert the following snippets anywhere between the <form>...</form> tag.

<select name="interest" id="interest" title="<?php echo Mage::helper('contacts')->__('Please select department to contact') ?>" value="" class="input-text required-entry" type="text" onchange="changeIntendedRecipient()" >
 <option value="Department 1">Department 1</option>
 <option value="Department 2">Department 2</option>
 <option value="Department 3">Department 3</option>
 <option value="Department 4">Department 4</option>
</select>
<input type="hidden" name="intended_recipient_email" id="intended_recipient_email" value="default_email@example.com" />
<input type="hidden" name="intended_recipient_name" id="intended_recipient_name" value="Default name" />

Step 2: Setup Javascript for onclick() change.


<script type="text/javascript">

function changeIntendedRecipient() {
 switch(document.getElementById("interest").selectedIndex) {
  case 0:
   document.getElementById("intended_recipient_email").value = "email1@example.com";
   document.getElementById("intended_recipient_name").value = "Recipient 1";
   break;
  case 1:
   document.getElementById("intended_recipient_email").value = "email2@example.com";
   document.getElementById("intended_recipient_name").value = "Recipient 2";
   break;
  case 2:
   document.getElementById("intended_recipient_email").value = "email3@example.com";
   document.getElementById("intended_recipient_name").value = "Recipient 3";
   break;
  case 3:
   document.getElementById("intended_recipient_email").value = "email4@example.com";
   document.getElementById("intended_recipient_name").value = "Recipient 4";
   break;
  default:
 }
}

</script>



Step 3: IndexController hack

Now we going to change the email handler (controller) in Magento core.

Open up app/code/core/Mage/Contacts/controllers/IndexController.php

Search for these lines


$mailTemplate = Mage::getModel('core/email_template');
/* @var $mailTemplate Mage_Core_Model_Email_Template */
$mailTemplate->setDesignConfig(array('area' => 'frontend'))
 ->setReplyTo($post['email'])
 ->sendTransactional(
  Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
  Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
  Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
  null,
  array('data' => $postObject)
  );

Add the following codes before those lines.

if (htmlentities(trim($post['intended_recipient_email']))) {
 $newrecipient = array('email' => htmlentities(trim($post['intended_recipient_email'])), 'name' => htmlentitiestrim($post['intended_recipient_name'])));

} else {
 $newrecipient = Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT);
}


Replace line

  Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),

with

  $newrecipient,


We are done!

Its a very simple hack thought. Its good enough for beginners to get started hacking. :)

Comments

  1. Hi Grant, thats for pointing out the error, I left out the hidden fields in Step 1, thats why the Javascript couldn't work.

    You can try again by adding in the hidden fields in step 1 :)

    ReplyDelete
  2. tried that and im getting: 
    Fatal error: Call to undefined function htmlentitiestrim() /code/core/Mage/Contacts/controllers/IndexController.php on line 93
    ie,    $newrecipient = array('email' => htmlentities(trim($post['intended_recipient_email'])), 'name' => htmlentitiestrim($post['intended_recipient_name'])); 

    ReplyDelete
  3. Hey Bing, i still can't get this working, if you can figure out the error i will defiantly donate to your paypal!  

    ReplyDelete
  4. Thanks for the code mate, i fixed the error, and thanks for your help! 

    ReplyDelete
  5. Hi there! Was away for holiday over the long weekend. Glad that you've fixed the error! ;)

    ReplyDelete
  6. Thanks for the pointing out the error.I have intrested html code.Java script is very goog.This script type is very wonderfull.

    ReplyDelete
  7. Thank you buddy. Did some error in coding. Your code works perfect. Did this moderation in Magento product: http://www.apptha.com/magento/ajax-cart-pro

    ReplyDelete
  8. Magento’s user-friendly control interface features highly effective marketing, seo and catalog-management resources to give suppliers the power to create sites that are designed to their unique business needs.

    Magento Cloud Hosting

    ReplyDelete
  9. Magento is an open source web application which is better known for building powerful eCommerce based websites. It provides the agility of an open source technology by playing nice with third-party extensions and modules, and offers users unprecedented control over their online storefronts. There are a lot of extensions or modules made by the Magento community or third-party coders, and are readily available to make your e-commerce solution features rich and get your online sales booming.

    Magento Hosting I Magento Cloud Hosting

    ReplyDelete
  10. Hey great Coding...... Thank you for sharing This video Amazing.


    Web Development India

    ReplyDelete
  11. Hi! Ive had a problem when sending:

    Fatal error: Call to undefined function htmlentitiestrim() in /usr/home/pekebebe.es/web/app/code/core/Mage/Contacts/controllers/IndexController.php on line 96

    I can load the list but its something wrong when sending

    I dont know what to try for fixing it

    ReplyDelete

Post a Comment

Popular posts from this blog

[Azure Websites PHP] Cross Domain request results in blank response page after Preflight HTTP OPTIONS