Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: send email

Reply
Thread Tools

Re: send email

 
 
Peter Nuttall
Guest
Posts: n/a
 
      09-25-2003
On Thursday 25 Sep 2003 4:17 am, Ian Bicking wrote:
> On Wednesday, September 24, 2003, at 09:04 PM, Alberto Vera wrote:
> > Could you tell me How I can send an email using WIN2000? What do I
> > need?

>
> Use the smtplib module, and maybe the new email module to create the
> email message (but you won't need that if you want to create simple
> email messages).
>
> Ian


here is a program to send emails with python. It was writien quickly so it is
not perfect.

import smtplib

winth_of_email=50

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 + '\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] + '\n' + msg[chop:]
x+=1
message = message +'\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 ')

message = 'From: '+fromaddr +'\n'+ 'To: '+toaddrs +'\n'+ 'Subject '+ subject +
'\n' +message


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

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

if anyone has any comments I would like to see them.

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.


 
Reply With Quote
 
 
 
 
Riccardo Attilio Galli
Guest
Posts: n/a
 
      09-25-2003
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.
>
> Peter


peter, your program write 'e-mail' absolutely not rfc2822 compliant.
read
http://www.faqs.org/rfcs/rfc2822.html
to know how should be a real e-mail message

This is an example of how the simplest e-mail should look like:

======================================
From: John Doe <(E-Mail Removed)>
To: Mary Smith <(E-Mail Removed)>
Subject: Saying Hello
Date: Fri, 21 Nov 1997 09:55:06 -0600
Message-ID: <(E-Mail Removed)>

This is a message just to say hello.
So, "Hello".
======================================

remembering that every line MUST end whit "\r\n"

Please try to make your program sending correct e-mail, because it's hard
to develop parser which understand e-mail written in non standard format.

Ciao,
Riccardo

--
-=Riccardo Galli=-

_,e.
s~ ``
~@. ideralis Programs
.. ol
`**~ http://www.sideralis.net
 
Reply With Quote
 
 
 
 
Peter Nuttall
Guest
Posts: n/a
 
      09-25-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.


 
Reply With Quote
 
Riccardo Attilio Galli
Guest
Posts: n/a
 
      09-25-2003
On Thu, 25 Sep 2003 16:25:18 +0100, Peter Nuttall wrote:

> On Thursday 25 Sep 2003 2:51 pm, Riccardo Attilio Galli wrote:
>> On Thu, 25 Sep 2003 11:46:16 +0100, Peter Nuttall wrote:
>> > if anyone has any comments I would like to see them.

> <snip>
>>
>> peter, your program write 'e-mail' absolutely not rfc2822 compliant.

> <snip>


> 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?


I'm in a hurry, so I can't loock at rfc. I think it's too simple. It
should be something really hard to generate again. Look at some real
e-mail you received.

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


Probably, but I can't tell it now.

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


It's over the limit. 78 is with \r\n.

> Here is the new code:


there's a bug here

> while end == 0:
> msg=raw_input()
>
> if msg=='':
> if new_line_1=='1':


If I would press enter in the first line, it raise a NameError (new_line_1
is not defined)
So define it before the if.


> Thanks again
>
> Peter


Good luck,
Riccardo

--
-=Riccardo Galli=-

_,e.
s~ ``
~@. ideralis Programs
.. ol
`**~ http://www.sideralis.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
which email module - MIME::LITE, Email::Send, other? brewsterbear@googlemail.com Perl Misc 2 01-05-2007 09:45 AM
How to have email form send email and start file download bmurphy@xlrotor.com Javascript 13 05-14-2006 06:27 PM
Cannot compose ,reply or send email on Sasktel email system pcbutts1 Computer Support 5 07-29-2005 06:13 AM
why cant i send email messages from this email Billie Jo Ames Computer Support 8 06-30-2005 12:24 PM
HowTo:? open email client to send email Peter ASP .Net 0 07-01-2003 04:58 PM



Advertisments