stevenyang1011
11/15/2017 - 9:00 PM

Magento email test script

Magento email test script

<?php
require_once '../abstract.php';

class MR_Emailtest extends Mage_Shell_Abstract
{
    /**
     * Retrieve Usage Help Message
     *
     * @return string
     */
    public function usageHelp()
    {
        $scriptName = basename(__FILE__);

        return <<<USAGE
Usage:  php -f $scriptName -- --template <template> --email <email>

Available template:

    new-account
    forgot-password
    abandoned-cart

USAGE;
    }

    public function run()
    {
        Mage::app()->setCurrentStore(1);

        switch ((string)$this->getArg('template')) {
            default:
                echo $this->usageHelp();
                break;
            case 'new-account':
                $this->testNewAccountEmail();
                break;
            case 'abandoned-cart':
                $this->testAbandonedCartEmail();
                break;
            case 'forgot-password':
                $this->testForgotPasswordEmail();
                break;
        }
    }

    protected function _getEmail()
    {
        return $this->getArg('email') ?: 'roc@moustacherepublic.com';
    }

    protected function _getCustomer()
    {
        return Mage::getModel('customer/customer')
            ->setWebsiteId(Mage::app()->getWebsite()->getId())
            ->loadByEmail($this->_getEmail());
    }


    public function testNewAccountEmail()
    {
        $customer = $this->_getCustomer();
        $customer->sendNewAccountEmail('registered', '', Mage::app()->getStore()->getId());

        echo "New Account email is sent to {$customer->getEmail()}\n";
    }

    public function testForgotPasswordEmail()
    {
        $newResetPasswordLinkToken = Mage::helper('customer')->generateResetPasswordLinkToken();
        $customer = $this->_getCustomer();
        $customer->changeResetPasswordLinkToken($newResetPasswordLinkToken)
            ->sendPasswordResetConfirmationEmail();

        echo "Forgot Password email is sent to {$customer->getEmail()}\n";
    }

    public function testAbandonedCartEmail()
    {
        $quote = Mage::getModel('sales/quote')
            ->loadByCustomer($this->_getCustomer())
            ->setEbizmartsAbandonedcartCounter(0)
            ->setEbizmartsAbandonedcartFlag(0)
            ->setEbizmartsAbandonedcartToken('')
            ->save();

        $resource = $quote->getResource();
        $writeConnection = Mage::getSingleton('core/resource')->getConnection('core_write');

        // Set "Updated At" to a earlier time.
        $writeConnection->update(
            $resource->getMainTable(),
            ['updated_at' => '2014-11-06 04:34:20'],
            $writeConnection->quoteInto($resource->getIdFieldName() . '=?', $quote->getId())
        );

        Mage::getModel('ebizmarts_abandonedcart/cron')->abandoned();
    }
}

$shell = new MR_Emailtest();
$shell->run();