3

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).

3

I think Mage::getUrl() might rely on you having an admin session (probably for the url keys), but if you just need a link to view the order, you can generate it yourself - it'll be something like:

https://<domain>/index.php/admin/admin/sales_order/view/order_id/<orderid>

The order id, of course, being found in $order->getId() - not the Increment ID.

As long as you're logged in, you can go to that URL successfully without the key at the end.

  • 1
    I'm using the real ID, and not the increment ID. I didn't think of the need to have an admin session opened to use this function, but now you say it, it makes sense ... I'll try to generate the url myself – christophe May 5 '15 at 16:41
  • Yeah, there's probably a way to do it "right", but if this is the only problem, this is probably much easier and quicker. – Mike May 5 '15 at 16:45
  • It worked for the url, but I have the same kind of problem when I try to send the mail (only on my live server...). See Edit 3 for more info. Thanks – christophe May 6 '15 at 10:02
  • 1
    @christophe which magento version you are using ? – Keyur Shah May 6 '15 at 10:22
  • 1
    @ Keyur Shah: I'm using Magento 1.9.1.0 on my 3 environments – christophe May 6 '15 at 10:23

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.