How to send email using Magento

simple function to send the email in magento

<?php
    public function sendMailAction() 
    {
        $html="
        put your html content here
        blah blah

        ";
        $mail = Mage::getModel('core/email');
        $mail->setToName('Your Name');
        $mail->setToEmail('Youe Email');
        $mail->setBody('Mail Text / Mail Content');
        $mail->setSubject('Mail Subject');
        $mail->setFromEmail('Sender Mail Id');
        $mail->setFromName("Msg to Show on Subject");
        $mail->setType('html');// You can use Html or text as Mail format
        $mail->setBodyHTML($html);  // your content or message

        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('');
        }
    }
?>

Reference


Create New Template Form "Transactional Emails".

hello {{var customerName}},
  You received test template. 
Thank you

After create New Template Note Its ID

Create controller action

public function sendEnquiry()
{
$customer = Mage::getSingleton('customer/session')->getCustomer();

$templateId = 8; // Enter you new template ID
$senderName = Mage::getStoreConfig('trans_email/ident_support/name');  //Get Sender Name from Store Email Addresses
$senderEmail = Mage::getStoreConfig('trans_email/ident_support/email');  //Get Sender Email Id from Store Email Addresses
$sender = array('name' => $senderName,
            'email' => $senderEmail);

// Set recepient information
$recepientEmail = $customer->getEmail();
$recepientName = $customer->getName();      

// Get Store ID     
$store = Mage::app()->getStore()->getId();

// Set variables that can be used in email template
$vars = array('customerName' => $customer->getName());  


// Send Transactional Email
Mage::getModel('core/email_template')
    ->sendTransactional($templateId, $sender, $recepientEmail, $recepientName, $vars, $storeId);

Mage::getSingleton('core/session')->addSuccess($this->__('We Will Contact You Very Soon.'));
}

Now You can send simple mail using Admin "Transactional Emails".

Following Your your_form.phtml

<form action="<?php echo $this->getUrl("your_module_name/index/sendEnquiry")?>" id="discuss" method="post">

//Your form 

</form>

Try this code and adjust it accordingly

$email_template  = Mage::getModel('core/email_template')
    ->loadDefault($template_id);

/* load template by id */
$email_template_variables = array(
    'customer_name' => $customer_name);

$sender_email = '[email protected]';
$sender_name =  'Your Friend at The Company';                          
$email_template->setSenderName($sender_name);
$email_template->setSenderEmail($sender_email); 

$email_template->send(
    $email_to, $customer_name,$email_template_variables
);

Tags:

Magento 1.8