Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 4th, 2004, 10:29 PM
Noah_C Noah_C is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 34 Noah_C User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 31 sec
Reputation Power: 5
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

Reply With Quote
  #2  
Old June 4th, 2004, 10:43 PM
Peyton Peyton is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 10 Peyton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 48 sec
Reputation 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()

Reply With Quote
  #3  
Old June 4th, 2004, 10:56 PM
Noah_C Noah_C is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 34 Noah_C User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 31 sec
Reputation Power: 5
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

Reply With Quote
  #4  
Old June 4th, 2004, 11:26 PM
Peyton Peyton is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 10 Peyton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 48 sec
Reputation Power: 0
Try '[toaddrs]' instead of 'toaddrs.'

Reply With Quote
  #5  
Old June 4th, 2004, 11:47 PM
Noah_C Noah_C is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 34 Noah_C User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 31 sec
Reputation Power: 5
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....

Reply With Quote
  #6  
Old June 5th, 2004, 01:13 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #7  
Old June 5th, 2004, 02:29 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,224 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 23 h 4 m 39 sec
Reputation Power: 263
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

Reply With Quote
  #8  
Old June 5th, 2004, 04:43 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #9  
Old June 5th, 2004, 10:05 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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/Pyth...n-on-the-Web/3/

Mark.

Reply With Quote
  #10  
Old June 5th, 2004, 10:56 AM
Noah_C Noah_C is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 34 Noah_C User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 31 sec
Reputation Power: 5
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()

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Python Question.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT