Magento中是否有需要发送文件附件的简单联系表单的默认功能?或者我需要使用Zend的邮件功能进行自定义吗?任何帮助或建议将不胜感激。
答案 0 :(得分:4)
为避免编辑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的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>
控制器:
<?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
添加到电子邮件模板中,并在电子邮件模板文件中将其回显。
我想这就是全部,如果你遇到麻烦,请告诉我
欢呼声
答案 1 :(得分:2)
要向电子邮件添加附件,您需要使用以下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 Email Attachments
答案 2 :(得分:0)
对默认功能不太确定,但这可能有所帮助 - http://ecommercesoftwaresolutionsonline.com/magento-enquiry-feedback-form-with-attachment-extension.html
答案 3 :(得分:0)
以下post描述了您可以轻松发送带附件的电子邮件:
答案 4 :(得分:0)
与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);