Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending emails via Sendgrid failing for some mails - unable to get mx info: failed to get IPs from PTR record: lookup <nil>: unrecognized address

As per How do i send email from Azure function app, we are unable to send email directly from our Azure Function, so instead we are using the SendGrid API to send our emails. (SendGrid seems to work well for us, and we get a free account via our Azure subscription)

While most emails go out fine, a handful aren't delivered. The SendGrid activity feed shows a status of Block for those. The detailed error message is

unable to get mx info: failed to get IPs from PTR record: lookup <nil>: unrecognized address

Sendgrid activity display, including the error message details

Since most emails do go out, we don't think it's a problem with our code. We have gone through the SendGrid domain authentication steps, verified our domain, added the DKIM keys to our DNS, and added the SendGrid hosts to our SPF entry. However, a few don't work, and we can't seem to find anything in the SendGrid help on our error.

The code we are using (Python) is largely taken from the SendGrid python example, in case it helps, but this doesn't report any errors when we send.

from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

def sendEmail(toaddress, subject, message):
   logging.info("Sending email to <%s> - %s", toaddress, subject)

   message = Mail(
      from_email=settings.emailFrom(),
      to_emails=toaddress,
      subject=subject,
      plain_text_content=message)
   try:
      sg = SendGridAPIClient(settings.sendgridAPIKey())
      response = sg.send(message)
      logging.info("Email sent via SendGrid: %d - %s", response.status_code, response.body)
   except Exception as e:
      logging.error(e.message)
like image 997
Gagravarr Avatar asked Apr 07 '20 11:04

Gagravarr


People also ask

Why is SendGrid not sending?

To fix this issue, you'll want to make sure that you've configured your setup to connect to smtp.sendgrid.net using authentication, and that the credentials you're using are your SendGrid username and a properly configured API key as the password. For more on API keys, see API Keys.

What is MX record in SendGrid?

A Mail Exchanger (MX) record in the DNS system specifies a mail server responsible for accepting email addresses on behalf of a domain. The MX records associated with a domain assure that the email is properly routed via Simple Mail Transfer Protocol (SMTP).

Can SendGrid Send from multiple domains?

Yes, it's possible to authenticate multiple domains. When multiple authenticated domains exist on your account, SendGrid will use the from address for each email you send through SendGrid and match it to a domain and branded link.


1 Answers

TL;DR - Check the domain for a typo!

.

It turns out that the error message shown in SendGrid did contain the answer in effect, but a bit cryptic and not quite what we'd expect to see for the actual error

unable to get mx info: failed to get IPs from PTR record: lookup <nil>: unrecognized address

The emails that were going out had the correct domain in them.

However, the problem emails all had a one character typo in the domain, close enough not to be spotted....

This typo'd domain does exist, and is registered. However, it has no DNS entries defined - no @ A record, and no MX record.

SendGrid was following the RFC, trying to find a MX record for the domain but there wasn't one, then falling back to the A record for the domain but that didn't exist either. This lead to SendGrid recording an error and giving up.

So, for this exact error, double-check the domains carefully for typos!

like image 138
Gagravarr Avatar answered Nov 09 '22 23:11

Gagravarr