I have a file attachment in my contact form as you can see here: http://www.cuylas.com/empleo/

I'm getting all of the fields but the file. Do you know where is the problem?


EDIT: Here's the code of my form page https://pastebin.com/2bupCGTx

  • did you do any code to send file in email??? Commented Sep 13, 2017 at 10:53
  • The only code I have is the one I attached located at /app/design/frontend/rwd/default/template/page/empleo.phtml
    – Èric H
    Commented Sep 13, 2017 at 11:02

1 Answer 1


Try this

1. Modify your contact form template

You need to modify the contact form template file:


a) Add enctype=”multipart/form-data” in element as:

<form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post" enctype="multipart/form-data">

b) Add file upload field below ‘Comment’ field (or anywhere you want) as:

    <label for="attachment"><?php echo Mage::helper('contacts')->__('Attachment') ?></label>
        <input name="MAX_FILE_SIZE" type="hidden" value="2000000" />
        <input name="attachment" id="attachment" type="file" />

2. Create controller class for processing file upload

Next step override Mage_Contacts_IndexController

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

                    <magefreak_customcontact before="Mage_Contacts">MageFreak_Customcontact</magefreak_customcontact>

b) Create your custom controller

Create controller file in your module dir as:


Copy this following code in it

require_once Mage::getModuleDir('controllers', 'Mage_Contacts') . DS . 'IndexController.php';
class MageFreak_Contactspro_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 */
            try {
                $postObject = new Varien_Object();

                $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('doc', 'docx','pdf'));
                        $path = Mage::getBaseDir('media') . DS . 'contacts';
                            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 */

                //sending file as attachment
                $attachmentFilePath = Mage::getBaseDir('media'). DS . 'contacts' . DS . $fileName;
                    $fileContents = file_get_contents($attachmentFilePath);
                    $attachment   = $mailTemplate->getMail()->createAttachment($fileContents);
                    $attachment->filename = $fileName;

                $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                        array('data' => $postObject)

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


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

            } catch (Exception $e) {

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

        } else {
  • Step 2 a) where can I find the file?
    – Èric H
    Commented Sep 13, 2017 at 13:52
  • Ok so, step 2 a) I did it on app/code/core/Mage/Contacts/etc/config.xml Then I followed the rest of steps, and a File Attachment appeared on my contact form. But I don't want it to appear there, I have 2 different contact forms, one standard (the one that got modified now, cuylas.com/contacto), and one so people can send their CV (cuylas.com/empleo). The second one is the one I want it to work. I already have the file attachment there, but it doesn't attach it on mail, other variables are sending ok.
    – Èric H
    Commented Sep 13, 2017 at 14:14
  • 1
    @ Èric H. First and foremost thing you should remember that not to modify core files. In this case you have to create module app/code/local/MageFreak/Customcontact/etc/config.xml. And now coming to your issue you have to create custom contact form for (cuylas.com/empleo). In that Contact form you have to add Step1. Refer this link ignaciogondra.com/2015/08/24/… to create custom contact form.
    – Raghav
    Commented Sep 14, 2017 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.