[Tutor] smtplib howto send with a subject line

Mike Hansen Mike.Hansen at atmel.com
Wed May 23 22:14:35 CEST 2007


 

> -----Original Message-----
> From: tutor-bounces at python.org 
> [mailto:tutor-bounces at python.org] On Behalf Of Daniel McQuay
> Sent: Wednesday, May 23, 2007 1:49 PM
> To: tutor at python.org
> Subject: [Tutor] smtplib howto send with a subject line
> 
> Hey Guys, I'm having a problem with my script that sends out 
> an email using smtplib. Whats happening now is when it is 
> send I get a (no subject) where the subject line should be. I 
> checked a few places such as effbot and some other online doc 
> but couldn't find any thing about the subject line. 
> 
> Any help would be much appreciated.
> 
> ###################################
> #Created by: Daniel McQuay      
> #This script will send and email  
> #to verify if the backup was      
> #successful or not.               
> ###################################
> 
> import smtplib
> import sys
> 
> emmssg = "/tmp/backup.log"
> smtpserver = 'localhost'
> AUTHREQUIRED = 0
> smtpuser = '' 
> smtppass = ''  
> 
> #Recipients who will be getting the emails
> RECIPIENTS = ['simplebob at gmail.com']
> 
> SENDER = 'Saint_Richards'
> 
> #This does not work?
> SUBJECT = 'Backup_Log' 
> 
> emmssg = open('/tmp/backup.log','r').read()
> 
> session = smtplib.SMTP(smtpserver)
> if AUTHREQUIRED:
>     session.login(smtpuser, smtppass)
> smtpresult = session.sendmail(SENDER, RECIPIENTS, SUBJECT, emmssg) 
> 
> if smtpresult:
>     errstr = ""
>     for recip in smtpresult.keys():
>         errstr = """Could not delivery mail to: %s
> 
> Server said: %s
> %s
> 
> %s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr) 
>     raise smtplib.SMTPException, errstr
> 
> Thanks in advance,
> 
> -- 
> Daniel McQuay
> Jaluno.com
> H: 814.825.0847
> M: 814-341-9013 
> 

It seems a little easier to use the email module along with the smtp
module to create and send a message. 

http://docs.python.org/lib/node162.html

Mike


More information about the Tutor mailing list