我想通過Tkinter發送文本消息。所以你輸入sms:hello
。這會發送一條文字信息,表示hello
。要做到這一點,它使用AT電子郵件服務器和GMail電子郵件發送該單詞。所以程序讀取INFO.txt
其中包含所有的電子郵件認證g_user
g_pass
和m_num
。然後它使用那些發送發送文本消息的電子郵件。我的問題是UnboundLocalError: local variable 'g_user' referenced before assignment
。我知道這是由不是global
變量的東西造成的。誰能幫我嗎?我難倒...全局和本地Python
root = Tk()
#open file
file=open('INFO.txt')
line=file.readline()
if 'Mobile_number:::' in line:
m_num=line[16:]
if 'GMail_name:::' in line:
g_user=line[13:]
if 'GMail_pass:::' in line:
g_pass=line[13:]
def callback(event):
text = inputfield.get()
if 'sms:' in text:
textmessage()
def textmessage():#sms:
import smtplib
#open file
file=open('INFO.txt')
line=file.readline()
if 'Mobile_number:::' in line:
m_num=line[16:]
if 'GMail_name:::' in line:
g_user=line[13:]
if 'GMail_pass:::' in line:
g_pass=line[13:]
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
sender = '{}@gmail.com'.format(g_user)
password='{}'.format(g_pass)
recipient = '{}@txt.att.net'.format(m_num)
subject = 'Gmail SMTP Test'
body = text[4:]
"Sends an e-mail to the specified recipient."
body = "" + body + ""
headers = ["From: " + sender,
"Subject: " + subject,
"To: " + recipient,
"MIME-Version: 1.0",
"Content-Type: text/html"]
headers = "\r\n".join(headers)
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.ehlo()
session.starttls()
session.ehlo
session.login(sender, password)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
session.quit()
text2=text[4:]
confirmation="SMS containing '{}' sent".format(text2)
tex.insert(END,confirmation)
tex=Text(root)
tex.pack(side='right')
inputfield = Entry(root)
inputfield.pack(side='bottom')
inputfield.bind('<Return>', callback)
root.mainloop()