0

Hi I create cusrom form(frontend). And I want to sent Transactional Email with file .

Below is IndexController and file template send_email.phtml

IndexController:

<?php

class Send_Email_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        //Get current layout state
        $this->loadLayout();   

        $block = $this->getLayout()->createBlock(
            'Mage_Core_Block_Template',
            'email.simple_contact',
            array(
                'template' => 'send_email/simple_contact.phtml'
            )
        );

        $this->getLayout()->getBlock('content')->append($block);
        //$this->getLayout()->getBlock('right')->insert($block, 'catalog.compare.sidebar', true);

        $this->_initLayoutMessages('core/session');

        $this->renderLayout();
    }

    public function sendemailAction()
    {
        if ($error) {
            throw new Exception();
        }

        //Fetch submited params
        $params = $this->getRequest()->getParams();

        $mail = new Zend_Mail();
        $mail->setBodyText($params['comment']);
        $mail->setFrom($params['email'], $params['name']);
        $mail->addTo('[email protected]', 'Some Recipient');
        $mail->setSubject('Test Email Module for Magento');
        try {
            $mail->send();
            Mage::getSingleton('core/session')->addSuccess('send email.');
        }
        catch(Exception $ex) {
            Mage::getSingleton('core/session')->addError('Unable to send email.');

        }

        //Redirect back to index action of (this) inchoo-simplecontact controller
        $this->_redirect('email/');
    }
}

template send_email.phtml:

<div class="box simple_contact">

    <form id="simple_contact_form" name="simple_contact_form" action="<?php echo $this->getUrl('email/') ?>index/sendemail" method="post" enctype="multipart/form-data">

        <fieldset class="group-select">
            <h4 class="legend">Simple Contact Form</h4>
            <ul>
                <li>
                    <div class="input-box">
                        <label for="name">Gimme your name <span class="required">*</span></label><br />

                        <input name="name" id="name" title="Name" value="" class="input-text required-entry" type="text" />
                    </div>

                    <div class="input-box">
                        <label for="email">And your email <span class="required">*</span></label><br />
                        <input name="email" id="email" title="Email" value="" class="input-text required-entry validate-email" type="text" />
                    </div>

                    <div class="clear"></div>
                    <label for="attachment"><?php echo Mage::helper('contacts')->__('Attachment') ?></label>
                    <div class="input-box">
                        <input name="MAX_FILE_SIZE" type="hidden" value="2000000" />
                        <input name="customerimg" id="attachment" class="input-text" type="file" />
                    </div>
                </li>
            </ul>
        </fieldset><br/>
        <div class="button-set">
           <button type="submit" title="Submit" class="button"><span><span>Submit</span></span></button>
        </div>
    </form>

</div>
<script type="text/javascript">
    //<![CDATA[
    var contactForm = new VarienForm('simple_contact_form', true);
    //]]>
</script>
2

1 Answer 1

1

You should first get the file, save it, get the contents (or convert it to base 64 if you don't want to save it) and then add it as an attachment. You are using zend_mail so this documentation should help you do it:

$mail->createAttachment($file);

Please refer to this documentation:

http://framework.zend.com/manual/1.12/en/zend.mail.attachments.html

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.