如何在Magento中获取邮件已读回执?
如何在Magento中设置邮件已读回执
你之前的代码没生效的原因是,Mage::getModel('core/email_template')的setData()方法是用来设置模板的自定义数据,而非直接添加邮件头信息。邮件头需要通过底层的Zend_Mail对象来配置,下面给你两种可行的解决方案:
方法一:发送单个邮件时添加回执头
在发送邮件前,先获取模板对应的Zend_Mail实例,手动添加Disposition-Notification-To头即可:
// 初始化邮件模板模型 $mailTemplate = Mage::getModel('core/email_template'); // 配置邮件模板基础信息(根据你的需求修改) $mailTemplate->setTemplateId('your_template_identifier') // 替换为你的实际模板ID ->setSenderName('你的发件人名称') ->setSenderEmail('sender@yourdomain.com') ->setTemplateSubject('邮件主题'); // 获取Zend_Mail对象并添加已读回执头 $zendMail = $mailTemplate->getMail(); $zendMail->addHeader('Disposition-Notification-To', 'kadamas49@gmail.com'); // 发送邮件(替换为实际收件人信息和模板变量) $mailTemplate->send('recipient@example.com', '收件人名称', ['template_variable' => 'value']);
方法二:全局添加回执头(通过重写模型)
如果你希望Magento发送的所有邮件都自动携带已读回执,可以通过自定义模块重写core/email_template模型:
- 在自定义模块的
config.xml中添加重写规则:
<global> <models> <core> <rewrite> <email_template>YourNamespace_YourModule_Model_Email_Template</email_template> </rewrite> </core> </models> </global>
- 创建重写的模型文件
YourNamespace/YourModule/Model/Email/Template.php:
class YourNamespace_YourModule_Model_Email_Template extends Mage_Core_Model_Email_Template { public function send($email, $name = null, array $variables = array()) { // 先执行父类的发送逻辑 parent::send($email, $name, $variables); // 获取邮件对象并添加回执头 $zendMail = $this->getMail(); if ($zendMail) { $zendMail->addHeader('Disposition-Notification-To', 'kadamas49@gmail.com'); } return $this; } }
重要提醒
已读回执的生效完全取决于收件人的邮件客户端设置,大部分邮件客户端会弹出提示让用户选择是否发送回执,所以即使你正确配置了头信息,也无法保证一定能收到回执。
内容的提问来源于stack exchange,提问作者Ajaykumar Kadam
相关产品推荐
相关产品推荐

