Re: send email

Discussion in 'Python' started by Peter Nuttall, Sep 25, 2003.

  1. 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.
     
    Peter Nuttall, Sep 25, 2003
    #1
    1. Advertising

  2. 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 <>
    To: Mary Smith <>
    Subject: Saying Hello
    Date: Fri, 21 Nov 1997 09:55:06 -0600
    Message-ID: <>

    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
     
    Riccardo Attilio Galli, Sep 25, 2003
    #2
    1. Advertising

  3. 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.
     
    Peter Nuttall, Sep 25, 2003
    #3
  4. 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
     
    Riccardo Attilio Galli, Sep 25, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter
    Replies:
    0
    Views:
    3,431
    Peter
    Jul 1, 2003
  2. Replies:
    2
    Views:
    445
    Tintin
    Jan 5, 2007
  3. Replies:
    13
    Views:
    551
    Randy Webb
    May 14, 2006
  4. pbd22
    Replies:
    1
    Views:
    190
  5. ashish makani
    Replies:
    5
    Views:
    697
    Mark Lawrence
    Sep 20, 2012
Loading...

Share This Page