0

I'm having the user input their email address. However, I'm assuming Python isn't recognizing the special character "@" but I'm not sure how recode/have the user input recognize that special character as part of the string.

import smtplib

sender_email = "kingofeverything@test.com"
rec_email = input('Enter a email to send to: ')
password = "Password"
SUBJECT = "Subject Line Test"
TEXT = "Test Text"
message = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender_email, password)
print("Login success")
server.sendmail(sender_email, rec_email, message)
print("Email has been ent to ", rec_email)
server.quit()

How do I correct this?

#Error: using kingofeverything@gmail.com as my input
Traceback (most recent call last):
  File "testss.gyp", line 4, in <module>
    rec_email = input('Enter a email to send to: ')
  File "<string>", line 1
    kingofeverything@gmail.com
                    ^
SyntaxError: invalid syntax
  • 2
    Is the `// MARK ...` actually part of the code? Python uses `#` to start comments. Otherwise, your code works for me without error. – BruceWayne Jul 14 '20 at 02:47
  • 1
    Are you using Python 2 or Python 3? – metatoaster Jul 14 '20 at 02:49
  • 3
    If you're using Python 2, this is a duplicate of [Why input() gives an error when I just press enter?](https://stackoverflow.com/q/5025920/4518341) though maybe you're running Python 2 instead of 3 by accident. If you're running Python3, you need to provide a [mre]. In any case, welcome to SO! Check out the [tour], and [ask] if you want advice. – wjandrea Jul 14 '20 at 02:56
  • Does this answer your question? [Why input() gives an error when I just press enter?](https://stackoverflow.com/questions/5025920/why-input-gives-an-error-when-i-just-press-enter) – wjandrea Jul 14 '20 at 14:40

1 Answers1

2

I suspect you are using Python 2. In Python 2, input() tries to run eval() on the input string. An email address isn't valid python so you get a syntax error. Try using raw_input() instead.

RootTwo
  • 4,288
  • 1
  • 11
  • 15