Magento: Send email easily using Zend_Mail

Here is a quick function code to send email in Magento. I am 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) {
		// I assume you have your custom module. 
		// If not, you may keep 'customer' instead of 'yourmodule'.
		Mage::getSingleton('core/session')
			->addError(Mage::helper('yourmodule')
			->__('Unable to send email.'));
	}
}

Hope this helps. Thanks.