0

In the function below, there is the problem of sending email to anonymous users, the email sending only for users logged and to complete checkout and payment.

I get the following error message:

Error sending e-mail (from admin@mysite.com to ).

My Module:

function mymodule_commerce_checkout_complete($order){
            // send mails
            mymodule_send_order_emails($order);
    }


function mymodule_send_order_emails($order = NULL) {
    $module = 'mymodule';
    $key = 'my_message';
    $from = variable_get('site_mail', 'info@mysite.com');
    $language = language_default();
    $values['html'] = 'Html text value.';
    $params = $values;
    $send = TRUE;

    // load profile
    $profile = commerce_customer_profile_load($order->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id']);

    // load user
    $user = user_load($order->uid);
    //$user->name;
    $to = $user->mail;

    $result = drupal_mail($module, $key, $to, $language, $params, $from, $send);
}
  • I don't think it's guaranteed that the checkout user is going to be an authenticated at this point so you may need to check the email address of the customer from the order and not the user object. – mradcliffe Feb 1 '19 at 13:17
0

You have anonymous orders so your code doing the loading of the user account obviously wont work as they dont have a user account:

 // load user
$user = user_load($order->uid);
//$user->name;
$to = $user->mail;

The above won't work for anonymous users as they don't have an account so the uid is always 0. I think the $order object has $order->mail to get the actual email address used on the order regardless if it was a full user or anonymous

| improve this answer | |
  • Great glad its sorted please feel free to mark my answer as the accepted answer if you'd like to – Leigh Feb 1 '19 at 14:00

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.