Skip to main content

[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>
<input type="hidden" name="intended_recipient_email" id="intended_recipient_email" value="" />
<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 = "";
   document.getElementById("intended_recipient_name").value = "Recipient 1";
  case 1:
   document.getElementById("intended_recipient_email").value = "";
   document.getElementById("intended_recipient_name").value = "Recipient 2";
  case 2:
   document.getElementById("intended_recipient_email").value = "";
   document.getElementById("intended_recipient_name").value = "Recipient 3";
  case 3:
   document.getElementById("intended_recipient_email").value = "";
   document.getElementById("intended_recipient_name").value = "Recipient 4";


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'))
  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




We are done!

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


  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 :)

  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'])); 

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

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

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

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

  7. Thank you buddy. Did some error in coding. Your code works perfect. Did this moderation in Magento product:

  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

  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

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

    Web Development India

  11. Hi! Ive had a problem when sending:

    Fatal error: Call to undefined function htmlentitiestrim() in /usr/home/ on line 96

    I can load the list but its something wrong when sending

    I dont know what to try for fixing it

  12. This is Great Coding.. Working Fine..

    Travel Management Software


Post a Comment

Popular posts from this blog

Recent activity on my Gmail just revealed that there's this app "Authorized Application (" from IP address assessing my Gmail.

The IP address is from a Amazon server (IP Lookup).

And blacklist check ( shows that the IP is clean too.

Clicking on the "Manage Account Access" didn't give any information on the application "".

After Googling, it appears that the app is Mailbox (source:!topic/gmail/9LVW_etXyTE)

To make things easy, Google should show the applications with their ID.

Backup MySQL to Azure Storage in 30 Seconds

Step 1. Disable password prompt for "mysqldump command"mysqldump: Got error: 1045: Access denied for user 'root'@'localhost' (using password: NO) when trying to connect 1. Run "vi ~/.my.cnf"
2. Add the following lines

[mysqldump]user=mysqluser password=secret
3. For Bitnami, you'll need to append the following line in "/opt/bitnami/mysql/my.cnf"

!include ~/.my.cnf
4. Try running to see if the command works.
mysqldump --all-databases > /home/bitnami/backups/db-backup.sql

Step 2(a). Install Azure-CLI Prerequisites: Installing npm.sudo apt-get update sudo apt-get install nodejs sudo apt-get install npm Note: If you facing issue while installing nodejs/npm on Ubuntu 12.04, you can refer to his article for alternative way to install, or this

Install Azure CLI.npm install azure-cl…

Generate GoDaddy SSL Certificate (.crt) for Azure Websites (.pfx)

Step 1: Getting GoDaddy SSL cert.
Let's say you have a domain name of You'll first need to generate the the .csr file for GoDaddy with the following command:

openssl req -new -newkey rsa:2048 -nodes -keyout -out

This gives you 2 files: - This is the private key - This is the Certificate Signing Request

Copy the content of file to the SSL signing authority (GoDaddy).

Once approved, GoDaddy give you back a .zip file with the following 2 files:

18f1c77f369c0b59.crt - This is your cert
gd_bundle-g2-g1.crt - This is the GoDaddy Certificate Chain

Step 2: Convert a CERT/PEM certificate to a PFX certificate
openssl pkcs12 -export -out -inkey -in 18f1c77f369c0b59.crt

Step 3: Certificate to Upload to Azure.

Step 4: Assign SSL Bindings.

Step 5: Done!