Hi,
Of late, there have been a lot of queries regarding emails on this
list. Here is a general writeup on the mechanism of email.
Basically, when you receive emails on your computer, there are three
programs in action. The first one is called the Mail Transport Agent,
or MTA. sendmail is the most popular one, although others like qmail
exist. This MTA is responsible for receiving emails from the outside
world that are meant for users on your machine. Sendmail typically works
by opening a TCP connection on port 25 of your computer and listening
for incoming connections.
The second program is called the Mail Delivery Agent, or MDA. Procmail
is by far, the best MDA available. Once sendmail gets an email, it hands
the mail over to procmail and it is procmail's job to deliver the mail to
the mailbox it is destined to (after parsing a ruleset the user defines,
to filter emails). In case procmail is absent, sendmail itself does this
job, or you can set up other programs to do this.
The third program is called the Mail User Agent or MUA. There are a host
of them available for unix, from the archaic and cryptic mailx to the
most advanced of them, mutt. :-) This is the program that presents the
mail to the person to whom it is addressed to, and allows him to decide
what to do with the mail.
Now, what happens when one sends an email? The MUA hands the mail over
to the MTA, in one of two ways. Mutt and pine saves the mail in a text
file and invokes sendmail with that file as one of the arguments (ie,
something like sendmail < /tmp/mutt-machname-pid-x). Netscape messenger
makes a direct connection to port 25 of the mailserver specified and
talks SMTP directly there.
In the first scenario, sendmail looks in its configuration and checks if
there are any relay hosts specified. If there is one, a TCP connection
is made to that host port 25, and the mail is handed over to the MTA
there. Otherwise, sendmail does an MX (mail exchange) lookup on the domain
name in the To: field of the email. Then it opens a TCP connection to
that host, and delivers the mail to the MTA there. (I hope this answers
the relaying related question that Mr. Kunal Parikh had posted, he has
to enter the relay host's name in /etc/sendmail.cf - DSrelay.hosts.name).
Now where do the other programs like imapd, pop3d and fetchmail come into
this picture? As I said, the MTA does an MX lookup on the To: address,
and delivers the mail there. Usually, this is the name of an ISP or
a central mailserver of a large organisation. Now, people want their
emails on their workstations, which don't have MX records typically. So,
these programs help them to download emails onto their workstations. Both
imapd and pop3d listen on a particular TCP port (143 for IMAP, 110 for
POP3). When a mail retrieval utility (like fetchmail or netscape) makes a
connection to one of these ports, then the program that listens on this
port does an authentication (to make sure that the person downloading
the email is indeed the person to whom it is addressed to).
Thus, fetchmail retrieves email from the mailservers you specify, and
then delivers the email to the local MTA. Then the email goes through
the cycle of MTA-MDA-MUA, before you read the mail.
Now, I hope it is clear that the common MUAs cannot handle email
filtering. It is the job of the MDA to filter emails according to
specific rules. Procmail, the common unix MDA, has a very powerful ruleset
that allows you to do almost anything with emails. I know a person who
handled a mailing list entirely via procmail recipes. The documentation
of procmail is excellent and almost anyone can setup one by just going
through the man pages.
You can try out MX record lookup using nslookup. Just type
set querytype=mx at the nslookup prompt.
When you send me an email, it goes through six different programs, after
it enters retortsoft.com. I am always amazed at the workings of SMTP, so
many programs co-ordinate with each other, and I have not lost a mail yet.
Any errors in the above, please report to me.
Any further queries, comments and follow-ups are welcome.
Regards,
Binand
References: Various RFCs, particularly 821, man pages and faqs for
sendmail and procmail and W. Richard Stevens' books.
--
#include <stdio.h> | Binand Raj S.
char *p = "#include <stdio.h>%cchar *p = %c%s%c; | This is a self-
int main(){printf(p,10,34,p,34,10);return 0;}%c"; | printing program.
int main(){printf(p,10,34,p,34,10);return 0;} | Try it!!
PGP signature