Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup cron job in magento?

I am working on magento 1.7.0.2 version.

I want to send csv in mails to customers. This should be done every month using cron job in magento.

For cron job config.xml

<crontab>
    <jobs>
        <Module_Store>
            <schedule><cron_expr>0,15,30,45 * * * *</cron_expr></schedule>
            <run><model>clinic/observer::sendMailtoClinic</model></run>
        </Module_Store>
    </jobs>
</crontab>    

Observer.php

class Module_Store_Model_Observer {

  public function sendMailtoClinic(Varien_Event_Observer $observer, $content){                        
    $mail = new Zend_Mail();
    $mail->setType(Zend_Mime::MULTIPART_RELATED);
    $mail->setBodyHtml($html_body);
    $mail->setFrom($from_email, $from_email_name);
    $mail->addTo($to_email, $toEmailName);
    $mail->setSubject($subject);
    $file = $mail->createAttachment(file_get_contents($file_path));
    $file->type = 'text/csv';
    $file->disposition = Zend_Mime::DISPOSITION_INLINE;
    $file->encoding = Zend_Mime::ENCODING_BASE64;
    $file->filename = $file_name;
    $mail->send();
  }
}

Admin configuration for cron are :-

Generate Schedules Every 15
Schedule Ahead for 15
Missed if Not Run Within 35
History Cleanup Every 15
Success History Lifetime 10
Failure History Lifetime 600

Any help would be much appreciable.

like image 945
Neeraj Garg Avatar asked Dec 18 '25 04:12

Neeraj Garg


2 Answers

If mail system working fine and still you facing issue with this then try this one also:

replace your config code with this:

<crontab>
    <jobs>
        <clinic_cron>
            <schedule><cron_expr>0,15,30,45 * * * *</cron_expr></schedule>
            <run><model>clinic/observer::sendMailtoClinic</model></run>
        </clinic_cron>
    </jobs>
</crontab> 


class [Packagename]_Clinic_Model_Observer { // don't forget to mention package name

  public function sendMailtoClinic(Varien_Event_Observer $observer){                        

    $html_body = 'this is html body text';
    $from_email = '[email protected]';
    $from_email_name = 'sendername';
    $to_email = '[email protected]';
    $toEmailName = 'receiverName';
    $subject = 'subject text here';
    $file_path = 'here/is/file/path';
    $file_name = 'filename.csv';

    $mail = new Zend_Mail();
    $mail->setType(Zend_Mime::MULTIPART_RELATED);
    $mail->setBodyHtml($html_body);
    $mail->setFrom($from_email, $from_email_name);
    $mail->addTo($to_email, $toEmailName);
    $mail->setSubject($subject);
    $file = $mail->createAttachment(file_get_contents($file_path));
    $file->type = 'text/csv';
    $file->disposition = Zend_Mime::DISPOSITION_INLINE;
    $file->encoding = Zend_Mime::ENCODING_BASE64;
    $file->filename = $file_name;
    $mail->send();
  }
}

search the word "clinic_cron" in Scheduled tasks tasks list here : System > Scheduler > List View. Make sure you are searching with all scheduled tasks, i mean see paging also ;)

Hope this helps! All the best!

like image 120
Lalit Kaushik Avatar answered Dec 20 '25 20:12

Lalit Kaushik


Got to app/code/core/Mage/CatalogRule/etc/config.xml

and the put

   <config>
     ...
   <crontab>
    <jobs>
    <catalogrule_apply_all>
        <schedule><cron_expr>0 1 * * *</cron_expr></schedule>
        <run><model>catalogrule/observer::dailyCatalogUpdate</model></run>
    </catalogrule_apply_all>
  </jobs>
    ...
 </crontab>
   ...
  </config>

You can change the schedule as per you need for more please see http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/how_to_setup_a_cron_job

like image 20
user3656133 Avatar answered Dec 20 '25 20:12

user3656133



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!