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 [email protected] 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', '[email protected]');
    $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);
}
1
  • 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
    Commented Feb 1, 2019 at 13:17

1 Answer 1

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.