Saturday, August 20, 2011

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

6 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