WordPress.org

Ready to get started?Download WordPress

Forums

wpMandrill
[resolved] Invalid API key error on some emails but not others (7 posts)

  1. Nick Ciske
    Member
    Posted 7 months ago #

    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?

    https://wordpress.org/plugins/wpmandrill/

  2. Nick Ciske
    Member
    Posted 7 months ago #

    Hey @mc_benjen - any ideas on this one? I'm pulling my hair out...

  3. Nick Ciske
    Member
    Posted 7 months ago #

    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 'mandrillapp.com'

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

    But when Gravity Forms sends the email... it resolves mandrillapp.com 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.

  4. mc_benjen
    Member
    Posted 7 months ago #

    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 'mandrillapp.com' ?

  5. Nick Ciske
    Member
    Posted 7 months ago #

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

    http://www.gravityhelp.com/documentation/page/Gform_pre_submission

    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.') <no-reply@website.com>', '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.

  6. Nick Ciske
    Member
    Posted 7 months ago #

    Also, I tried commenting out the API key check... and the Mandrill template call fails with the same error:
    [error] => Couldn't resolve host 'mandrillapp.com'

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

  7. Nick Ciske
    Member
    Posted 7 months ago #

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

    http://i.imgur.com/IW8simF.gif

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

Reply

You must log in to post.

About this Plugin

About this Topic