Tkinter 経由でテキスト メッセージを送信しようとしています。だからあなたは入力しますsms:hello
。というテキスト メッセージが送信されますhello
。これを行うために、AT&T 電子メール サーバーと GMail を使用して単語を電子メールで送信します。したがって、プログラムINFO.txt
は、すべての電子メール認証g_user
g_pass
とm_num
. 次に、それらを使用して、テキスト メッセージを送信する電子メールを送信します。
今私の問題はそれ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()