smtp question

Max M maxm at mxm.dk
Wed Jan 5 16:44:09 EST 2005


Philippe C. Martin wrote:
> Hi,
> 
> I am testing the smtp module and have the following question:
> 
> in the code below (taken from net sample) prior to adding the "Subject:"
> field, the email client found the "From" and the "To". Without the
> "Subject:" field on I get this:
> 
> Email client = Evolution: the "From" field is blank
> Email client = KMail: the "To" field is blank
> 
> Any clue ?

It' very easy to create email messages with the email module.

 >>> from email.Message import Message
 >>> fromaddr = 'maxm at mxm.dk'
 >>> to = 'maxm at mxm.dk'
 >>> msg = Message()
 >>> msg['Subject'] = 'From Python'
 >>> msg['From'] = fromaddr
 >>> msg['To'] = to
 >>> body = "This is the message content"
 >>> msg.set_payload(body)

Thats all. Though some smtp servers needs a Date header too, to work.

 >>> from time import gmtime, strftime
 >>> msg['Date'] = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())

Sending the message via the smtp module is even simpler.

 >>> import smtplib
 >>> server = smtplib.SMTP('localhost')
 >>> server.sendmail(fromaddr, [to], msg.as_string())
 >>> server.quit()


-- 

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science



More information about the Python-list mailing list