can I have A record and MX record for the same hostname?

Can I have an A record and a MX record for the same hostname?

For example

example.com  A   192.0.2.42
example.com  MX  mail.anothersite.example.

Context

I'm trying to conciliate a gmail email forward (GMail requires for that MX records with hostnames without mail.*) with a website hosted at the 192.0.2.42

Example of typical gmail email forward configuration: enter image description here


Solution 1:

This is supported and actually widely used configuration. Almost everybody uses it.

Don't confuse "mail.example.com" with MX record for "example.com". Those are completely different things. "mail" is just a name. You can use it for anything. There are absolutely no rules how it should be used and that it must be used at all. It plays zero significance in email delivery. On the contrary, MX DNS record is the definitive source of information for other mail systems, which tell them how to delivery mail for this domain. An MX host can have name "blah" and have a record blah.example.net A some.ip.add.ress and for "example.com" mail to be delivered there, you just add corresponding MX record, like example.com. MX 10 blah.example.net. and live happily. At the same time, you can also have a name "mail" only for web mail access. This is only reception side; originating mail is controlled other way.

By the way, notice a number in the MX record, a "priority". The mail delivery first tried according to the record with lowest "priority", then if that host doesn't answer, to the next lowest and so on.

It seems some systems use "mail" name for their MX host name, mostly smaller ones. This is fine. On small sites a single system often does everything and it is convenient to give it name "mail". Large sites often have some hosts that receive mail, other hosts which originate it, third ones which do web mail access, some more which process IMAP, and others can accept user submissions. There is no concrete system which could be called just "mail", so often they don't use this name at all.

A note about "apex A" record, example.com. A ip.ad.dr.ess. If a domain don't have MX record at all (i.e. if there are no records like example.com. MX priority mx.host.name.), other mail exchangers will fall back to delivery the mail for this domain to the "apex" record (and they will connect to the ip.ad.dr.ess for that). MX record exists in the world precisely to deflect mail from this system to some dedicated mail server. Of course, when you have MX, you can use "apex" record for anything, including a web side, or not use it at all.