Recently I ran into a problem trying to send email using the mailgun API because mailgun uses the domain registered with mailgun in the 'sender' field for the email 'envelope' map.
The failed response error message I got was:
Server response: 550 [email protected] Sender rejected
I followed the directions in the mailgun documentation for verifying my domain and used the recommended best practice of creating the 'mg' subdomain for my mailgun email routing. After adding the text records, I was able to send email using the smtp method through a gmail alias, however whenever I tried to send an email using the same reply-from address, it failed.
After further investigation, I noticed that it does not fail when I send an email to a gmail account, but it does fail when I try to send the email to an account hosted by mydomain.com.
The problem turns out to be that certain email servers will first check that the subdomain specified in the sender email hostname have a MX record that is active. This check appears to be a part of internet standards and exist in order to combat spam. However it is used by some but not all email servers... gmail does not seem to perform this verification so emails to gmail addresses did not get rejected.
Because the mailgun api adds whichever domain is registered and verified with mailgun in the 'sender' field, my emails were being rejected by mydomain.com because there was no MX record for that subdomain.
I fixed the problem by going to my DNS and adding the MX record for the subdomain
mg.example.com
and it started to work once the record changes were propagated.In the fourth item of the verification process, mailgun does suggest that an MX record be added for your domain. However, my mistake seems to have been to think that an MX record attached to the root domain would cover the subdomain. A separate MX record must also be setup for the subdomain.