Tuesday, July 26, 2011

How to send email easily in magento?

Here is a quick code to send email in Magento using the Zend_Mail class.
/**
 * Send email
 */
public function sendEmail()
{
    $fromEmail = "from@example.com"; // sender email address
    $fromName = "John Doe"; // sender name
 
    $toEmail = "to@example.com"; // recipient email address
    $toName = "Mark Doe"; // recipient name
 
    $body = "This is Test Email!"; // body text
    $subject = "Test Subject"; // subject text
 
    $mail = new Zend_Mail();       
    $mail->setBodyText($body);
    $mail->setFrom($fromEmail, $fromName);
    $mail->addTo($toEmail, $toName);
    $mail->setSubject($subject);
 
    try {
        $mail->send();
    }
    catch(Exception $ex) {
        // If you are using this code in your custom module use 'yourmodule' name.
        // If not, you may keep 'customer' instead of 'yourmodule'.
        Mage::getSingleton('core/session')
            ->addError(Mage::helper('yourmodule')
            ->__('Unable to send email.'));
    }
}

No comments:

Post a Comment