|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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()
|
|
#3
|
|||
|
|||
|
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
|
|
#4
|
|||
|
|||
|
Try '[toaddrs]' instead of 'toaddrs.'
|
|
#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.... |
|
#6
|
||||
|
||||
|
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. |
|
#7
|
|||
|
|||
|
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 |
|
#8
|
||||
|
||||
|
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. |
|
#9
|
||||
|
||||
|
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. |
|
#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()
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Python Question. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|