python - Tkinter StringVar Send mail error SMTPlib -
i making tkinter app send short email.
the code in full:
from tkinter import * smtplib import * time import sleep root=tk() root.wm_title("gmail short message sender") w = 500 # width tk root h = 500 # height tk root # screen width , height ws = root.winfo_screenwidth() # width of screen hs = root.winfo_screenheight() # height of screen # calculate x , y coordinates tk root window x = (ws/2) - (w/2) y = (hs/2) - (h/2) # set dimensions of screen # , placed root.geometry('%dx%d+%d+%d' % (w, h, x, y)) def send(): e1_var.get() e2_var.get() e3_var.get() try: smtpobj = smtp('smtp.gmail.com', "587") smtpobj.ehlo() smtpobj.starttls() smtpobj.ehlo() smtpobj.login("123@gmail.com", "password") smtpobj.sendmail(sender1, receivers1, msg1) l1=label(text="sent").grid() sleep(1) l1.destroy() except smtpexception: l2=label(text="error").grid() raise e1_var=stringvar() e2_var=stringvar() e3_var=stringvar() sender = e1_var receivers = [e2_var] msg = e3_var sender1 = '%s' % (sender) receivers1 = '[%s]'% (receivers) msg1 = '%s' % (msg) e1=entry(root, textvariable=e1_var).grid() e2=entry(root, textvariable=e2_var).grid() e3=entry(root, textvariable=e3_var).grid() b1=button(text="send", command=send).grid() root.mainloop()
the problem:
the problem stringvar
instances. first stringvar
instances drawing error.
exception in tkinter callback traceback (most recent call last): file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/lib-tk/tkinter.py", line 1536, in __call__ return self.func(*args) file "desktop/python scripts/email.py", line 32, in send smtpobj.sendmail(sender1, receivers1, msg1) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/smtplib.py", line 724, in sendmail esmtp_opts.append("size=%d" % len(msg)) attributeerror: stringvar instance has no attribute '__len__'
but fixed adding section in,
sender1 = sender receivers1 = receivers msg1 = msg
and changing smtpobj.sendmail(sender, receivers, msg)
smtpobj.sendmail(sender1, receivers1, msg1)
. getting error.
exception in tkinter callback traceback (most recent call last): file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/lib-tk/tkinter.py", line 1536, in __call__ return self.func(*args) file "desktop/python scripts/email.py", line 32, in send smtpobj.sendmail(sender1, receivers1, msg1) file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/smtplib.py", line 742, in sendmail raise smtprecipientsrefused(senderrs) smtprecipientsrefused: {'[[<tkinter.stringvar instance @ 0x103e8be60>]]': (555, '5.5.2 syntax error. yi8sm12824416pab.22 - gsmtp')}
is syntax error because of quote marks? if so, how fix first error?
this (and other code it) incorrect:
sender = e1_var receivers = [e2_var] msg = e3_var
you need change this:
sender = e1_var.get() receivers = [e2_var.get()] msg = e3_var.get()
that's not problem, though. code running before user has chance enter anything, results empty string. need wait call .get()
methods until you're ready use them (which, curiously, you're doing inside of send
).
Comments
Post a Comment