We needed to send a friendly reminder to some guests who have been invited to our Magento Enterprise store but never accepted. We have a plugin that creates coupon codes and had it dump to a CSV and gave them some free reward points to spend if they came to our site and joined.
1. create the xml for our new Module in our package Bloom:
In app/etc/modules/ create a file called Bloom_SecondChanceEmail.xml
<?xml version="1.0"?>
<config>
<modules>
<Bloom_SecondChanceEmail>
<active>true</active>
<codePool>local</codePool>
<version>0.0.1</version>
</Bloom_SecondChanceEmail>
</modules>
</config>
2. Create a folder and sub folders in /app/code/local/Bloom/
New folder SecondChanceEmail
New sub folders: etc, Model and sql
Inside etc create a file called config.xml
<?xml version="1.0"?>
<config>
<modules>
<Bloom_SecondChanceEmail>
<version>0.0.1</version>
</Bloom_SecondChanceEmail>
</modules>
<global>
<models>
<bloom_secondchanceemail>
<class>Bloom_SecondChanceEmail_Model</class>
</bloom_secondchanceemail>
</models>
<resources>
<secondchanceemail_setup>
<setup>
<module>Bloom_SecondChanceEmail</module>
</setup>
</secondchanceemail_setup>
</resources>
<template>
<email>
<secondchance_email_template translate="label" module="secondchanceemail">
<label>Second Chance Email</label>
<file>secondchance.html</file>
<type>html</type>
</secondchance_email_template>
</email>
</template>
</global>
</config>
Inside Model create a file called Observer.php
<?php
class Bloom_SecondChanceEmail_Model_Observer {
public function sendSecondChanceEmails() {
$baseUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
// get the template for sending the reminder email
$templateId = Mage::getStoreConfig('secondchance_email/template');
// subject
$mailSubject = 'Your friend has sent you a gift from Bloom.com';
// Sender email
$sender = array(
'name' => Mage::getStoreConfig('trans_email/ident_support/name'),
'email' => Mage::getStoreConfig('trans_email/ident_custom1/email')
);
$supportEmail = Mage::getStoreConfig('trans_email/ident_support/email');
$supportPhone = Mage::getStoreConfig('general/store_information/phone');
$days_back = 45;
$dateStart = date( "Y-m-d H:i:s", mktime( 0,0,0, date( "m" ), date( "d" ) - $days_back, date( "Y" ) ) );
$dateEnd = date( "Y-m-d H:i:s", mktime( 23,59,59, date( "m" ), date( "d" ), date( "Y" ) ) );
//$pendingInvite = Mage::getModel('enterprise_invitation/invitation')->getCollection()->addFieldToFilter('status','sent');
$pendingInvitations = Mage::getModel('enterprise_invitation/invitation')->getCollection()
->addFieldToFilter('date', array('from' => $dateStart, 'to' => $dateEnd))
->addFieldToFilter('status','sent')->getData();
$row = 0;
$file = $baseUrl . "secondchance.csv";
$coupon_array = array();
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$coupon_array[$row] = $data[0];
$row++;
}
fclose($handle);
}
foreach( $pendingInvitations as $invite )
{
$storeId = $invite['store_id'];
// Load the skin url based on the store ID
Mage::app()->setCurrentStore($storeId);
$customer_data = Mage::getModel('customer/customer')
->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
->loadByEmail($invite['email'])->getData();
if($customer_data)
{
// Do nothing for now
}
else
{
$couponCode = $coupon_array[$i];
// Set variables that can be used in email template
$vars = array(
'customerName' => $customerName,
'customerEmail' => $invite['email'],
'supportPhone' => $supportPhone,
'supportEmail' => $supportEmail,
'couponCode' => $couponCode
);
$translate = Mage::getSingleton('core/translate');
Mage::getModel('core/email_template')
->setTemplateSubject($mailSubject)
->sendTransactional($templateId, $sender, $invite['email'], $customerName, $vars, $storeId);
$translate->setTranslateInline(true);
}
}
}
}
In sql create a folder called secondchanceemail_setup
Inside the new folder secondchanceemail_setup create a file called mysql4-install-0.0.1.php
<?php
$installer = $this;
$installer->startSetup();
/* @var $installer Mage_Core_Model_Resource_Setup */
$configValuesMap = array(
'secondchance_email/template' =>
'secondchance_email_template',
);
foreach ($configValuesMap as $configPath=>$configValue) {
$installer->setConfigData($configPath, $configValue);
}
$installer->endSetup();
In the main folder of your site create a csv file called secondchance.csv and put in some coupon codes but make sure you have either exactly how many you need to fulfill every invite or better yet a few more (we needed 4000! ). Here is a sample:
19VCH27
17LUG62
17FUE65
16UVL45
15PFC32
14ECJ28
13GJU35
14ENS67
15DHV65
15FUT82
14EAV75
13RAK87
14UNL82
13EUN73
15ETC64
11RWX25
12LPM45
12WVP73
13WCN32
12GNF86
12VUT98
13ARC82
13XNV78
Last step is to create a file in shell/ called secondChanceEmail.php
<?php
/**
* Magento Enterprise Edition
*
* NOTICE OF LICENSE
*
* This source file is subject to the Magento Enterprise Edition License
* that is bundled with this package in the file LICENSE_EE.txt.
* It is also available through the world-wide-web at this URL:
* http://www.magentocommerce.com/license/enterprise-edition
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Shell
* @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
* @license http://www.magentocommerce.com/license/enterprise-edition
*/
require_once 'abstract.php';
/**
* Magento Log Shell Script
*
* @category Mage
* @package Mage_Shell
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Shell_secondAttemptSendInvite extends Mage_Shell_Abstract
{
/**
* Run script
*
*/
public function run()
{
/*
* MAKE NOTE!!!!!!!
*
* this transaction is not saved, so if you run it more than once in 45 days,
* customers will get another email
*
*/
$secondChance = new Bloom_SecondChanceEmail_Model_Observer();
$secondChance->sendSecondChanceEmails();
}
}
$shell = new Mage_Shell_secondAttemptSendInvite();
$shell->run();
Now to get this to run, you use ssh and log into your site, chance into the shell directory
$ cd shell/ ( hit enter )
then type this command
$ php secondChanceEmail.php ( hit enter )