1

I cannot send the email by clicking on the button 'email me'.It is redirecting and showing an empty page.Please anyone help me

my code form.phtml

<div>
<b> Hello <?php echo $_POST["name"]; ?>!</b><br>
<b>Email    :</b> <?php echo $_POST["email"]; ?>.<br>
<b>Gender   :</b> <?php echo $_POST["gender"]; ?>.<br>
<b>Birthday :</b>
<?php
     $day = $_POST['day'];
     $month = $_POST['month'];
     $year = $_POST['year'];
     $date = $day."-".$month."-".$year;
     $my_date = date('d-m-Y', strtotime($date));
     echo $my_date;
?>
</div>



<div>
<form method="post" action="<?php echo $this->getPostActionUrl()?>" id="form-validate">
<table border="1" style="width:100%">

<tr>
<th id= "sno" style="font-family: sans-serif; font-size: 100%; font-weight: bold;" class="bg-color" width="5%">S.No</th>
<th id= "vaccine" style="font-family: sans-serif; font-size: 100%; font-weight: bold;" class="bg-color center" width="32%">Vaccine</th>     
<th id="decsription" style="font-family: sans-serif; font-size: 100%; font-weight: bold;">Description</th>
<th id="duedate" style="font-family: sans-serif; font-size: 100%; font-weight: bold;" class ="bg-color" width="15%">Due Date</th>

 </tr>
<tr>
<td>1</td>
<td><a href="http://www.labwise.in/devel/hepatitis-b/" style="color: rgb(0,255,0)"><font color="#0000FF">Hepatitis B</font></a>
</td>       
<td>

        <b><span> At birth: </span></b><br></br>
        <span>For infants born to hepatitis B surface antigen (HBsAg)-positive mothers, administer HepB vaccine and 0.5 mL of hepatitis B immune globulin (HBIG) within 12 hours of birth. These infants should be tested for HBsAg and antibody to HBsAg (anti-HBs) 1 to 2 months after completion of the HepB series at age 9 through 18 months (preferably at the next well-child visit).</span>

</td>
<td> 
<div id="duedtdiv">

<div class="input-box dob" id="duedtdiv">
<?php

      $date=date_create("now");
      $bdate=date_create($my_date);

      //echo date_format($dueDate,'d-m-Y');

        if($date >= $bdate)
        {
            $dueDate=$bdate->modify('+6 week');
            echo date_format($dueDate,"d-m-Y");
        }
        else
        {
            $bdate->modify('+2 week');
            echo date_format($bdate,'d-m-Y');
        }
?>

</div>


</div>

</td>
 </tr>
 </table>
</form>
       <button type="submit" title="<?php echo $this->__('Save Form') ?>" class="button"><span><span><?php echo $this->__('Save Form') ?></span></span> </button></div>

 </div>
&nbsp; 
 <div>
      <button type="submit" title="<?php echo Mage::helper('vaccination')->__('Email Me') ?>" class="button"><span><span><?php echo Mage::helper('vaccination')->__('Email Me')?></span></span>   </button>
 </div>
    <script type="text/javascript">

   //<![CDATA[
       var dataForm = new VarienForm('form-validate', true);

   //]]>
   </script>

IndexController.php

  <?php

    class Labwise_Vaccination_IndexController extends Mage_Core_Controller_Front_Action
{

const XML_PATH_EMAIL_RECIPIENT  = 'contacts/customemail/recipient_email';
const XML_PATH_EMAIL_SENDER     = 'contacts/customemail/sender_email_identity';
const XML_PATH_EMAIL_TEMPLATE   = 'contacts/customemail/email_template';
const XML_PATH_ENABLED          = 'contacts/vaccination/enabled';

public function preDispatch()
{
    parent::preDispatch();

    if( !Mage::getStoreConfigFlag(self::XML_PATH_ENABLED) ) {
        $this->norouteAction();
    }
}

public function indexAction()
{
    $this->loadLayout();
    $this->getLayout()->getBlock('vaccinationform')
        ->setFormAction( Mage::getUrl('*/*/send') );

    $this->_initLayoutMessages('customer/session');
    $this->_initLayoutMessages('catalog/session');
    $this->renderLayout();
}

public function sendAction()
{
    $post = $this->getRequest()->getPost();
    if ( $post ) {
        $translate = Mage::getSingleton('core/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['email']), 'EmailAddress')) {
                $error = true;
            }

            if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                $error = true;
            }

            if ($error) {
                throw new Exception();
            }
            Zend_Debug::dump($postObject); 
            die();
            $mailTemplate = Mage::getModel('core/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('vaccination')->__('Thanks for your enquiry. We will be in touch shortly!'));
            $this->_redirect('*/*/');

            return;
        } catch (Exception $e) {
            $translate->setTranslateInline(true);

            Mage::getSingleton('customer/session')->addError(Mage::helper('vaccination')->__('Sorry, we were unable to submit your enquiry.'));
            $this->_redirect('*/*/');
            return;
        }

    } else {
        $this->_redirect('*/*/');
    }
}

}
 ?>

email template:

<?php
 $installer = $this;
 $installer->startSetup();

 $installer->run("
  INSERT INTO {$this->getTable('core_email_template')} (`template_code`, `template_text`, `template_type`, `template_subject`, `template_sender_name`, `template_sender_email`, `added_at`, `modified_at`) VALUES
('Vaccination', 'Child Name: {{var data.name}}\r\nE-mail: {{var data.email}}\r\nMobile Number: {{var data.mobile}}', 1, 'Vaccination', NULL, NULL, NOW(), NOW());
");
 $installer->endSetup();
  • Please anyone help me.I need it very soon. – Annaram Ravinder Aug 13 '15 at 5:59
1

Inchoo has an excellent post on this: http://inchoo.net/magento/magento-custom-emails/

No need for an installer even, if you follow the instructions you should be able to send an email.

From the post: In your controller you should have something like this where you would fill the $emailTemplateVariables with your post data

...
/*
 * Loads the html file named 'custom_email_template1.html' from
 * app/locale/en_US/template/email/activecodeline_custom_email1.html
 */ 
$emailTemplate  = Mage::getModel('core/email_template')
                        ->loadDefault('custom_email_template1');                                    

//Create an array of variables to assign to template
$emailTemplateVariables = array();
$emailTemplateVariables['myvar1'] = 'Branko';
$emailTemplateVariables['myvar2'] = 'Ajzele';
$emailTemplateVariables['myvar3'] = 'ActiveCodeline';

/**
 * The best part :)
 * Opens the activecodeline_custom_email1.html, throws in the variable array 
 * and returns the 'parsed' content that you can use as body of email
 */
$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);

/*
 * Or you can send the email directly, 
 * note getProcessedTemplate is called inside send()
 */
$emailTemplate->send('john@someemail.com','John Doe', $emailTemplateVariables);
...

In your config.xml you assign a template

<global>      
    <template>
        <email>
            <custom_email_template1 module="SampleModule1">
                <label>ActiveCodeline custom email module</label>
                <file>activecodeline_custom_email1.html</file>
                <type>html</type>
            </custom_email_template1>
        </email>
    </template>
</global>  
...

And create the email template under app/locale/[language]/template/email/[templatefile].html

<!--@subject ActiveCodeline custom email module @-->

<div>
<h1>ActiveCodeline custom email example by Branko Ajzele</h1>
<p>Hi there {{var myvar1}} {{var myvar2}} from {{var myvar3}}. This is just some example template to test custom email module.</p>
</div>

Where you can reference the keys in your template variable array in the controller via the syntax {{var [keyname]}}

  • can you please check my code and give me suggestions. – Annaram Ravinder Aug 17 '15 at 6:31
  • Did you follow the code in the link? That works, i'd suggest working from there to adapt it to your needs – Sander Mangel Aug 17 '15 at 6:52
  • Yes I followed the code in the link but,when i click on the button it is going to default page and not getting any mail – Annaram Ravinder Aug 17 '15 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.