So I tried to create a customized form action, based on the built in contacts form in Magento.
I managed to send an uploaded file in an attachment email, and save it to a folder. But I would like to upload multiple files.
Form code:
<form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post" enctype="multipart/form-data">
 <h2 class="legend"><?php echo Mage::helper('contacts')->__('Leave') ?> <?php echo Mage::helper('contacts')->__('a') ?> <b><?php echo Mage::helper('contacts')->__('Message') ?></b></h2>
 <div class="row">
 <ul class="form-list col-sm-6">
 <li>
 <label for="name" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Name') ?></label>
 <div class="input-box">
 <input name="name" id="name" title="<?php echo Mage::helper('contacts')->__('Name') ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserName()) ?>" class="input-text required-entry" type="text" />
 </div>
 </li>
 <li>
 <label for="email" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Email') ?></label>
 <div class="input-box">
 <input name="email" id="email" title="<?php echo Mage::helper('contacts')->__('Email') ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserEmail()) ?>" class="input-text required-entry validate-email" type="text" />
 </div>
 </li>
 <li>
 <label for="telephone"><?php echo Mage::helper('contacts')->__('Telephone') ?></label>
 <div class="input-box">
 <input name="telephone" id="telephone" title="<?php echo Mage::helper('contacts')->__('Telephone') ?>" value="" class="input-text" type="text" />
 </div>
 </li>
 </ul>
 <ul class="form-list col-sm-6">
 <li>
 <label for="comment" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Comment') ?></label>
 <div class="input-box input-textarea">
 <textarea name="comment" id="comment" title="<?php echo Mage::helper('contacts')->__('Comment') ?>" class="required-entry input-text" cols="5" rows="3" placeholder="<?php echo Mage::helper('contacts')->__('Comment') ?>"></textarea>
 </div>
 </li>
<li>
 <label for="attachment"><?php echo Mage::helper('contacts')->__('Attachment') ?></label>
 <div>
 <input name="MAX_FILE_SIZE" type="hidden" value="2000000" />
 <input name="attachment" id="attachment" type="file" multiple="multiple" />
 </div>
</li>
 <li>
 <input type="text" name="hideit" id="hideit" value="" style="display:none !important;" />
 <button type="submit" title="<?php echo Mage::helper('contacts')->__('Submit') ?>" class="button"><span><span><?php echo Mage::helper('contacts')->__('Submit') ?></span></span></button>
 </li>
 </ul>
 </div>
</form>
<script type="text/javascript">
//<![CDATA[
 var contactForm = new VarienForm('contactForm', true);
//]]>
</script>
Here is my controller code:
 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 = '';
foreach ($_files as $filename => $fileOpt) {
 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', 'jpg', 'png', 'zip')); //add more file types you want to allow
 $uploader->setAllowRenameFiles(false);
 $uploader->setFilesDispersion(false);
 $path = Mage::getBaseDir('media') . DS . 'contacts';
 if(!is_dir($path)){
 mkdir($path, 0777, true);
 }
 $uploader->save($path . DS, $fileName );
 } catch (Exception $e) {
 Mage::getSingleton('customer/session')->addError($e->getMessage());
 $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;
 if(file_exists($attachmentFilePath)){
 $fileContents = file_get_contents($attachmentFilePath);
 $attachment = $mailTemplate->getMail()->createAttachment($fileContents);
 $attachment->filename = $fileName;
 }
 /**************************************************************/
1 Answer 1
I had similar requirement and able to achieve using following code. 
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;
 }
 /**************************************************************/
 $photos = array();
 foreach ($_FILES['photo']['name'] as $key => $image) {
 if (empty($image)) {
 continue;
 }
 $fileName = '';
 if (isset($_FILES['photo']['name'][$key]) && $_FILES['photo']['name'][$key] != '') {
 try {
 $fileName = $_FILES['photo']['name'][$key];
 $fileExt = strtolower(substr(strrchr($fileName, ".") ,1));
 $fileNamewoe = rtrim($fileName, $fileExt);
 $fileName = preg_replace('/\s+', '', $fileNamewoe) . time() . $key . '.' . $fileExt;
 if(!in_array($fileExt,array('jpg','jpeg', 'png','gif'))){
 Mage::getSingleton('core/session')->addError(Mage::helper('rga')->__('Only jpg, jpeg, png and gif file types are allowed.'));
 session_write_close();
 $this->_redirect('*/*/');
 }
 array_push($photos, $fileName);
 $uploader = new Varien_File_Uploader(
 array(
 'name' => $_FILES['photo']['name'][$key],
 'type' => $_FILES['photo']['type'][$key],
 'tmp_name' => $_FILES['photo']['tmp_name'][$key],
 'error' => $_FILES['photo']['error'][$key],
 'size' => $_FILES['photo']['size'][$key]
 )
 );
 $uploader->setAllowedExtensions(array('jpg','jpeg', 'png','gif'));
 $uploader->setAllowRenameFiles(false);
 $uploader->setFilesDispersion(false);
 $path = Mage::getBaseDir('media') . DS . 'rga';
 if(!is_dir($path)){
 mkdir($path, 0777, true);
 }
 $uploader->save($path . DS, $fileName );
 } catch (Exception $e) {
 $error = true;
 }
 }
 }
 /**************************************************************/
 if ($error) {
 Mage::getSingleton('customer/session')->addError(Mage::helper('rga')->__('Unable to submit your request. Please, try again later'));
 session_write_close();
 throw new Exception();
 }
 $mailTemplate = Mage::getModel('core/email_template');
 /* @var $mailTemplate Mage_Core_Model_Email_Template */
 /**************************************************************/
 //sending file as attachment
 foreach($photos as $pic) {
 $attachmentFilePath = Mage::getBaseDir('media'). DS . 'rga' . DS . $pic;
 if(file_exists($attachmentFilePath)){
 $fileContents = file_get_contents($attachmentFilePath);
 $attachment = $mailTemplate->getMail()->createAttachment($fileContents);
 $attachment->filename = $pic;
 }
 }
 /**************************************************************/
 $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()) {
 Mage::getSingleton('customer/session')->addError(Mage::helper('rga')->__('Unable to submit your request. Please, try again later'));
 session_write_close();
 throw new Exception();
 }
 $translate->setTranslateInline(true);
 Mage::getSingleton('core/session')->addSuccess(Mage::helper('rga')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
 session_write_close();
 $this->_redirect('*/*/');
 return;
 } catch (Exception $e) {
 $translate->setTranslateInline(true);
 Mage::getSingleton('core/session')->addError(Mage::helper('rga')->__('Unable to submit your request. Please, try again later'));
 session_write_close(); 
 $this->_redirect('*/*/');
 return;
 }
 } else {
 $this->_redirect('*/*/');
 }
 }
You can go through it and modify it according to your requirement.
I hope this will be helpful.
- 
 I solved it with this solution - stackoverflow.com/questions/22609709/… - But I will try to implement your solution to keep images in email attachmentCarsten– Carsten2017年11月27日 08:53:16 +00:00Commented Nov 27, 2017 at 8:53
- 
 Where to add this code and how it will be execute?Shoaib Saleem– Shoaib Saleem2019年02月22日 10:49:32 +00:00Commented Feb 22, 2019 at 10:49
Explore related questions
See similar questions with these tags.