Magento:如何在magento中发送带有附件的联系表单电子邮件?
Magento 中是否有用于需要发送文件附件的简单联系表单的默认功能?还是需要用Zend的邮件功能来定制?任何帮助或建议将不胜感激。
Is there any default functionality in Magento for a simple contact form that needs to send a file attachment? Or do I need to customise it with Zend's mail function? Any help or suggestions would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(5)
池木2024-11-26 18:45:00
要向电子邮件添加附件,您需要使用以下 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 电子邮件附件
毁我热情2024-11-26 18:45:00
じ违心2024-11-26 18:45:00
与 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 => "[email protected]"))
->setTo(array($email => "$fname $lname"))
->setBody($body);
$message->attach(Swift_Attachment::fromPath("../../invoices_unpaid/$id.pdf"));
$result = $mailer->send($message);
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在联系表单中添加文件按钮
为了避免编辑 phtml 文件,我喜欢使用事件:
在 config.xml 中创建观察者:
<前><代码><事件>;
;
联系人附件/观察者
<观察者>
<类型>单例
<方法>addFileBoton
观察者:
块类(您刚刚在观察者中实例化):
.phtml 文件,您可以在其中将 enctype 属性添加到表单并添加文件输入:
<前><代码>getPassedTransport();
$originalForm = str_replace('action', 'enctype="multipart/form-data" action', $originalForm);
$lastListItem = strrpos($originalForm, '
echo substr($originalForm, 0, $lastListItem);
?>
处理附件
您需要重写 Magento 的 Contacts IndexController 以将文件上传到您想要的位置并添加电子邮件中的链接。
配置.xml:
控制器:
<前><代码>getRequest()->getPost();
如果($post){
$translate = Mage::getSingleton('core/translate');
/* @var $translate Mage_Core_Model_Translate */
$translate->setTranslateInline(false);
尝试 {
$postObject = new Varien_Object();
$postObject->setData($post);
$错误=假;
if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
$错误=真;
}
if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
$错误=真;
}
if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
$错误=真;
}
if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
$错误=真;
}
如果($错误){
抛出新的异常();
}
//上传附件
尝试 {
$uploader = new Mage_Core_Model_File_Uploader('附件');
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(true);
$uploader->setAllowCreateFolders(true);
$结果 = $上传者->保存(
法师::getBaseDir('media') 。 DS。 '联系附件' 。 DS
);
$fileUrl = str_replace(Mage::getBaseDir('media') . DS, Mage::getBaseUrl('media'), $result['path']);
} catch (异常$e) {
Mage::getSingleton('customer/session')->addError(Mage::helper('contactattachment')->__('文件上传出现问题'));
$this->_redirect('*/*/');
返回;
}
$mailTemplate = Mage::getModel('core/email_template');
/* @var $mailTemplate Mage_Core_Model_Email_Template */
$mailTemplate->setDesignConfig(array('area' => 'frontend'))
->setReplyTo($post['email'])
-> 发送事务(
Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
法师::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
无效的,
数组('数据' => $postObject)
);
if (!$mailTemplate->getSentSuccess()) {
抛出新的异常();
}
$translate->setTranslateInline(true);
Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('您的询问已提交,我们将尽快回复,感谢您与我们联系.'));
$this->_redirect('*/*/');
返回;
} catch (异常$e) {
$translate->setTranslateInline(true);
Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('无法提交您的请求。请稍后重试'));
$this->_redirect('*/*/');
返回;
}
} 别的 {
$this->_redirect('*/*/');
}
}
}
在控制器中,您需要将
$fileUrl
添加到电子邮件模板中,并在您的电子邮件模板文件上回显它。我认为这就是全部内容,如果您遇到问题,请告诉我。
干杯
add file button in the contact form
To avoid editing the phtml file, I like to use events:
create observer in config.xml:
the observer:
the block class (that you've just instanciated in the observer):
the .phtml file, where you add the enctype attribute to the form and add the file input:
procesing the attachment
you need to rewrite Magento's Contacts IndexController to upload the file where you want to and add the link in the email.
config.xml:
the controller:
in the controller you need to add the
$fileUrl
into the email template, and on your email template file you need to echo it.I think this is the whole thing, let me know if you're having troubles with that.
cheers