玛根托 : How to send contact form email with attachment in magento?

标签 magento email-attachments zend-mail

Magento 中是否有任何默认功能可用于需要发送文件附件的简单联系表单?还是需要用Zend的邮件功能来定制?任何帮助或建议将不胜感激。

最佳答案

在联系表单中添加文件按钮

为了避免编辑 phtml 文件,我喜欢使用事件:

  1. 在config.xml中创建观察者:

    <events>
        <core_block_abstract_to_html_after>
            <observers>
                <add_file_boton>
                    <type>singleton</type>
                    <class>contactattachment/observer</class>
                    <method>addFileBoton</method>
                </add_file_boton>
            </observers>
        </core_block_abstract_to_html_after>
    </events>
    
  2. 观察者:

    class Osdave_ContactAttachment_Model_Observer
    {
        public function addFileBoton($observer)
        {
            $block = $observer->getEvent()->getBlock();
            $nameInLayout = $block->getNameInLayout();
    
            if ($nameInLayout == 'contactForm') {
                $transport = $observer->getEvent()->getTransport();
                $block = Mage::app()->getLayout()->createBlock('contactattachment/field');
                $block->setPassingTransport($transport['html']);
                $block->setTemplate('contactattachment/field.phtml')
                        ->toHtml();
            }
            return $this;
        }
    }
    
  3. block 类(您刚刚在观察者中实例化):

    class Osdave_ContactAttachment_Block_Field extends Mage_Core_Block_Template
    {
        private $_passedTransportHtml;
    
        /**
         * adding file select field to contact-form
         * @param type $transport
         */
        public function setPassingTransport($transport)
        {
            $this->_passedTransportHtml = $transport;
        }
    
        public function getPassedTransport()
        {
            return $this->_passedTransportHtml;
        }
    }
    
  4. .phtml 文件,您可以在其中将 enctype 属性添加到表单并添加文件输入:

    <?php
    $originalForm = $this->getPassedTransport();
    $originalForm = str_replace('action', 'enctype="multipart/form-data" action', $originalForm);
    $lastListItem = strrpos($originalForm, '</li>') + 5;
    echo substr($originalForm, 0, $lastListItem);
    ?>
    <li>
        <label for="attachment"><?php echo $this->__('Select an attachment:') ?></label>
        <div class="input-box">
            <input type="file" class="input-text" id="attachment" name="attachment" />
        </div>
    </li>
    <?php
    echo substr($originalForm, $lastListItem);
    ?>
    

    处理附件

您需要重写Magento的Contacts IndexController以将文件上传到您想要的位置并在电子邮件中添加链接。

  • config.xml:

        <global>
            ...
            <rewrite>
                <osdave_contactattachment_contact_index>
                    <from><![CDATA[#^/contacts/index/#]]></from>
                    <to>/contactattachment/contacts_index/</to>
                </osdave_contactattachment_contact_index>
            </rewrite>
            ...
        </global>
    
        <frontend>
            ...
            <routers>
                <contactattachment>
                    <use>standard</use>
                    <args>
                        <module>Osdave_ContactAttachment</module>
                        <frontName>contactattachment</frontName>
                    </args>
                </contactattachment>
            </routers>
            ...
        </frontend>
    
  • Controller :

    <?php
    
    /**
     * IndexController
     *
     * @author david
     */
    
    require_once 'Mage/Contacts/controllers/IndexController.php';
    class Osdave_ContactAttachment_Contacts_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;
                    }
    
                    if ($error) {
                        throw new Exception();
                    }
    
                    //upload attachment
                    try {
                        $uploader = new Mage_Core_Model_File_Uploader('attachment');
                        $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
                        $uploader->setAllowRenameFiles(true);
                        $uploader->setAllowCreateFolders(true);
                        $result = $uploader->save(
                            Mage::getBaseDir('media') . DS . 'contact_attachments' . DS
                        );
                        $fileUrl = str_replace(Mage::getBaseDir('media') . DS, Mage::getBaseUrl('media'), $result['path']);
                    } catch (Exception $e) {
                        Mage::getSingleton('customer/session')->addError(Mage::helper('contactattachment')->__('There has been a problem with the file upload'));
                        $this->_redirect('*/*/');
                        return;
                    }
    
                    $mailTemplate = Mage::getModel('core/email_template');
                    /* @var $mailTemplate Mage_Core_Model_Email_Template */
                    $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('*/*/');
            }
        }
    }
    

在 Controller 中,您需要将 $fileUrl 添加到电子邮件模板中,并在电子邮件模板文件上回显它。
我认为这就是全部内容,如果您遇到问题,请告诉我。
干杯

关于玛根托 : How to send contact form email with attachment in magento?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6665558/

相关文章:

mysql - Magento 重新索引目录搜索索引锁定等待超时超时

ruby-on-rails - Magento API : get products from a category

c# - 通过电子邮件发送 log4net 日志作为 System.Net.Mail.Attachment 抛出 IOException(进程锁定)

c# - 如何删除作为附件的图像但显示在电子邮件正文中

php - 不阻塞发送邮件 'execution'

php - 尝试使用Zend Mail发送电子邮件时遇到解析错误?为什么?

php - Magento 或 Zend 是否缓存数据库连接字符串?

python - 如何使用 python 解码此附件文件名?

php - 邮件正文中带有法语字符的 Zend_Mail

magento - Magento 属性集观察者保存后、删除