Reputation: 615
I have this program that uses tkinter as my GUI. My program clicks on a button to send an email to. Once they click on that button, an entry/button will also appear so that the user can enter their email to send a message. Once that button is clicked, that is when the message will be sent to that email.
However, I get this error right when I clicked on the first button (Send An Email):
{'': (555, b'5.5.2 Syntax error. i72sm3973288itc.11 - gsmtp')}
The error happens before I even get to enter the email address on the entry box. I am trying to successfully enter an email address and send a message to that email, but failing so far.
This is my code:
from tkinter import *
import smtplib
root = Tk()
def create_button():
email_btn = Button(root, text="SEND AN EMAIL", fg='blue',
command=lambda: get_email())
email_btn.pack()
def get_email():
entry_email = StringVar()
entry_email.get()
email = Entry(root, textvariable=entry_email)
email.pack()
send_btn = Button(root, text="SEND", command=send_email_info(entry_email))
send_btn.pack()
def send_email_info(entry_email):
try:
prompt_msg = "THIS IS A MESSAGE FOR THE EMAIL"
user = '*****@gmail.com'
password = '******'
sender = entry_email.get()
subject = "EMAIL TEST "
message = "Subject: {} \n\n{}".format(subject, prompt_msg)
send_to = ("{}".format(sender))
mail = smtplib.SMTP_SSL('smtp.gmail.com', 465)
mail.ehlo()
mail.login(user, password)
mail.sendmail(user, send_to, message)
mail.close()
print("Success Email!")
email_cmd = Label(root, text="Email Sent!")
email_cmd.pack()
except Exception as x:
print("FAILED")
print(x)
def main():
create_button()
root.mainloop()
main()
Upvotes: 1
Views: 2409
Reputation: 5774
First, you don't need entry_email.get()
this just gets the string from the widget, and you are not capturing the return value anyways. You do need entry_email.get()
instead of entry_email
as the argument to your function for send_btn
. The reason it is evaluating prematurely is because you are not using a lambda
function like you do in the first part of your code (like you should every time you include an argument with your callback to your function). I think you are looking for code like:
from tkinter import *
import smtplib
root = Tk()
def create_button():
email_btn = Button(root, text="SEND AN EMAIL", fg='blue',
command=lambda: get_email())
email_btn.pack()
def get_email():
entry_email = StringVar()
# entry_email.get() # you don't need this, it does nothing
email = Entry(root, textvariable=entry_email)
email.pack()
# function below needs a lambda
send_btn = Button(root, text="SEND", command=lambda: send_email_info(entry_email.get()))
send_btn.pack()
def send_email_info(entry_email):
try:
prompt_msg = "THIS IS A MESSAGE FOR THE EMAIL"
user = '*****@gmail.com'
password = '******'
sender = entry_email
subject = "EMAIL TEST "
message = "Subject: {} \n\n{}".format(subject, prompt_msg)
send_to = ("{}".format(sender))
mail = smtplib.SMTP_SSL('smtp.gmail.com', 465)
mail.ehlo()
mail.login(user, password)
mail.sendmail(user, send_to, message)
mail.close()
print("Success Email!")
email_cmd = Label(root, text="Email Sent!")
email_cmd.pack()
except Exception as x:
print("FAILED")
print(x)
def main():
create_button()
root.mainloop()
main()
Upvotes: 3