smtplib: subject? to?

Michael P. Reilly arcege at shore.net
Fri Sep 3 10:40:17 EDT 1999


Gerrit Holl <gerrit.holl at pobox.com> wrote:
: Hello,

: when I try to send mail with python, I can't get a To: and a Subject: header!
: How do I get them?

: regards,
: Gerrit.

How are you trying to send email?  smtplib? popen to sendmail?

With smtplib, you need to know your mail forwarder.
  import smtplib

  mailer = smtplib.SMTP('mailhost')
  try:
    mailer.sendmail('arcege at shore.net', ['gerrit.holl at pobox.com'], '''\
  From: (Arcege) arcege at shore.net
  To: gerrit.holl at pobox.com
  Subject: Testing smtplib within Python

  It seems to work.''')
    mailer.quit()
  except SMTPException, why:
    print why

With sendmail, you just need the binary.
  import os

  mailer = os.popen('sendmail -t -i', 'w')
  mailer.write('''\
  From: (Arcege) arcege at shore.net
  To: gerrit.holl at pobox.com
  Subject: Testing smtplib within Python

  It seems to work.
  ''')
  rc = mailer.close()

With both, you need to send the entire messages, including headers; the
blank line seperates the headers from the message body.

  -Arcege





More information about the Python-list mailing list