send email

Peter Nuttall pnu at ukfsn.org
Thu Sep 25 17:25:18 CEST 2003


On Thursday 25 Sep 2003 2:51 pm, Riccardo Attilio Galli wrote:
> On Thu, 25 Sep 2003 11:46:16 +0100, Peter Nuttall wrote:
> > here is a program to send emails with python. It was writien quickly so
> > it is not perfect.
> > [SNIP]
> >
> > if anyone has any comments I would like to see them.
<snip>
>
> peter, your program write 'e-mail' absolutely not rfc2822 compliant.
<snip>

> Ciao,
> Riccardo

Yeah, I thought It was not compliant. Thanks for the weblink. I have changed 
it based on the standard and what you said, but I have a few questions:

1. message-ID: I have written an few lines of code that have the date and time 
on one side of a @ and the computer's name of the other. is that OK?

2. is there a better way to make the date string that the loop that I have?

3. line length: Is having it set to 78 ok or is that 2 over the limit?

Here is the new code:

import smtplib
from time import gmtime, strftime, localtime
 
winth_of_email=78

message = ''

print 'type message here:'
    
end = 0

while end == 0:
    msg=raw_input()
    
    if msg=='':
        if new_line_1=='1':
            print 'end of message'
            end=1
        else:
            new_line_1='1'
            message = message + '\r\n'
    else:
        new_line_1=''
        new_line_2=''
        lent=len(msg)/winth_of_email
        x=0
        while x <= lent:
            chop=(x+1) * winth_of_email+x
            msg = msg[:chop] + '\r\n' + msg[chop:]
            x+=1
        message = message +'\r\n'+ msg


#adds headers to email
fromaddr=raw_input('what is the from address ')
toaddrs=raw_input('what is the to address ')
subject=raw_input('what is the subject ')
sever=raw_input('what is the smtp sever you are using ')
timesend=strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
#msg-id maker
#change lupin to the name of your computer

setime=localtime()
x=0
msg_id=''
while x<5:
    msg_id=msg_id+str(setime[x]) 
    x=x+1
msg_id = msg_id + '@lupin'


finalmessage = 'From: '+fromaddr +'\r\n'+ 'To: '+toaddrs 
finalmessage = finalmessage +'\r\n'+ 'Subject: '+ subject +'\r\n' + 'Date: ' + 
timesend
finalmessage = finalmessage + '\r\n' +'Message-ID: ' + msg_id + '\r\n' + '\r
\n' +message


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

print
#code for sending email
server = smtplib.SMTP(sever)
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, finalmessage)
server.quit()

Thanks again

Peter

-- 
Someone esle can help you better than I can.

Every time I think that perhaps we are an advanced race, I turn around and 
read ramblings on Slashdot, and realize I was wrong.






More information about the Python-list mailing list