Is there a way to run a program or script on a linux box to see what MX records are used by default when sending outgoing email from mail or mailx? I'm writing a utility that is going to be run on a large, random set of linux machines, and I'd like that utility to automatically figure out the MX hosts that will actually be used on each machine when mail or mailx are used.
I know I can look at the machine's mail server's config file to see what the setting is for outgoing email, but each host might be using a different email server (sendmail, postfix, exim, etc.), and I don't want to write a utility that searches for each one and then tries to parse whatever config files it might find.
If I knew the algorithm that mail and mailx use for deciding what server to use for outgoing mail, I could then replicate that algorithm within my own utility.
Do mail and mailx just try 127.0.0.1? Or do they do something like getting the current host name and then using a DNS query to find the MX records for that host, or if none exist, to use the info from the A record? Or do they simply use the sendmail program (in which case, I'd need to figure out which outgoing server that sendmail chooses)? ... or what?
Thank you very much.
I have found the source code for
mailand for a version ofmailx, and it seems likemailjust uses thesendmailexecutable, andmailxallows an optional specification of an SMTP server, or if that's absent, it also uses thesendmailexecutable.This doesn't help me much, and so I think I need to use my own algorithm. Here's what I have come up with (pseudo-code). Does anyone see any problems with this? ...
In the end,
mxswill contain a list of IP addresses upon which the current machine's SMTP server is likely to be listening. It's still possible that none of these will actually work for sending email, but that's OK for my purposes.In the real world, I'll use a set rather than a list, to avoid duplicate entries.
How does this look?