gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:55:24 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com