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>
</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
  12. This is Great Coding.. Working Fine..


    Travel Management Software

    ReplyDelete

Post a Comment

Popular posts from this blog

225019099301.apps.googleusercontent.com

Recent activity on my Gmail just revealed that there's this app "Authorized Application (225019099301.apps.googleusercontent.com)" from IP address 54.235.159.144 assessing my Gmail.

The IP address is from a Amazon server ec2-54-235-159-144.compute-1.amazonaws.com. (IP Lookup).

And blacklist check (http://whatismyipaddress.com/blacklist-check) shows that the IP is clean too.

Clicking on the "Manage Account Access" didn't give any information on the application "225019099301.apps.googleusercontent.com".


After Googling, it appears that the app is Mailbox (source: http://productforums.google.com/forum/#!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 https://rtcamp.com/tutorials/nodejs/node-js-npm-install-ubuntu/, or this http://stackoverflow.com/questions/16302436/install-nodejs-on-ubuntu-12-10#comment32247107_16303380

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 my_domain.com. You'll first need to generate the the .csr file for GoDaddy with the following command:

openssl req -new -newkey rsa:2048 -nodes -keyout my_domain.com.key -out my_domain.com.csr


This gives you 2 files:
my_domain.com.key - This is the private key
my_domain.com.csr - This is the Certificate Signing Request


Copy the content of my_domain.com.csr 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 my_domain.com.pfx -inkey my_domain.com.key -in 18f1c77f369c0b59.crt


Step 3: Certificate to Upload to Azure.


Step 4: Assign SSL Bindings.




Step 5: Done!


References:

http://azure.microsoft.com/en-us/documentation/articles/web-sites-configure-ssl-certificate…