terrid25
terrid25

Reputation: 1946

Magento - problem sending email through an action

I have a basic module that accepts form data on the frontend and saves to a database.

What I'm also looking to do is to send an email if the data has been saved successfully.

I'd like the email to be sent to the store owner:

I have done the following to try and get this to work:

app/code/local/Aero/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>

and

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

I have created a template file in: app/locale/en_US/template/email/request_brochure.html

   <p>
     {{var data.fname}}
   </p>

And this is my postAction in my IndexController.php

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;
}

When submitting my form, data isn't sent to the email address that I have configured in the admin

Does anyone have any idea what I'm doing wrong?

Thanks

Upvotes: 0

Views: 2967

Answers (2)

terrid25
terrid25

Reputation: 1946

Ok, so to make it work, I had to replace:

array('data' => $data)

with

array('data' => $postObject)

All sends perfectly now!

Thanks all

Upvotes: 0

OSdave
OSdave

Reputation: 8586

I think you need to set a subject, otherwise you won't pass:

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

(Mage_Core_Model_Email_Template)

So, in your email template, add: <!--@subject The subject of email @-->

Hope that helps

Upvotes: 4

Related Questions