How to send email in magento writing an action in index controller?
my index controller;
public function postAction()
{
$post = $this
Here's another way, if you don't need templates. Call from a controller.
<?php
$body = "Hi there, here is some plaintext body content";
$mail = Mage::getModel('core/email');
$mail->setToName('John Customer');
$mail->setToEmail('customer@email.com');
$mail->setBody($body);
$mail->setSubject('The Subject');
$mail->setFromEmail('yourstore@url.com');
$mail->setFromName("Your Name");
$mail->setType('text');// You can use 'html' or 'text'
try {
$mail->send();
Mage::getSingleton('core/session')->addSuccess('Your request has been sent');
$this->_redirect('');
}
catch (Exception $e) {
Mage::getSingleton('core/session')->addError('Unable to send.');
$this->_redirect('');
}
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.
Finally i created a function for sending mail by using zend
public function sendMail() { $post = $this->getRequest()->getPost(); if ($post){ $random=rand(1234,2343); $to_email = $this->getRequest()->getParam("email"); $to_name = 'Hello User'; $subject = ' Test Mail- CS'; $Body="Test Mail Code : "; $sender_email = "sender@sender.com"; $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 $mail->setSubject($subject); $msg =''; try { if($mail->send()) { $msg = true; } } catch(Exception $ex) { $msg = false; //die("Error sending mail to $to,$error_msg"); } $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($msg)); } }