I have a little problem here. I've created a custom module to generate order reports from Magento (CSV file) and to send it by email through a cron task.
I developped it on a local environment (Magento installed on windows) with the exact same DB as my live website (duplication on local). Everything works well on this local environment, I am able to generate the report manually through the button I've put in the admin section and the cron task send the email with the report periodically.
The problem appeared once deployed on my live server (unix base server) where my code seems to fail on this line :
$row = array();
$row['order_link'] = Mage::getUrl("adminhtml/sales_order/view", array('order_id' => $item->getOrderId()));
I checked the content of $item->getOrderId()
and it is an existing order id.
The weird thing is I don't have any error in my logs (var/log/system.log or var/log/exception.log) and no exception is fired (I tried to surround this code with a try/catch and to display the exception in a log file).
I'm sure this is the line which fails because my log right after isn't displayed. However, I can't understand why it doesn't work on my live website but it does on my local.
Any idea please ?
Edit1: English mistakes
Edit2: The url is retrieved if I trigger it through the admin interfaces (click on the generate report button manually), but it fails when run through the cron task.
Edit 3: I've followed Mike advice and creating the order link myself works. I still have this issue when I try to send my transactional email. Weird thing is it works on my local and on a test server (I executed the cron every five minutes all night and it worked), but not on my live server ... again, no error message in system.log or exception.log.
Here's more info:
protected function sendTransactionalEmail($templateId, $sender, $recipientEmail, $recipientName, array $vars, $file, $fileType)
{
$status = false;
try {
if (self::LOG_ENABLED) Mage::log("create transactional email object", null, self::LOG_FILE);
$transactionalEmail = Mage::getModel('core/email_template')
->setDesignConfig(array('area' => 'frontend', 'store' => null));
if($file != null) {
if (self::LOG_ENABLED) Mage::log("add attachment file", null, self::LOG_FILE);
$this->addAttachment($transactionalEmail, $file, $fileType, 'Orders_Report_' . date("d-m-Y"));
}
if (self::LOG_ENABLED) Mage::log("send email", null, self::LOG_FILE);
$transactionalEmail
->sendTransactional($templateId, $sender, $recipientEmail, $recipientName, $vars);
$status = true;
} catch (Exception $e) {
if (self::LOG_ENABLED) Mage::log("FAILURE: Email not sent", null, self::LOG_FILE);
if (self::LOG_ENABLED) Mage::log($e->getMessage(), null, self::LOG_FILE);
}
if (self::LOG_ENABLED) Mage::log("Email sent SUCCESSFULLY", null, self::LOG_FILE);
return $status;
}
protected function sendTransactionalEmail($templateId, $sender, $recipientEmail, $recipientName, array $vars, $file, $fileType)
{
$status = false;
try {
if (self::LOG_ENABLED) Mage::log("create transactional email object", null, self::LOG_FILE);
$transactionalEmail = Mage::getModel('core/email_template')
->setDesignConfig(array('area' => 'frontend', 'store' => null));
if($file != null) {
if (self::LOG_ENABLED) Mage::log("add attachment file", null, self::LOG_FILE);
$this->addAttachment($transactionalEmail, $file, $fileType, 'Orders_Report_' . date("d-m-Y"));
}
if (self::LOG_ENABLED) Mage::log("send email", null, self::LOG_FILE);
$transactionalEmail
->sendTransactional($templateId, $sender, $recipientEmail, $recipientName, $vars);
$status = true;
} catch (Exception $e) {
if (self::LOG_ENABLED) Mage::log("FAILURE: Email not sent", null, self::LOG_FILE);
if (self::LOG_ENABLED) Mage::log($e->getMessage(), null, self::LOG_FILE);
}
if (self::LOG_ENABLED) Mage::log("Email sent SUCCESSFULLY", null, self::LOG_FILE);
return $status;
}
The logs stop after the log "send email" (and never display the log "Email sent SUCCESSFULLY" nor exception logs).