Tuesday, 17 June 2014

Magento send contact form email with attachment

Here You can see how to send contact form email with image attachment.

1.You need to rewrite Magento's Contacts IndexController.

 config.xml:
Add xml override code in your module’s config.xml as:

<frontend>
    <routers>
        <contacts>
            <args>
                <modules>
                    <namespace_modulename before="Mage_Contacts">Namespace_Modulename</namespace_modulename>
                </modules>
            </args>
        </contacts>
    </routers>
</frontend>


IndexController.php:
copy the following code:

<?php
require_once Mage::getModuleDir('controllers', 'Mage_Contacts') . DS . 'IndexController.php';
class Namespace_Modulename_IndexController extends Mage_Contacts_IndexController
{
public function postAction()
    {
        $post = $this->getRequest()->getPost();      
      
        if ($post) {
          
            $translate = Mage::getSingleton('core/translate');
          
          
            /* @var $translate Mage_Core_Model_Translate */
            $translate->setTranslateInline(false);
          
            try {
                $postObject = new Varien_Object();
                $postObject->setData($post);
              
                $error = false;
                if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
                    $error = true;
                }

                if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
                    $error = true;
                }

                if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                    $error = true;
                }

                if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                    $error = true;
                }
                
                $fileName = '';
                if (isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] != '') {
                    try {
                        $fileName       = $_FILES['attachment']['name'];
                        $fileExt        = strtolower(substr(strrchr($fileName, ".") ,1));
                        $fileNamewoe    = rtrim($fileName, $fileExt);
                        $fileName       = preg_replace('/\s+', '', $fileNamewoe) . time() . '.' . $fileExt;

                        $uploader       = new Varien_File_Uploader('attachment');
                        $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
                        $uploader->setAllowRenameFiles(false);
                        $uploader->setFilesDispersion(false);
                        $path = Mage::getBaseDir('media') . DS . 'Customform';
                        if(!is_dir($path)){
                            mkdir($path, 0777, true);
                        }
                        $uploader->save($path . DS, $fileName );

                    } catch (Exception $e) {
                        $error = true;
                    }
                }
                        
                if ($error) {
                    throw new Exception();
                }     
              
                $mailTemplate = Mage::getModel('core/email_template');
                /* @var $mailTemplate Mage_Core_Model_Email_Template */
              
          
        //send file
                $attachmentFilePath = Mage::getBaseDir('media'). DS . 'Customform' . DS . $fileName;
                if(file_exists($attachmentFilePath)){
                    $fileContents = file_get_contents($attachmentFilePath);
                    $attachment   = $mailTemplate->getMail()->createAttachment($fileContents);
                    $attachment->filename = $fileName;
                }

                 
                $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)
                    );

                if (!$mailTemplate->getSentSuccess()) {
                    throw new Exception();
                }

                $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.'));
              
                $this->_redirect('*/*/');

                return;
            } catch (Exception $e) {
                $translate->setTranslateInline(true);

                Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
                $this->_redirect('*/*/');
                return;
            }

        } else {
            $this->_redirect('*/*/');
        }
    }
}
?>

 
Hope this helps you.
Best of luck.

No comments:

Post a Comment