Magento Contacts添加上传文件当邮件附件
来源:互联网 发布:adobe reader for mac 编辑:程序博客网 时间:2024/05/16 10:43
简单修改几步,即可让magento的联系我们可以上传文件作为发送邮件的附件。
步骤:
- 修改模板文件“template/contacts/form.phtm”,
form修改为
<form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post" enctype="multipart/form-data">
再在comment文本框的下面添加
<li> <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="attachment" id="attachment" class="input-text" type="file" /> </div></li>
- 添加控制器逻辑代码。
自定义一个新模块,在新模块的配置文件/app/code/local/CsjCg/Customcontact/etc/config.xml里定义如下:
<frontend> <routers> <contacts> <args> <modules> <csjcg_customcontact before="Mage_Contacts">CsjCg_Customcontact</csjcg_customcontact> </modules> </args> </contacts> </routers></frontend>
模块的controller文件/app/code/local/CsjCg/Customcontact/controllers/IndexController.php
<?php/** * * @category Mage * @package Mage_Adminhtml * @author Sjolzy <chen@sjolzy.cn> */require_once Mage::getModuleDir('controllers', 'Mage_Contacts') . DS . 'IndexController.php';class CsjCg_Customcontact_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('doc', 'docx','pdf','xls', 'xlsx','csv')); $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) { $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; } /**************************************************************/ $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('*/*/'); } }}
有限制了上传格式,及将附件保存在/media/contacts/
- 完成Magneto联系我们上传文件.
阅读全文
0 0
- Magento Contacts添加上传文件当邮件附件
- 类似163邮件附件选择文件上传
- magento 文件上传类
- 添加Magento新布局文件
- magento 后台添加图片上传功能
- Magento多文件上传代码功能实现
- Magento后台新建表单上传文件
- 邮件附件
- 邮件附件
- 以邮件附件形式发送文件
- ionic添加cordova插件-Contacts
- 如何: 保存到文件的邮件附件
- 添加文件上传的js
- Contacts
- Contacts
- Html5添加phonegap--Contacts插件教程
- 【Contacts】联系人界面添加联系人统计项
- 添加多个邮件附件页面。只有视图层。
- 机器学习与神经网络(三):自适应线性神经元的介绍和Python代码实现
- 【study】linux 进程通信之管道
- 王永杰应聘前端开发岗位个人简历-Tel:17633559293
- 文章标题
- 机器学习实战-朴素贝叶斯笔记
- Magento Contacts添加上传文件当邮件附件
- matplotlib基础教程(1)
- 面向对象(四)——继承
- 基于tomcat的webscoket应用
- AndroidStudio最完整配置SVN
- Java 易遗漏知识点
- vs 2015 Commmunity 每次启动都提示Key Required解决方案
- 【Java】边框总结
- 如何上手深度学习中的图像处理?有这个代码资源库就够了