When managing a mailing list, one critical task is ensuring that the email addresses in your list are valid. Sending emails to invalid addresses not only clutters your system but also harms your sender reputation, which can affect your email deliverability. One effective way to validate email addresses is by checking their Mail Exchanger (MX) records.
What Are MX Records?
MX records are a type of Domain Name System (DNS) record that specifies the mail servers responsible for receiving email on behalf of a domain. If a domain has valid MX records, it means it is configured to receive emails. An email address without a valid MX record is considered invalid for sending emails.
Why Check MX Records?
- Reduce Bounce Rates: Emails sent to addresses without valid MX records will bounce back. High bounce rates can negatively impact your sender reputation with email service providers.
- Improve Deliverability: Cleaning up your list by removing emails without valid MX records helps ensure that your messages reach the intended recipients, improving overall deliverability rates.
- Cost Efficiency: Sending emails to invalid addresses wastes resources. By identifying and removing these addresses, you can save on costs associated with email sending services.
- Enhanced Analytics: A cleaner list provides more accurate metrics and insights. Knowing that your emails are reaching valid addresses helps you better measure the success of your email campaigns.
Bash Script
To help you can use the Bash script below to check if a domain list has valid MX records. If there is no MX record then any email sent here will be bounced. This can be used to remove email addresses that are not valid.
File domains.txt:
freebsd.org
redhat.com
yahoo.com
google.com
Then create mxlookup.sh
output='ns_output.txt'
# Clears previous output
> $output
# Seconds to wait between lookups:
loop_wait='1' # Is set to 1 second.
for domain in `cat $domain_list` # Start looping through domains
do
MX=$(dig MX $domain +short) #query MX records from domain list and store it as variable $MX
#echo $MX >> $output;
#echo $domain >> $output;
arr=( $MX ) #creates array variable for the MX record answers
echo ${domain} -- ${arr[1]} >> $output; #outputs only one record from above MX dig
: '
for ((i=1; i<${#arr[@]}; i+=2)); #since MX records have multiple answers, for loop goes through each answer
do
#echo $domain >> $output;
echo ${arr[i]} >> $output; #outputs each A record from above MX dig
#dig A +short "${arr[i]}" >> $output #queries A record for IP and writes answer
done
'
done;
Run it ./mxlookup.sh
which will produce a file ns_output.txt
which will allow you to identify domains without a valid MX record. These can then be removed from your mailer list.
Python Script
Or if you prefer you can use this python script:
import dns.resolver
def check_mx(domain):
try:
records = dns.resolver.resolve(domain, 'MX')
return True
except (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN):
return False
domains = ["example.com", "invalid-domain.com"]
valid_domains = [domain for domain in domains if check_mx(domain)]
print(f"Valid domains: {valid_domains}")
By regularly checking the MX records of the domains in your mailing list, you can maintain a clean and efficient list, improve your email deliverability, and protect your sending reputation.