Support » Plugin: wpMandrill » Invalid API key error on some emails but not others

  • Resolved Nick Ciske


    Weird issue… I’m baffled.

    I get an Invalid API key error when I try to send mail with wp_mail. Gravity Forms (which uses wp_mail) sends emails using Mandrill just fine. Also happens if it try to use wpMandrill::mail directly.

    Same server. Same API key. Same function. One errors out, one works fine.

    [14-Jul-2014 16:21:21 UTC] wpMandrill Error: Error :
    [14-Jul-2014 16:21:21 UTC] 2014-07-14 16:21:21 wpMandrill::sendEmail: Exception Caught => Invalid API Key

    API key is valid.
    Server has curl.
    Domain is a valid sending domain (contact us emails work).

    Any ideas?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hey @mc_benjen – any ideas on this one? I’m pulling my hair out…

    Dug deeper and found out more, but it’s still pretty bizarre…

    So the failing requests are actually erroring out with:

    [error] => Couldn’t resolve host ‘’

    When it tries to validate the API Key by pinging the API endpoint.

    But when Gravity Forms sends the email… it resolves just fine… verifying the key, and grabbing the template.

    We’re talking seconds between sends… same server, same code, same DNS – that’s just just consistently flaky?

    Tried hardcoding the IP, but that threw a SSL certificate error:
    [error] => Problem with the SSL CA cert (path? access rights?)

    I don’t see any Mandrill specific code in Gravity Forms… so I’m still baffled how one call to wp_mail => mandrill fails while the other succeeds.

    Hey Nick, could you please tell us a bit more about how you’re making those requests and what you’re attempting to achieve?

    For example, you mention “same function”, do you mean the same function in Gravity Forms is the same function you’re attempting to use in your own code?

    Is your code ever successful, or are you able to get it to consistently fail in the same way every time?

    Are you able to share the code you’re using when you get: [error] => Couldn’t resolve host ‘’ ?

    I’m simply calling wp_mail in a gform_pre_submission Gravity Forms hook (fires just before the entry is saved to the DB):

    Snippet of the code sending the email:

    $subject = $from->display_name.' ('.$from->user_login.') replied to your "'.$item->post_title.'" post! ';
    	$message = $before."\r\n\r\n".$content."\r\n\r\n".$after;
    	$headers = array( 'From: Website Name ('.$from->user_login.') <>', 'Reply-To: '.$from->user_email );
    	wp_mail( $to->user_email, $subject, $message, $headers );

    Gravity Forms also uses wp_mail:

    $is_success = wp_mail($to, $subject, $message, $headers, $attachments);

    My email goes through the native wp_mail function with the Invalid API key error – though the key is valid and CURL is enabled… so it’s a red herring — the error messages around API key validation could be more descriptive (e.g. a missing key is technically invalid, bit “API Key Missing” would be more useful, being unable to verify a key does not make it invalid, but unverifiable).

    A contact form response sent by Gravity Forms… goes through Mandrill with no API error.

    The “unable to resolve” error is from me dumping the $response in Mandrill::request right after it’s returned (or not in this case — it appears to be a CURL error).

    Hence why I’m baffled – if it’s all going through the Mandril override of wp_mail – why the inconsistent behavior? Is it possible there are 2 code paths there? I worked my way all the way down to the Mandrill::http_request and it all seems to be the same, but of course that would mean it’s external to the code — but that should mean that contact form responses fail to use Mandrill sometimes (they never do).

    I tried using wpMandrill::mail directly in my code but received the same errors, so the issue is deeper than the wp_mail wrapper code.

    Also, I tried commenting out the API key check… and the Mandrill template call fails with the same error:
    [error] => Couldn’t resolve host ‘’

    So it consistently fails to reach the mandrill API server, not just on the API key validation — but only when I call wp_mail in my handler… apparently not anywhere else…

    Well, solved my own problem.

    The issue was the pre-submission hook.

    Moving my email code to after submission hook fixed it — which makes sense as that’s when Gravity Foms would send admin notices and user confirmations.

    Must be that the Gravity Forms pre submission hook fires too early in the hook order for Mandrill to work correctly.

    Now I know 😉

    The More You Know GIF

    PS- Have you considered using the wp_remote_get and wp_remote_post helpers instead of CURL/fsockopen directly?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Invalid API key error on some emails but not others’ is closed to new replies.