Subject with SMTPLIB?

Tim Roberts timr at
Wed Mar 14 07:33:07 CET 2001

root <root at> wrote:
>I need to write some mail for my fax-log evaluating script. I'm
>currently using the smtplib to simply send some mails. But how 
>can I get (without studying RFCs and papers on SMTP for years)
>a SUBJECT for my mails?

There have already been several replies to this, but I'm not sure they
really explained the root principle here.  This is an important and
non-intuitive point, although perhaps a bit off-topic for this newsgroup.

<enter lecture mode>

smtplib is concerned with SMTP.  That is the protocol for transferring
e-mail messages, described in RFC 821.  SMTP cares only about the
originator and the recipients of your message.  Everything else about the
message, including the body AND all of the headers, is just random text to
SMTP.  None of the e-mail headers you are accustomed to are part of the
protocol at all.  Here is a perfectly valid SMTP session in which I send a
message to myself:

  MAIL FROM: <timr at>
  RCPT TO: <timr at>
  From: SMTP doesn't care what I put here
  To:   SMTP doesn't care what I put here
  Subject: SMTP doesn't care what I put here
  Blech: Headers don't matter

  Message body.

Everything up to the "DATA" command is called the "envelope".  This is what
SMTP servers like sendmail use to direct the transmission of the message.
The headers in the message itself (which are defined in a separate RFC,
822) are irrelevant to SMTP; they are just part of the payload.

The headers in the payload are only there for the mail reader at the other
end.  If you want the mail reader to show a subject for your message, you
need to add a Subject: header.  Further, if you want the mail reader to
show a valid sender and recipient, YOU have to include correctly formatted
"From:" and "To:" headers.  The addresses you pass to the smtplib.sendmail
method are STRICTLY for the envelope: the SMTP MAIL and RCPT commands.
smtplib.sendmail does not add the "From:" and "To:" headers, because they
are not part of SMTP.

For the benefit of modern mail readers, it is important to get the "From:"
and "To:" lines correct.  If I actually sent the message I showed above, my
mail reader would discard it as spam, because my address is not present in
the "To:" or "Cc:" headers.  However, the exchange is perfectly valid SMTP.

<exit lecture mode>
- Tim Roberts, timr at
  Providenza & Boekelheide, Inc.

More information about the Python-list mailing list