Tkinter를 통해 문자 메시지를 보내려고합니다. 그래서 sms:hello
을 입력하십시오. 그러면 hello
이라는 문자 메시지가 전송됩니다. 이렇게하려면 AT & T 전자 메일 서버와 GMail을 사용하여 단어를 전자 메일로 보냅니다. 그래서이 프로그램은 모든 전자 메일 인증 g_user
g_pass
및 m_num
을 포함하는 INFO.txt
을 읽습니다. 그런 다음 텍스트 메시지를 보내는 전자 메일을 보낼 사람을 사용합니다.전역 및 로컬 파이썬
이제 내 문제는 UnboundLocalError: local variable 'g_user' referenced before assignment
입니다. 내가 아는 것은 global
변수가 아닌 것으로 인해 발생합니다. 누구든지 나를 도울 수 있습니까? 나는
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()