18

This question got me thinking, and I now realize that I don't know anything about the internals of MTAs.

What exactly does an MTA do? Everything after the SMTP protocol seems like dark magic to me. Let's say that I wanted to code a minimalistic MTA (or MDA) just for sending emails, what would I need to learn/do?

Edit: I don't actually plan on writing an MTA, I just want to understand how it works internally.

Community
  • 1
  • 1
Alix Axel
  • 151,645
  • 95
  • 393
  • 500

3 Answers3

15

--- edit after somehow noticing you talked about possibly writing a MTA ---

To write a MTA, you need to open a server socket. When someone connects, you need to send and receive text (ascii) data on that socket in compliance with the SMTP protocol. SMTP is very chatty, so you can expect a few rounds of communication.

The initial round of communication typically tells you whether SMTP is supported or ESMTP is supported. The second (optional) round of communication is to determine security / encryption / feature support. Eventually the "client" side will ask to send a message to a particular address / set of addresses. When done, the server will indicate that it's ready to get the body of the email message. When the body of the message (and it's optinal attachments) have all been transmitted, the MTA will tell you it received the message fine. At that point in time, the MTA will act as a client to other MTAs discovered via DNS MX records to get your email closer to it's destination MTA which will copy it into someone's inbox.

So an MTA is needed because mail delivery on the client side is the equivalent to handing a physical letter to a post office. Post offices are responsible for inter-postoffice routing (which parallels to MTA-to-MTA transmission). The destination Post office is then responsible for delivery of the letter to the post office box or home address (which parallels one's computer inbox).

They don't call it e-mail for nothing.

--- original post follows --- A MTA will accept a mail message, see if it can forward or deliver it, respond if it can be forwarded or delivered, and then forward or deliver it if it indicated it could.

How the message gets closer to it's final destination usually has a bit to do with DNS. MX (mail exchange) records in DNS indicate servers which are responsible (or at least closer to the responsible server) for particular email domain names. It is not possible to fully understand how a mail message gets closer to it's destination without understanding how DNS works.

A MTA typically looks at the delivery address, and either is configured to be the "end point" of the email address's mail domain, or knows that server XYZ is one hop closer to the email address's mail domain. If it's an endpoint, it will copy the message from the wire into someone's inbox. If it's relaying it will "forward" the message to the next MTA.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • I assume MTAs do a MX lookup on the domain of the delivery address, connect to a specific port to the server which that has the lowest MX priority and then send a payload obeying to a specific protocol, right? I think I would understand this a lot better if I could perform a manual message "forward" but I have no idea which port I should connect to or the specific payload to send... – Alix Axel Feb 17 '11 at 00:46
  • I understand how SMTP works, let me put it this way: when I send an email to a @gmail address through my local MTA the message is delivered - I assume my MTA doesn't perform any kind of authentication with Gmail since it doesn't know my login credentials. However, if I connect to the Gmail SMTP server and have a perfectly valid SMTP dialog (ignoring the authentication) with it my message doesn't get through. What is my MTA doing that I ain't? – Alix Axel Feb 17 '11 at 00:54
  • Your MTA is likely forwarding to a proxy MTA set up by your ISP. That MTA probably accepts non-authenticated connections (as the pain of configuring per-customer authentication would be high). It then relays (same client-server communications) the email closer to gmail.com; but, it probably is doing so with a channel that requires stronger authentication. It's a spam fighting technique to prevent anyone who just learned SMTP to write a small client that can spam port 25. Google (the irony) Email Authentication, DomainKeys and Sender policy framework. It's probably what's blocking you. – Edwin Buck Feb 17 '11 at 01:06
  • If you manage to add to your client all of the above, I'll bet your client could connect to gmail directly! Or at least, it would have a better chance :) – Edwin Buck Feb 17 '11 at 01:07
  • Note that if you are opening with a HELO, then delivery might be rejected outright on some MTAs because most of the above is only available through ESMTP, which requires a EHLO (I think) hello. – Edwin Buck Feb 17 '11 at 01:09
  • @Edwin: I'm aware of SPF and DKIM, and actually it worked. Thank you for you input. – Alix Axel Feb 17 '11 at 01:28
4

Here ya go: http://en.wikipedia.org/wiki/Message_transfer_agent

Quickly, the MTA receives the raw message, decides where it's ultimate destination is, and then forwards the message on to that destination.

A very simple MTA can be written the delivers only to local inboxes. The MTA is an "easier" part of the system to write because you can behave badly but still be functional, so your interoperability with other systems is less of an issue (that's where much of the complexities of email lie nowadays, that and spam/virus checking).

The real contract of an MTA is simply that if you accept the message from the system sending it to you, you accept responsibility to deliver that message. Thus, when that socket closes with an acknowledgement of acceptance, the delivering systems job is done and it's all in your hands.

If you happen to do a crummy job, mail is lost, and it's your problem. But it's still fun to play around with.

Will Hartung
  • 115,893
  • 19
  • 128
  • 203
  • What I'm trying to figure out is how it forwards the message to the destination... Surely there must be some kind of protocol involved, no? – Alix Axel Feb 17 '11 at 00:00
  • All depends on the destination, no? The MTA is what listens to the SMTP port on the server, so it accepts the request from the client (an email program or another mail server). If the final destination for the message is simply a local inbox, then the "protocol" can be as simple as appending the message to an mbox. If the final destination is somewhere else (i.e. another mail server), then it's likely going to be using SMTP to xfer the message. – Will Hartung Feb 17 '11 at 00:20
  • I tried connecting to Google MX and SMTP servers directly, on port 25 and 587 then I sent some data according to the SMTP protocol (without authentication, which should be the way my local MTA does) and the message was not delivered. Using the MTA it does, but why? – Alix Axel Feb 17 '11 at 00:39
  • What exactly did you try and send? In particular were you trying to relay, i.e. not send a message to that end server? MTAs need lots of defences against spammers so you may have triggered something accidentally which got your message binned. – Rup Feb 17 '11 at 00:56
  • @Rup: I was trying to send an email to my Gmail address without authenticating and I got a reply of `530-5.5.1 Authentication Required` when I sent the `MAIL FROM` command. – Alix Axel Feb 17 '11 at 01:02
  • 1
    Works for me, sorrry - connecting to gmail-smtp-in.l.google.com port 25. I can `HELO test` and `MAIL FROM:` and get a positive response. – Rup Feb 17 '11 at 01:07
  • @Rup: Yup, works for me too. The problem was I was connecting via SSL (kinda dumb I know). I'm amazed how easy it was, it's just the pure old SMTP protocol in action. Thanks! :) – Alix Axel Feb 17 '11 at 01:31
  • 1
    Ah, right - yes, I think the SSL SMTP on 587 is for sending email out not in. So that would need authentication yes. For SSL in I think you make a plain connection on port 25 then request an upgrade to TLS on the same connection with `STARTTLS` – Rup Feb 17 '11 at 01:35
  • So its like a router? Why would you need such a thing when we already have routers? – B T Jan 14 '16 at 23:41
3

Edit: The original tutorial I linked to has gone 404. Here's another that's ok: https://troubleshootguru.wordpress.com/2014/07/06/mail-server-components-mta-mda-mua/

In short, a MUA is a user client that uses SMTP to send an email to an MTA. The MTA is a server that is responsible for routing the MTA to its destination. If that destination is another server, the MTA hands the email to an MDA. The MDA is a client on the server that uses SMTP to forward the email to the other server, which is also an MTA.

So what do you need to learn? If you want to write an MUA or MDA, you need to learn how to open a socket to another computer, send SMTP commands, and receive SMTP responses. If you want to write an MTA, you need to learn how to listen for socket connections on a port, receive SMTP commands, and send SMTP responses.

james.garriss
  • 12,959
  • 7
  • 83
  • 96