Magento: Email alert for customers

Adding users is faster and more efficient for eCommerce website. This is particularly helpful for companies who add large volumes of users on an ongoing basis. Sending email using a cron has numerous benefits for growing customers in the website. Many of our customers purchased product, but some customers are not logged in the website from long time or some customers are even not logged in the website. So i am writing this post which helps to send mail to not logged in customers.

1. Create mail template named ‘not_logged_in’ in Magento admin
2. Create not_logged_in.php file in your website and past below mentioned code in the file.

<?php

/**
 * Include Mage.php
 * Initialize Magento
 */

require_once (realpath(dirname(__FILE__) . '/../app/Mage.php'));
umask(0);
Mage::app('default');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);


/**
 *  Define variables
 */
$logFileName 	= 'cron_log_' . date('y-m-d') . '.log';
$storeId 	= Mage::app()->getStore()->getId();
$temlateName 	= 'not_logged_in';
$scheduleDay	= '30';

$paramDay 	= Mage::app()->getRequest()->getParam('day');
if($paramDay){
	$scheduleDay = $paramDay;
}

Mage::log('Schedule day : '.$scheduleDay, null, $logFileName);

// Load email template
Mage::log('Load email template : '.$temlateName, null, $logFileName);
$mailTemplate = Mage::getModel('core/email_template');
/* @var $mailTemplate Mage_Core_Model_Email_Template */
$translate  		= Mage::getSingleton('core/translate');
$template_collection=  $mailTemplate->loadByCode($temlateName);
$template_data 		= $template_collection->getData();
$templateId 		= $template_data['template_id'];
$mailSubject 		= $template_data['template_subject'];


//fetch sender data from Adminend > System > Configuration > Store Email Addresses > General Contact
$from_email 	= Mage::getStoreConfig('trans_email/ident_general/email'); //fetch sender email
$from_name 	    = Mage::getStoreConfig('trans_email/ident_general/name'); //fetch sender name

$sender = array('name'  => $from_name,
		'email' => $from_email);

$model 		= $mailTemplate->setReplyTo($sender['email'])->setTemplateSubject($mailSubject);

$customers = Mage::getModel('customer/customer')->getCollection();

foreach($customers as $customer){

	$customer = $customer->load($customer->getId());

	if(!empty($customer))
	{
			
		if(!empty($template_data))
		{

			$vars = array('customer'=>$customer); //for replacing the variables in email with data
			/*This is optional*/

			$email 	= $customer->getEmail();
			$name 	= $customer->getName();
			Mage::log($email, null, $logFileName);

			// calculate number of days

			$log = Mage::getModel('log/customer')->load($customer->getId());
				
			$now 		= time();
			$lastDate 	= strtotime($log->getLogoutAt());
			$datediff 	= $now - $lastDate;
			$totalDay  	= floor($datediff/(60*60*24));
				
			if($log->getLogId() && $totalDay > $scheduleDay && $now > $lastDate)
			{
				$model->sendTransactional($templateId, $sender, $email, $name, $vars, $storeId);
				$translate->setTranslateInline(true);
			}

				
			if (!$mailTemplate->getSentSuccess()) {
				Mage::log("CANNOT SEND EMMAIL : ", null, $logFileName);
			}else{
				Mage::log('Email has successfully sent', null, $logFileName);
			}


		}
	}

} // end foreach customer collection


Mage::log('Completed', null, $logFileName);
?>

3. Run below URL in browser or setup cron job using this URL
http://www.mysite.com/not_logged_in.php?day=15

Day is the parameter in which you can pass required number of days.

Note: You can also send mail to customers by calculating days using customer created date.

// calculate number of days


<?php
$now 		= time();
$lastDate 	= strtotime($customer->getCreatedAt());
$datediff 	= $now - $lastDate;
$totalDay  	= floor($datediff/(60*60*24));

?>

Enjoy!!