Chapter 13 ■ SMtp
246
Sending E-Mail
Before sharing the nitty-gritty details of the SMTP protocol, a warning is in order: if you are writing an interactive
program, daemon, or web site that needs to send e-mail, then your site or system administrator (in cases where that is
not you) might have an opinion about how your program sends e-mail, and they might save you a lot of work by doing so!
As noted previously, successfully sending e-mail generally requires a queue where a message can sit for seconds,
minutes, or even days until it can be successfully transmitted toward its destination. Thus, you typically do not want
your front-end programs using Python’s smtplib to send e-mail directly to a message’s destination, because if your
first transmission attempt fails, then you will be stuck with the job of writing a full mail transfer agent (MTA), as the
RFCs call an e-mail server, and giving it a full standards-compliant retry queue. This is not only a big job, but it is also
one that has already been done well several times, and you will be wise to take advantage of one of the existing MTAs
(look at postfix, exim, and qmail) before trying to write something on your own.
Only rarely will you be making SMTP connections out into the world from Python. More often, your system
administrator will tell you one of two things:
• That you should make an authenticated SMTP connection to a e-mail server that already exists
within your organization, using a username and password that will belong to your application.
• That you should run a local binary on the system—like the sendmail program—that the
system administrator has already gone to the trouble of configuring so that local programs can
send e-mail.
The Python Library FAQ has sample code for invoking a sendmail compatible program. Take a look at the section
“How do I send mail from a Python script?” found at http://docs.python.org/faq/library.html.
Because this book is about networking, I won’t cover this possibility in detail. However, remember to do raw
SMTP yourself only when no simpler mechanism exists on your machine for sending e-mail.
Headers and the Envelope Recipient
The key concept involved in SMTP that consistently confuses beginners is that the addressee headers you are so
familiar with—To, Cc (carbon copy), and Bcc (blind carbon copy)—are not consulted by the SMTP protocol to decide
where your e-mail goes! This surprises many users. After all, almost every e-mail program in existence asks you to fill
in those addressee fields, and when you click Send, the message wings it way toward those mailboxes. What could be
more natural? But it turns out that this is a feature of the e-mail client itself, not of the SMTP protocol: the protocol
knows only that each message has an “envelope” around it naming a sender and some recipients. SMTP itself does not
care whether those names are ones that it can find in the headers of the message.
That e-mail must work this way will actually be quite obvious if you think for a moment about the Bcc blind
carbon-copy header. Unlike the To and Cc headers, which make it to the e-mail’s destination and let each recipient
see who else was sent that e-mail, the Bcc header names people whom you want to receive the e-mail without any of
the other recipients knowing. Blind copies let you quietly bring a message to someone’s attention without alerting the
other recipients of the e-mail.
The existence of a header like Bcc, which can be present when you compose a message but does not actually get
included in the outgoing message, raises two points:
• Your e-mail client edits your message’s headers before sending it. Besides removing the Bcc header
so that none of the e-mail’s recipients gets a copy of it, the client typically adds headers as well,
such as a unique message ID and perhaps the name of the e-mail client itself (an e-mail I just
received on my desktop, for example, identifies the X-Mailer that sent it as YahooMailClassic).
• An e-mail can pass across SMTP toward a destination address that is not mentioned anywhere
in the e-mail headers or text itself—and it can do this for the most legitimate of reasons.