sending mail in magento

How to send email in magento writing an action in index controller?

my index controller;

public function postAction()

    $post = $this->getRequest()->getPost();     
    if(!$post) exit;
    $translate = Mage::getSingleton('core/translate');
    try {
            $postObject = new Varien_Object();
            if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                echo '<div class="error-msg">'.Mage::helper('contacts')->__('Please enter a valid email address. For example').'</div>';
            $storeId = Mage::app()->getStore()->getStoreId();
            $emailId = Mage::getStoreConfig(self::XML_PATH_SAMPLE_EMAIL_TEMPLATE);
            $mailTemplate = Mage::getModel('core/email_template');              
            $mailTemplate->setDesignConfig(array('area'=>'frontend', 'store'=>$storeId))
                ->sendTransactional($templateId, $sender, $email, $name, $vars=array(), $storeId=null)

            if (!$mailTemplate->getSentSuccess()) {                 
                echo '<div class="error-msg">'.Mage::helper('contacts')->__('Unable to submit your request. Please, try again later.').'</div>';
            echo '<div class="success-msg">'.Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.').'</div>';
            catch (Exception $e) {
            echo '<div class="error-msg">'.Mage::helper('contacts')->__('Unable to submit your request. Please, try again later.').$e.'</div>';

is there any wrong.. please help me to out of this.. Thanks in advance..


It looks like there are a few problems with the way you are calling sendTransactional(). First off, $templateId is not defined, it looks like you've actually stored the template id in $emailId. Also, $sender, $email, and $name are undefined. You can try something like this:

->sendTransactional($emailId, 'general', $post['email'], "Need a send to name here")

This is only going to work if you are getting a valid template id back from your call to getStoreConfig(). You'll also need to set the last name param correctly.

There could be other issues, but that's what I noticed with a quick glance anyway.

Here's another way, if you don't need templates. Call from a controller.

$body = "Hi there, here is some plaintext body content";
$mail = Mage::getModel('core/email');
$mail->setToName('John Customer');
$mail->setSubject('The Subject');
$mail->setFromName("Your Name");
$mail->setType('text');// You can use 'html' or 'text'

try {
    Mage::getSingleton('core/session')->addSuccess('Your request has been sent');
catch (Exception $e) {
    Mage::getSingleton('core/session')->addError('Unable to send.');

Finally i created a function for sending mail by using zend

public function sendMail()
        $post = $this->getRequest()->getPost();     
        if ($post){

                $to_email = $this->getRequest()->getParam("email");
                $to_name = 'Hello User';
                $subject = ' Test Mail- CS';
                $Body="Test Mail Code : "; 

                $sender_email = "";
                $sender_name = "sender name";

                $mail = new Zend_Mail(); //class for mail
                $mail->setBodyHtml($Body); //for sending message containing html code
                $mail->setFrom($sender_email, $sender_name);
                $mail->addTo($to_email, $to_name);
                //$mail->addCc($cc, $ccname);    //can set cc
                //$mail->addBCc($bcc, $bccname);    //can set bcc
                $msg  ='';
                try {
                         $msg = true;
                catch(Exception $ex) {
                        $msg = false;
                        //die("Error sending mail to $to,$error_msg");

Need Your Help

Why doesn't java.util.Set have get(int index)?

java data-structures collections set

I'm sure there's a good reason, but could someone please explain why the java.util.Set interface lacks get(int Index), or any similar get() method?

Cannot find module 'karma' while using grunt

angularjs npm gruntjs yeoman bower

I am using Yeoman toolkit to bootstrap an Angular web application. I've followed all the steps mentioned here. But still, apparently, grunt can not find the karma module. It spits out this error: