Magento - 通过动作发送电子邮件的问题

时间:2011-02-16 15:02:11

标签: magento magento-1.4

我有一个基本模块,它接受前端的表单数据并保存到数据库。

我还要做的是,如果数据已成功保存,则发送电子邮件。

我希望将电子邮件发送给店主:

我已经做了以下尝试让这个工作:

应用程序/代码/本地/航空/ Catalogrequest的/ etc / config.xml中

<template>
  <email>
    <request_brochure_email translate="label" module="Aero_Catalogrequest">
      <label>Request a brochure</label>
        <file>request_brochure.html</file>
          <type>html</type>
    </request_brochure_email>
</email>
</template>

<default>
  <Aero_Catalogrequest>
    <sendemail>
      <template>request_brochure_email</template>
    </sendemail>
  </Aero_Catalogrequest>
</default>

我在:app / locale / en_US / template / email / request_brochure.html

中创建了一个模板文件
   <p>
     {{var data.fname}}
   </p>

这是我的IndexController.php中的postAction

public function postAction()
{
    $post = $this->getRequest()->getPost();
    if ( $post ) {
        $request = Mage::getModel('catalogrequest/catalogrequest');
        $data = $this->getRequest()->getPost();
        $data['time_added'] = now();
        $data['country'] = $data['country_id'];
        $data['ip'] = $_SERVER['REMOTE_ADDR'];
        $data['fname'] = ucwords($data['fname']);
        $data['lname'] = ucwords($data['lname']);
        $data['address1'] = ucwords(str_replace(",", " ",$data['address1']));
        $data['address2'] = ucwords(str_replace(",", " ",$data['address2']));
        $data['city'] = ucwords($data['city']);
        if(empty($data['region'])){
            $data['state'] = Mage::getModel('directory/region')->load($data['state'])->getCode();
        } else {
            $data['state'] = $data['region'];
        }
            $postObject = new Varien_Object();
            $postObject->setData($post);

        // Validate
        if(!$errors = $request->validate($data)){
            MAGE::getSingleton('core/session')->addError($errors);
        }


        // Add to database
        try {
            $mailTemplate = Mage::getModel('core/email_template');
            /* @var $mailTemplate Mage_Core_Model_Email_Template */
            $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                ->sendTransactional(
                    Mage::getStoreConfig(self::XML_PATH_SAMPLE_EMAIL_TEMPLATE),
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                    Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                    null,
                    array('data' => $data)
                );

            if (!$mailTemplate->getSentSuccess()) {
                throw new Exception();
            }

            $request->setData($data)->save();
            MAGE::getSingleton('core/session')->addSuccess($this->__('<h2>Thank you</h3> You can expect to receive your catalogue in 10-14 days.o. '));

            $translate->setTranslateInline(true);
            $this->_redirect('*/*/thanks');

        } catch(Exception $e){
            MAGE::getSingleton('core/session')->addError('Sorry, we\'ve had some trouble saving your request');
            $this->_redirect('*/*/');
            return;
        }

    }
    return;
}

提交表单时,数据不会发送到我在管理员

中配置的电子邮件地址

有谁知道我做错了什么?

由于

2 个答案:

答案 0 :(得分:4)

我认为你需要设置一个主题,否则你不会通过:

public function isValidForSend()
{
    return !Mage::getStoreConfigFlag('system/smtp/disable')
        && $this->getSenderName()
        && $this->getSenderEmail()
        && $this->getTemplateSubject();
}

(Mage_Core_Model_Email_Template)

因此,在您的电子邮件模板中,添加:<!--@subject The subject of email @-->

希望有所帮助

答案 1 :(得分:0)

好的,为了让它起作用,我不得不更换:

array('data' => $data)

array('data' => $postObject)

现在所有发送完美!

全部谢谢