Magento 中是否有任何默认功能用于需要发送文件附件的简单联系表单?还是需要用 Zend 的邮件功能自定义?任何帮助或建议将不胜感激。
5 回答
在联系表单中添加文件按钮
为了避免编辑 phtml 文件,我喜欢使用事件:
在 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>
观察者:
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; } }
块类(您刚刚在观察者中实例化):
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; } }
.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 的联系人 IndexController 以将文件上传到您想要的位置并在电子邮件中添加链接。
配置.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>
控制器:
<?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('*/*/'); } } }
在控制器中,您需要将其添加$fileUrl
到电子邮件模板中,在您的电子邮件模板文件中,您需要回显它。
我想这就是全部,如果你遇到麻烦,请告诉我。
干杯
要向电子邮件添加附件,您需要使用以下 Zend_Mail 函数:
public function createAttachment($body,
$mimeType = Zend_Mime::TYPE_OCTETSTREAM,
$disposition = Zend_Mime::DISPOSITION_ATTACHMENT,
$encoding = Zend_Mime::ENCODING_BASE64,
$filename = null)
{
这是一个与 Magento 一起使用的示例,用于将 pdf 文件附加到电子邮件:
$myEmail = new new Zend_Mail('utf-8'); //see app/code/core/Mage/Core/Model/Email/Template.php - getMail()
$myEmail->createAttachment($file,'application/pdf',Zend_Mime::DISPOSITION_ATTACHMENT,Zend_Mime::ENCODING_BASE64,$name.'.pdf');
您可以使用此扩展来获取灵感:Fooman 电子邮件附件
不太确定默认功能,但这可能会有所帮助 - http://ecommercesoftwaresolutionsonline.com/magento-enquiry-feedback-form-with-attachment-extension.html
这是描述您可以多么轻松地发送带有附件的电子邮件的帖子:
与 Magento 无关,但我使用 PHP 的 SwiftMailer (http://swiftmailer.org/):
require_once('../lib/swiftMailer/lib/swift_required.php');
...
$body="Dear $fname,\n\nPlease find attached, an invoice for the period $startDate - $endDate\n\nBest regards,\n\nMr X";
$message = Swift_Message::newInstance('Subject goes here')
->setFrom(array($email => "no-reply@mydomain.com"))
->setTo(array($email => "$fname $lname"))
->setBody($body);
$message->attach(Swift_Attachment::fromPath("../../invoices_unpaid/$id.pdf"));
$result = $mailer->send($message);