Magento : How to send contact form email with attachment in magento?

Magento : How to send contact form email with attachment in magento?

August 5, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento : How to send contact form email with attachment in magento?

Sending Attachments with Magento Contact Form: A Step-by-Step Guide

As a developer working on an e-commerce platform like Magento, you may often need to implement custom functionality that isn’t readily available in the core features. One such common requirement is handling attachments in the default contact form.

Magento’s built-in contact form doesn’t support file attachments by default. This means you’ll need to extend the core functionality using Magento’s extension system and Zend Framework mailing classes. In this post, we will walk through a simple yet effective way to achieve this.

Understanding the Default Contact Form

The default Magento contact form is managed by the Mage_Core_Model_Email_Template model and the Mage_Page_Block_Html_Form block. When a user submits the form, the data is posted to the contact/index/post controller action.

Extending the Default Functionality

To send attachments with the contact form submission, we need to modify two key areas:

  1. The Form Block
  2. The Post Action

Let’s start by modifying the form block to accept a file upload field.

Modifying the Contact Form Block

First, create an extension that extends Magento’s core Contact module. In your new module’s config.xml file, add the following declaration:

<blocks>
    <contact>
        <class>MyCompany_Contact_Block</class>
    </contact>
</blocks>

Then, in your custom block class (app/code/local/MyCompany/Contact/Block/Contact/Index.php), override the default form block:

class MyCompany_Contact_Block_Contact_Index extends Mage_Core_Block_Template {
  public function getFormActionUrl() {
    return $this->getUrl('*/*/post', array(
        'form_key' => Mage::getSingleton('core/session')->getFormKey()
    ));
  }

  protected function _prepareLayout() {
    parent::_prepareLayout();
    $head = $this->getLayout()->getBlock('head');
    if ($head) {
        $head->setTitle($this->__('Contact Us'));
    }
    return $this;
  }

  public function getFormHtml() {
    $form = new Varien_Data_Form();
    $form->setAction($this->helper('contact')->getContactPostUrl())
         ->setId('contact_form')
         ->setName('contact_form')
         ->setMethod('post')
         ->setUseContainer(true);

    // Add file upload field
    $field = new Varien_Data_Form_Element_File(array(
        'name' => 'attachment',
        'id' => 'attachment',
        'label' => Mage::helper('contact')->__('Upload attachment'),
        'required' => false,
        'class' => 'validate-file-extension validate-size'
    ));

    $form->addElement($field);
    // Add other required form fields

    return $form->getHtml();
  }
}

Note that we’ve added a new Varien_Data_Form_Element_File instance for the attachment field. This field allows users to upload a file with their contact submission.

Modifying the Post Action

Next, let’s modify the post action of the Contact controller (app/code/core/Mage/Contact/controllers/IndexController.php) to handle the uploaded file:

public function postAction() {
    $post = $this->getRequest()->getPost();
    if ($post) {
        $translate = Mage::getSingleton('core/translate');
        /* @var $translate Mage_Core_Model_Translate */
        $translate->init('frontend', true);
        try {
            // Validate the form data
            $errors = array();

            if (!($validator = Mage::getModel('validate/code')) || !($validator->isValid($post['name'])) {
                $errors[] = Mage::helper('contact')->__('Invalid name');
            }

            // Handle image upload
            if (isset($_FILES)) {
                $uploader = new Varien_File_Uploader('attachment');

                // If there was a file uploaded
                if ($uploader->save(Mage::getBaseDir('media') . DS . 'contact' . DS)) {
                    $imagePath = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'contact/' . $uploader->getFileName();
                } else {
                    $errors[] = Mage::helper('contact')->__('Unable to find a file uploaded to the server.');
                }
            }

            // If no errors
            if (!count($errors)) {
                // Send email
                $postObject = new Varien_Object();
                $postObject->setData($post);

                try {
                    $mailTemplate = Mage::getModel('core/email_template');
                    /* @var $mailTemplate Mage_Core_Model_Email_Template */
                    $mailTemplate->setDesignConfig(array('area' => 'frontend'));
                    $mailTemplate->sendTransactional(
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                        null,
                        array('data' => $postObject)
                    );
                } catch (Exception $e) {
                    $messages = $e->getMessages();
                    if (is_array($messages)) {
                        foreach ($messages as $message) {
                            $errors[] = $message;
                        }
                    } else {
                        $errors[] = Mage::helper('contact')->__('Unable to send email. Please, try again later');
                    }
                }

                if (!count($errors)) {
                    // $this->_getSession()->addSuccess(Mage::helper('contact')->__('Your inquiry has been submitted.'));
                    return true;
                } else {
                    throw new Exception(implode("\n", $errors));
                }
            } else {
                throw new Exception(implode("\n", $errors));
            }
        } catch (Exception $e) {
            $translate->setTranslateInline(true);
            Mage::getSingleton('core/session')->addError($e->getMessage());
            $translate->setTranslateInline(false);

            // Go back to form
            return false;
        }
    }

    return false;
}

We’ve added the necessary code to handle file uploads using Varien_File_Uploader, save the uploaded file, and then send an email with the attachment.

Conclusion

Sending attachments with Magento’s contact form requires extending the core functionality. By modifying the contact block and post action as shown in this guide, you can easily implement this feature without needing to customize the Zend Mail function directly.

Remember that security is paramount when handling file uploads. Always validate the uploaded files and limit their size and types to prevent potential security vulnerabilities.

Don’t forget to check out LaravelCompany’s Magento development services if you need expert help with your e-commerce projects.

Happy coding!

Extensions Themes Configuration Email

Our network