#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    37
    Rep Power
    10

    Python Question.


    Code:
    import smtplib
    toaddrs = "noah@hotmail.com"
    fromaddr = raw_input("What address do you want to send from? ")
    name = raw_input("What's your first  name ?")
    name2 = raw_input("What's your last name ?")
    msg = "required First Name=", name,\n\"required Last Name=", name2
    print msg
    server = smtplib.SMTP('smtp.netins.net')
    server.sendmail(fromaddr, toaddrs, msg)
    server.quit()

    File "C:\Python\SMTP.py", line 6
    msg = "required First Name=", name,\n\"required Last Name=", name2
    ^
    SyntaxError: invalid token


    There is the error I get.

    What I'm trying to do is ask the user a group of questions and then have the ansewers e-mailed to me....

    Anyhelp would be appreciated. I've looked around and managed to get it all working expect for this small syntax problem. Ideally the e-mail will look like... each object on a new line.

    Required First name=joey
    last name=Johnson
    Test=test
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    10
    Rep Power
    0
    Try this:

    Code:
    import smtplib
    toaddrs = "noah@hotmail.com"
    fromaddr = raw_input("What address do you want to send from? ")
    name = raw_input("What's your first  name ?")
    name2 = raw_input("What's your last name ?")
    msg = "required First Name=", name, "\nrequired Last Name=", name2
    print msg
    server = smtplib.SMTP('smtp.netins.net')
    server.sendmail(fromaddr, toaddrs, msg)
    server.quit()
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    37
    Rep Power
    10
    Thanks, that fixed the syntax error but now I get this....
    Code:
    Traceback (most recent call last):
      File "C:\Python\test.py", line 9, in ?
        server.sendmail(fromaddr, toaddrs, msg)
      File "C:\Python23\lib\smtplib.py", line 688, in sendmail
        (code,resp) = self.data(msg)
      File "C:\Python23\lib\smtplib.py", line 481, in data
        q = quotedata(msg)
      File "C:\Python23\lib\smtplib.py", line 189, in quotedata
        re.sub(r'(?:\r\n|\n|\r(?!\n))', CRLF, data))
      File "C:\Python23\lib\sre.py", line 143, in sub
        return _compile(pattern, 0).sub(repl, string, count)
    TypeError: expected string or buffer
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    10
    Rep Power
    0
    Try '[toaddrs]' instead of 'toaddrs.'
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    37
    Rep Power
    10
    Peyton,

    That didn't help. So then I thought since I was using example variables such as 'fromaddr', 'toaddrs', 'msg' maybe those were the problem so I changed those. I've come up with two differen't versions of this program. One works and one doesn't. I'm going to keep adding features I guess until she breaks...and then report back. Thanks for the help so far.

    This works...
    Code:
    import smtplib
    green = "noah@hotmail.com"
    blue = raw_input("What address do you want to send from? ")
    yellow = raw_input("What's your first  name ?")
    messege = "required First Name="
    print messege
    server = smtplib.SMTP('smtp.netins.net')
    server.sendmail(green, blue, messege)
    server.quit()

    this doesn't...

    Code:
    import smtplib
    green = "noah@hotmail.com"
    blue = raw_input("What address do you want to send from? ")
    name = raw_input("What's your first  name ?")
    name2 = raw_input("What's your last name ?")
    messege = "required First Name=", name, "\nrequired Last Name=", name2
    print messege
    server = smtplib.SMTP('smtp.netins.net')
    server.sendmail(green, blue, messege)
    server.quit()
    EDIT:....
    When the following line

    messege = "required First Name="

    becomes

    messege = "required First Name=",yellow

    Code:
    Traceback (most recent call last):
      File "C:\Python\test.py", line 8, in ?
        server.sendmail(green, blue, messege)
      File "C:\Python23\lib\smtplib.py", line 688, in sendmail
        (code,resp) = self.data(msg)
      File "C:\Python23\lib\smtplib.py", line 481, in data
        q = quotedata(msg)
      File "C:\Python23\lib\smtplib.py", line 189, in quotedata
        re.sub(r'(?:\r\n|\n|\r(?!\n))', CRLF, data))
      File "C:\Python23\lib\sre.py", line 143, in sub
        return _compile(pattern, 0).sub(repl, string, count)
    TypeError: expected string or buffer
    That error occurs....
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I thinkwhat pyton was trying to say was blue needs to be a sequence of addresses, not a string as it is in your example right now. I actually have a working example of this somewhere which i'l have a look for a little later.

    Code:
    ...
    server.sendmail(green, [blue], messege)
    ...
    Hope this helps, but then, i've had alot of problems trying to sent emails with Python before. What Platform are you on. I'm also knowticing that your not constructing any mime headers from the data.

    You might also wana turn debugging by adding this line above the previous sendmail() call.

    Code:
    ...
    sever.set_debuglevel(1)
    ...
    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    The problem is that messege[sic] needs to be a string. You are assigning a tuple to it.

    This is what you are doing:
    Code:
    >>> name = 'foo'; name2 = 'bar'
    >>> messege = "required First Name=", name, "\nrequired Last Name=", name2
    >>> messege
    ('required First Name=', 'foo', '\nrequired Last Name=', 'bar')
    Instead of separating the elements of messege with commas, you need to concatenate them with the '+' operator.

    Code:
    >>> messege = "required First Name=" + name + "\nrequired Last Name=" + name2
    >>> messege
    'required First Name=foo\nrequired Last Name=bar'
    >>>
    Dave - The Developers' Coach
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    May i sugest that in future you use more discriptive naming conventions Noah. Not sure how the colors relate to email or addresses is all .

    Mark.
    programming language development: www.netytan.com Hula

  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ok i found it , i knew that the article was around somewhere.. though i expected it to be on devshed. anyway here it is:

    http://www.devarticles.com/c/a/Pytho...-on-the-Web/3/

    Mark.
    programming language development: www.netytan.com Hula

  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    37
    Rep Power
    10
    Thanks guys, she works great!
    I'm going to do some reading so I understand certian things didn't work.... Also, the "blue", "green", "yellow" was just to prototype and see if I could figure out what is wrong...

    Thanks again. Below is the fixed code that works incase someone wants to use it/needs help with it in the future.
    Code:
    import smtplib
    toaddr = "noah@hotmail.com"
    fromaddr = raw_input("What address do you want to send from? ")
    name = raw_input("What's your first  name ?")
    name2 = raw_input("What's your last name ?")
    message = "required First Name=" + name + "\nrequired Last Name=" + name2
    print message
    server = smtplib.SMTP('smtp.netins.net')
    server.sendmail(fromaddr, toaddr, message)
    server.quit()

IMN logo majestic logo threadwatch logo seochat tools logo