Send emails with pdf attachment in Magento Cronjob

2019-08-29 16:33发布

问题:

I have a report.phtml that I render to create a PDF file and send it to some email every month.

I am trying to automate the process with Magento Cronjob.

I know Magento has built-in feature to handle cronjobs but that solution involves a model. I tried to render the report in model but I get nothing.

class MyModule_Model_Report extends Mage_Core_Model_Abstract
{

    public function generateReport()
    {
        $content = $this->getLayout()->createBlock(
                    'Mage_Core_Block_Template', 'mymodule.report', array(
                    'template' => 'mymodule/report/report.phtml',
                    )
                )->toHtml(); // nothing returns as we can't render layouts in model

        mail('test@test.com', 'monthly report', $content);// for now just testing email

        // rest of the PDF generation and emailing script            
    }

}

mymodule's config.xml

<config>
...
    <crontab>
        <jobs>
            <namespace_module>
                <schedule>
                    <cron_expr>0,15,30,45 * * * *</cron_expr>
                </schedule>
                <run>
                    <model>mymodule/Report:generateReport</model>
                </run>
            </namespace_module>
        </jobs>
    </crontab>
...
</config>

If I create controller and put above generateReport function it works fine.

class MyModule_ReportController extends Mage_Core_Controller_Front_Action
{

        public function generateReportAction()
        {
            $content = $this->getLayout()->createBlock(
                        'Mage_Core_Block_Template', 'mymodule.report', array(
                        'template' => 'mymodule/report/report.phtml',
                        )
                    )->toHtml(); // it returns the report.phtml contents

        mail('test@test.com', 'monthly report', $content); // for now just testing email

            // rest of the PDF generation and emailing script            
        }
}

Can someone please guide me how can I create a cronjob in such scenario.

Thanks in advance!