smtp question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philippe C. Martin

    #1

    smtp question

    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 ?

    Thanks and regards,

    Philippe
    *************** *******

    import smtplib

    def prompt(prompt):
    return raw_input(promp t).strip()


    fromaddr = prompt("From: ")
    toaddrs = prompt("To: ").split()
    print "Enter message, end with ^D (Unix) or ^Z (Windows):"

    # Add the From: and To: headers at the start!
    msg = ("From: %s\r\nTo: %s\r\n\r\n" % (fromaddr, ", ".join(toaddrs) ))
    while 1:
    try:
    line = raw_input()
    except EOFError:
    break
    if not line:
    break
    msg = msg + line

    print "Message length is " + repr(len(msg))

    server = smtplib.SMTP('s mtp.sbcglobal.y ahoo.com')
    server.set_debu glevel(1)
    server.login ('xxxxx','yyyyy yy')
    server.sendmail (fromaddr, toaddrs, 'Subject:from python\n\n'+msg )
    server.quit()









    --
    *************** ************
    Philippe C. Martin
    SnakeCard LLC
    Secure the right domain name for your business or website today. Custom tailored payment plans available to fit any budget.

    *************** ************

  • Kartic

    #2
    Re: smtp question

    Philippe,

    Looks like the problem lies where you have added the Subject header.
    You have terminated it with a \n\n and then your From and To headers.
    You may want to rewrite it as:

    server.sendmail (fromaddr, toaddrs, 'Subject:from python\r\n'+msg )

    Why dont you consider using the email module -
    http://python.org/doc/2.4/lib/module-email.html ? It is elegant and
    easy to use.

    Thank you,
    --Kartic

    Comment

    • Mike Meyer

      #3
      Re: smtp question

      "Philippe C. Martin" <philippecmarti n@sbcglobal.net > writes:
      [color=blue]
      > 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 ?
      >
      > Thanks and regards,
      >
      > Philippe
      > *************** *******
      >
      > import smtplib
      >
      > def prompt(prompt):
      > return raw_input(promp t).strip()
      >
      >
      > fromaddr = prompt("From: ")
      > toaddrs = prompt("To: ").split()
      > print "Enter message, end with ^D (Unix) or ^Z (Windows):"
      >
      > # Add the From: and To: headers at the start!
      > msg = ("From: %s\r\nTo: %s\r\n\r\n" % (fromaddr, ", ".join(toaddrs) ))
      > while 1:
      > try:
      > line = raw_input()
      > except EOFError:
      > break
      > if not line:
      > break
      > msg = msg + line
      >
      > print "Message length is " + repr(len(msg))
      >
      > server = smtplib.SMTP('s mtp.sbcglobal.y ahoo.com')
      > server.set_debu glevel(1)
      > server.login ('xxxxx','yyyyy yy')
      > server.sendmail (fromaddr, toaddrs, 'Subject:from python\n\n'+msg )[/color]

      You've got \n\n after the Subject line, not \r\n. That is being treated
      as two newlines, thus ending the headers.

      <mike

      --
      Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
      Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

      Comment

      • Max M

        #4
        Re: smtp question

        Philippe C. Martin wrote:[color=blue]
        > 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 ?[/color]

        It' very easy to create email messages with the email module.
        [color=blue][color=green][color=darkred]
        >>> from email.Message import Message
        >>> fromaddr = 'maxm@mxm.dk'
        >>> to = 'maxm@mxm.dk'
        >>> msg = Message()
        >>> msg['Subject'] = 'From Python'
        >>> msg['From'] = fromaddr
        >>> msg['To'] = to
        >>> body = "This is the message content"
        >>> msg.set_payload (body)[/color][/color][/color]

        Thats all. Though some smtp servers needs a Date header too, to work.
        [color=blue][color=green][color=darkred]
        >>> from time import gmtime, strftime
        >>> msg['Date'] = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())[/color][/color][/color]

        Sending the message via the smtp module is even simpler.
        [color=blue][color=green][color=darkred]
        >>> import smtplib
        >>> server = smtplib.SMTP('l ocalhost')
        >>> server.sendmail (fromaddr, [to], msg.as_string() )
        >>> server.quit()[/color][/color][/color]


        --

        hilsen/regards Max M, Denmark


        IT's Mad Science

        Comment

        • Tim Roberts

          #5
          Re: smtp question

          "Philippe C. Martin" <philippecmarti n@sbcglobal.net > wrote:
          [color=blue]
          >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 ?[/color]

          Absolutely.
          [color=blue]
          >server = smtplib.SMTP('s mtp.sbcglobal.y ahoo.com')
          >server.set_deb uglevel(1)
          >server.login ('xxxxx','yyyyy yy')
          >server.sendmai l(fromaddr, toaddrs, 'Subject:from python\n\n'+msg )
          >server.quit( )[/color]

          You have two newlines following the Subject: line. That inserts a blank
          line, which terminates the headers. Everything else, including the From:
          and To: lines, will be taken as part of the message body. I assume you
          meant to use \r\n, but \n will work just as well and is less error prone.
          [color=blue]
          >print "Message length is " + repr(len(msg))[/color]

          Easier is:
          print "Message length is", len(msg)
          More efficient is:
          print "Message length is %d" % len(msg)
          --
          - Tim Roberts, timr@probo.com
          Providenza & Boekelheide, Inc.

          Comment

          Working...