I am pretty new to networking. I would like to buy a domain to setup a website and to use it also for emails. To do this, I should need to configure an A DNS record and a MX one. My question is... If I use a different server for emails than the one I use for the website (let's say I decide to use gmail with the custom domain), will email be sent ONLY to the server set up with the MX record, or will they also be sent to the A one? I'm asking because I saw a lot of configurations using a subdomain for emails, like "mail.domain.com", and therefore there is no risk of conflicting IP resolutions. I basically don't want that also the VPS provider receive the emails, as I want them to be handled only by Google
Mail will always be sent to the MX records that are configured for a domain1.
It is perfectly suitable to set up a domain with the
A record for the bare domain and
www pointing towards a web server and the MX record(s) to a different provider.
example.com. IN A 10.9.8.7 www.example.com. IN A 10.9.8.7 example.com. IN MX 1 ASPMX.L.GOOGLE.COM example.com. IN MX 5 ALT1.ASPMX.L.GOOGLE.COM
Note 1: there are a couple of possible exceptions/corner cases, for instance when you name name your own VPS
example.com and attempt to send mail from that VPS to
email@example.com odds are that the mail daemon on that VPS will say: '"hey,
@example.com , that's me!" and attempt local delivery rather than looking up the MX record and sending the message to G suite.
The 'A' and 'MX' records are orthogonal concepts. The MX record outlines where email should go, the A record to which IP address a particular DNS entry resolves to. The simplest example would be something like;
- MX record for your fqdn (mydomain.com), pointing to mail.mydomain.com.
- A record for mail.mydomain.com pointing to a given IP address.
You mention you're looking to configure email to be delivered to gmail. It is not clear whether you're planning to use a mailserver operated by Google (hosted email for your domain), or whether you're looking to forward email from your domain to a specific emailbox (e.g. firstname.lastname@example.org). In the latter case, you´ll probably need to configure a mailserver that provides that forward.
If I use a different server for emails than the one I use for the website (let's say I decide to use gmail with the custom domain), will email be sent ONLY to the server set up with the MX record, or will they also be sent to the A one?
The answer is yes but. You will be interested about RFC5321, more specifically section 5.1. An SMTP client may use A records as fallback mechanism if no MX is found. Keep in mind that not all systems will necessarily do that or follow RFCs strictly.
5.1. Locating the Target Host
Once an SMTP client lexically identifies a domain to which mail will be delivered for processing (as described in Sections 2.3.5 and 3.6), a DNS lookup MUST be performed to resolve the domain name (RFC 1035 ). The names are expected to be fully-qualified domain names (FQDNs): mechanisms for inferring FQDNs from partial names or local aliases are outside of this specification. Due to a history of problems, SMTP servers used for initial submission of messages SHOULD NOT make such inferences (Message Submission Servers  have somewhat more flexibility) and intermediate (relay) SMTP servers MUST NOT make them.
The lookup first attempts to locate an MX record associated with the name. If a CNAME record is found, the resulting name is processed as if it were the initial name. If a non-existent domain error is returned, this situation MUST be reported as an error. If a temporary error is returned, the message MUST be queued and retried later (see Section 220.127.116.11). If an empty list of MXs is returned, the address is treated as if it was associated with an implicit MX RR, with a preference of 0, pointing to that host. If MX records are present, but none of them are usable, or the implicit MX is unusable, this situation MUST be reported as an error.
Transient DNS resolutions errors can and do occur. So there is a possibility that the host of the A record(s) will receive incoming mail. It is your responsibility to properly configure your zone but if you have an MTA running on the A address and assuming you can't disable it you should ensure that it will not accept mail for your domain name. The exact configuration depends on the software.