Magento:在联系表格中添加附件

时间:2016-03-24 23:14:06

标签: magento

我跟着很多帖子解决了我的问题...我想在magento联系表单中添加附件。我这样做了。 在代码/ etc / modulex中创建名为 MagePsycho_Customcontact.xml 的模块:

<?xml version="1.0"?>
<config>
<modules>
    <MagePsycho_Customcontact>
        <active>true</active>
        <codePool>local</codePool>
    </MagePsycho_Customcontact>
</modules>
</config>

然后在app / code / local / MagePsycho / Customcontact / etc中创建文件config.xml

<frontend>
<routers>
    <contacts>
        <args>
            <modules>
                <MagePsycho_Customcontact  before="Mage_Contacts">MagePsycho_Customcontact</MagePsycho_Customcontact>
            </modules>
        </args>
    </contacts>
</routers>
</frontend>
</config>

然后控制器在code / local / MagePsycho / Customcontact / controllers中,名称为IndexController.php

<?php
require_once Mage::getModuleDir('controllers', 'Mage_Contacts') . DS . 'IndexController.php';
class MagePsycho_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', '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);

            $mailTemplate->getMail()->createAttachment($fileContents,

            Zend_Mime::TYPE_OCTETSTREAM,

            Zend_Mime::DISPOSITION_ATTACHMENT,

            Zend_Mime::ENCODING_BASE64,

            $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('*/*/');
    }
}
}

这是位于app / design / frontend / sm-shoppystore / default / template / contacts / form.phtml中的表单

<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE_AFL.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category    design
* @package     base_default
* @copyright   Copyright (c) 2012 Magento Inc.   (http://www.magentocommerce.com)
* @license     http://opensource.org/licenses/afl-3.0.php  Academic  Free License (AFL 3.0)
*/
?>
<div id="messages_product_view"><?php echo   $this->getMessagesBlock()->getGroupedHtml() ?></div>

<div class="row box-1-contact">
<div class="contact-map-wrap col-lg-6 col-md-6 col-sm-6 col-xs-12">
    <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('google-map')->toHtml() ?>
</div>
<form class="contact-form col-lg-6 col-md-6 col-sm-6 col-xs-12" action="<?php echo Mage::getUrl(); ?>contacts/index/post/" id="contactForm" method="post" enctype="multipart/form-data">
    <div class="form-wrapper">
        <ul class="form-list">
            <li class="fields">
                <div class="field">
                    <div class="input-box">
                        <input name="name" id="name" title="<?php echo Mage::helper('contacts')->__('Your name') ?>" onfocus="if(this.value=='<?php echo Mage::helper('contacts')->__('Your name...') ?>') this.value='';" onblur="if(this.value=='') this.value='<?php echo Mage::helper('contacts')->__('Your name...') ?>';" value="<?php echo ($this->helper('contacts')->getUserName())?$this->htmlEscape($this->helper('contacts')->getUserName()):Mage::helper('contacts')->__('Your name...'); ?>" class="input-text required-entry" type="text" />
                    </div>
                </div>
                <div class="field">
                    <div class="input-box">
                        <input name="email" id="email" title="<?php echo Mage::helper('contacts')->__('Your email') ?>" onfocus="if(this.value=='<?php echo Mage::helper('contacts')->__('Your email...') ?>') this.value='';" onblur="if(this.value=='') this.value='<?php echo Mage::helper('contacts')->__('Your email...') ?>';" value="<?php echo ($this->helper('contacts')->getUserEmail())?$this->htmlEscape($this->helper('contacts')->getUserEmail()):Mage::helper('contacts')->__('Your email...'); ?>" class="input-text required-entry validate-email" type="text" />
                    </div>
                </div>
            </li>
            <li>
                <div class="input-box field">
                    <input name="subject" id="subject" title="<?php echo Mage::helper('contacts')->__('Title') ?>" onfocus="if(this.value=='<?php echo Mage::helper('contacts')->__('Subject') ?>') this.value='';" onblur="if(this.value=='') this.value='<?php echo Mage::helper('contacts')->__('Subject') ?>';" value="<?php echo ($this->helper('contacts')->getUserEmail())?$this->htmlEscape($this->helper('contacts')->getUserEmail()):Mage::helper('contacts')->__('Subject'); ?>" class="input-text" type="text" />  
                </div>
            </li>
            <li class="wide">
                <div class="input-box">
                    <textarea name="comment" id="comment" title="<?php echo Mage::helper('contacts')->__('Comment') ?>" onfocus="if(this.value=='<?php echo Mage::helper('contacts')->__('Comment...') ?>') this.value='';" onblur="if(this.value=='') this.value='<?php echo Mage::helper('contacts')->__('Comment...') ?>';" value="<?php echo Mage::helper('contacts')->__('Comment...'); ?>" class="required-entry input-text" cols="10" rows="7"><?php echo Mage::helper('contacts')->__('Comment...') ?></textarea>
                </div>
            </li>
             <li>
                  <label for="attachment"><?php echo Mage::helper('contacts')->__('Allegato') ?></label>
              <div class="buttons-set">
                  <input name="MAX_FILE_SIZE" type="hidden" value="2000000" />
                  <input name="attachment" id="attachment" class="button" type="file" />
              </div>
           </li>
        </ul>
    </div>
    <div class="buttons-set">
        <input type="text" name="hideit" id="hideit" value="" style="display:none !important;" />
        <button type="submit" title="<?php echo Mage::helper('contacts')->__('Send Email') ?>" class="button"><span><span><?php echo Mage::helper('contacts')->__('Send Email') ?></span></span></button>
    </div>
</form>
<div style="clear:both; padding:15px 0px;">&nbsp;</div>

<?php echo $this->getChildHtml('info-store'); ?>

</div>


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

发送我正在使用SMTPPRo ...我不知道它是否可以相关。

它不起作用......我做错了什么?你能帮帮我吗?谢谢!

0 个答案:

没有答案