[Tutor] SMTPLIB module - how to add subject ?

Sheila King sheila at spamcop.net
Sat Sep 15 17:05:40 EDT 2001


On Sat, 15 Sep 2001 22:45:18 +0200, "A" <printers at sendme.cz>  wrote
about [Tutor] SMTPLIB module - how to add subject ?:

:Hi,
:I tried 
:11.11.2 SMTP Example from Python docs.
:Does anyone know how I can add subject to an email send by this example?
:Thank you very much for help
:Ladislav

I've pasted the example below. Actually, I really dislike this example.
They are using
"\r\n"
as a character for newlines. Whose idea was that???
It should be just
"\n"

Below this sample code I've pasted some suggested mods:

import smtplib
import string

def prompt(prompt):
    return raw_input(prompt).strip()

fromaddr = prompt("From: ")
toaddrs  = prompt("To: ").split()
print "Enter message, end with ^D:"

# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n"
       % (fromaddr, string.join(toaddrs, ", ")))
while 1:
    try:
        line = raw_input()
    except EOFError:
        break
    if not line:
        break
    msg = msg + line

print "Message length is " + `len(msg)`

server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()


Suggested mods:

Add a line that prompts for the subject:

subj = prompt("Subject: ")

Then change this line:

msg = ("From: %s\r\nTo: %s\r\n\r\n"
       % (fromaddr, string.join(toaddrs, ", ")))

To something like this:

msg = ("From: %s\r\nTo: %s\r\nSubject:%s\r\n\r\n"
       % (fromaddr, string.join(toaddrs, ", "), subj))

Caution! untested, but should work.

I still don't like the "\r\n" in this example. Bad. :(

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/





More information about the Python-list mailing list