I would like to send my customers a mail, after a week, when the order has been completed.

Can someone help me? (Magento 1.9.2).

I also found a topic about this issue, but it is not updated for a while.

My code for so far:


    <?xml version="1.0"?>
                <schedule><cron_expr>5 * * * *</cron_expr></schedule>


class Productreviews_Productreview_Model_Cron{   
    public function cronSendReviewMail(){
        Mage::log("Cron Test ".now(),null,"mageCronExample.log");

$time = time();
$to = date('Y-m-d H:i:s', $time);
$lastTime = $time - 604800; // 60*60*7*24
$from = date('Y-m-d H:i:s', $lastTime);

$order_collection = Mage::getModel('sales/order')
->setOrder('created_at', 'desc')
->addAttributeToFilter('created_at', array('from' => $from, 'to' => $to))
->addAttributeToFilter('status', array('==' => array('complete')));

                foreach ($order_collection as $item) {
                    // echo $item->getId();

                    $sendtoemail = $item->getShippingAddress()->getEmail();

                    $idid = $item->getIncrementId();
                    $order = $item->loadByIncrementId($idid);
                    $items = $order->getAllVisibleItems();

                        // EMAIL LAYOUT
                        $email = 'Email content here';

                            // EMAIL VERSTUREN
                            $to      = $sendtoemail;
                            $subject = 'Subject';
                            $headers = 'From: x@example.com' . "\r\n" .
                                'Reply-To: x@example.com' . "\r\n" .
                                'X-Mailer: PHP/' . phpversion();
                            $headers .= "MIME-Version: 1.0\r\n";
                            $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

                            mail($to, $subject, $email, $headers);      

  • you want to send mail immediate after order complete or after a week of order complete. – Ashish Madankar M2 Professiona May 11 '16 at 13:50
  • Customers should receive a mail after a week of order complete. – Dneiz May 11 '16 at 20:14

For this you have to use cron which will execute on daily basis and fetch order data, depending upon created_at field of collection, you can decide the order of your required duration some thing like all orders whose created_at value is less than a week and order status is complete. for this you can use the following code.

$time = time();
$to = date('Y-m-d H:i:s', $time);
$lastTime = $time - 604800; // 60*60*7*24
$from = date('Y-m-d H:i:s', $lastTime);
$order_items = Mage::getResourceModel('sales/order_item_collection')
    ->addAttributeToFilter('created_at', array('from' => $from, 'to' => $from))
    ->addAttributeToFilter('status', array('eq' => Mage_Sales_Model_Order::STATE_COMPLETE))

and to create a cron you can follow the following tutorials.


Hope this will help you.

|improve this answer|||||
  • Thank you very much. I edited the code and it is working fine. However, is there a way to start the cron after the order has been completed? Finally, it needs to be like: order finished at 01-05-2016 --> customer receives a mail at 07-05-16. Thanks! – Dneiz May 12 '16 at 15:08
  • For this difference just replace the 604800 in line $lastTime = $time - 604800; // 60*60*7*24 with 518400 this will provide you the exact record. for testing you can try $time = strtotime('2016-05-07'); echo '<br> TO - '.$to = date('Y-m-d H:i:s', $time); $lastTime = $time - 518400; // 60*60*7*24 echo '<br>FROM - '.$from = date('Y-m-d H:i:s', $lastTime); – Ashish Madankar M2 Professiona May 12 '16 at 17:59
  • Thank you again. However, you stated that the cron needs to be executed on daily basis and completed order date is set to 7 days. Is this the right way to do this? Because customers will receive a mail again on day 2 now. I also added the my code above now. – Dneiz May 14 '16 at 11:04
  • How? firstly on day 2 difference is going to be 8 days so this order is not going to select on second days and you can use custom table which stores last order processed by the cron and add a condition to not to select order for which emails are already sended. – Ashish Madankar M2 Professiona May 14 '16 at 12:57

Me too faced this same situation. I solved with Following Code:

Create a file called sendmail.php in root folder with the following code:

require_once ("app/Mage.php");
$time = time();
$to = date('Y-m-d H:i:s', $time);
$lastTime = $time - 604800; // 60*60*7*24
$from = date('Y-m-d H:i:s', $lastTime);
$lastweek = date('Y-m-d', strtotime("-7 days"));
//Get Completed Order Collection placed in last one week 
$orders = Mage::getModel('sales/order')->getCollection()
->addAttributeToFilter('created_at', array('from' => $lastweek .' 00:00:00', 'to' => $lastweek. ' 23:59:59'))
->addAttributeToFilter('status', array('in' => Mage_Sales_Model_Order::STATE_COMPLETE));
foreach($orders as $order)  
    $order_incr_id = base64_encode($order->getIncrementId());

    $template = 38;// Template Id, You Can Get from System Config too
    $storeId = Mage::app()->getStore()->getId();
    $store_name = Mage::app()->getWebsite()->getName();
    $website_id = Mage::getModel('core/store')->load($order->getStoreId())->getWebsiteId();
    $website = Mage::app()->getWebsite($website_id);
    $website_name = strtolower($website->getName());
    $template = Mage::getStoreConfig('statuschange_options/quote_email/rating_template');
    $translate = Mage::getSingleton('core/translate');

    $mailSuccess = Mage::getModel('core/email_template')->setDesignConfig(
        array('area' => 'frontend', 'store' => $storeId))
                //You Can Add Any Parameters to be Passed in the mail
                    'sender'  => $senderName,
                    'username' => $order->getCustomerFirstname(),
                    'order_id' => $order->getIncrementId(),
                    'storename' => $website_name,
                    'customer_id' => $order->getCustomerId(),
                    'order_incr_id' => $order_incr_id

                // If mail Sent it will be Logged with Customer name, Order Id and Mail Id
                Mage::log('Mail sent. OrderId: '. $order->getIncrementId().' '.'Customer Name: '.$order->getCustomerFirstname().' '.$order->getCustomerLastname().' '.'MailId: '. $order->getCustomerEmail()
                , null, $lastweek.'-'.'MailSent.log');
            else {
                // If mail Not Sent it will be Logged with Customer name, Order Id and Mail Id
                Mage::log('Mail not sent. OrderId: '. $order->getIncrementId().' '.'Customer Name: '.$order->getCustomerFirstname().' '.$order->getCustomerLastname().' '.'MailId: '.$order->getCustomerEmail()
                , null, $lastweek.'MailNotSent.log');


You Need to Set in Cron File to Run when it to be run. I set it to run in the 11:55:00 PM

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.