我正在尝试使用Tkinter制作程序来发送邮件,但我无法在尝试发送邮件之前弹出窗口。我该怎么做?
代码:
import smtplib
from Tkinter import *
root=Tk()
v=StringVar()
entry=Entry(root, textvariable=v)
sender = "email@email.com.au"
receiver = [v]
message = """From: <email@email.com.au>
To: email@email.com
Subject: I made a code to send this!
Hello i made a code to send this!"""
try:
session = smtplib.SMTP('mail.optusnet.com.au',25)
session.ehlo()
session.starttls()
session.ehlo()
session.login(sender,'passwrd')
session.sendmail(sender,receiver,message)
session.quit()
except smtplib.SMTPException as e:
print(e)
答案 0 :(得分:2)
使用窗口系统的程序使用某种事件进行操作。在Windows上,系统会为窗口创建,用户输入等内容生成窗口消息。程序必须不断处理事件并及时响应适当的消息。
在tkinter中,这是通过将tkinter mainloop函数作为代码中的最终操作来完成的。这将启动程序处理事件。在这样的程序中,然后发送邮件消息以响应事件。这可能是用户单击按钮或菜单项或响应计时器或窗口创建事件。
您没有看到图形用户界面的原因是,当您的程序执行时,它会为自己排队一些事件,但它会在处理任何事件之前退出。因此,窗口创建事件,地图事件,绘制事件和所有其他事件永远不会被处理,退出系统的过程将丢弃它们。
答案 1 :(得分:0)
import smtplib
from smtplib import SMTPException
#this app sends email via gmail
def gmail( ):
usermail = user_email.get()
receivermail=receiver_email.get()
server=smtplib.SMTP('smtp.gmail.com:587')
pass_word=password.get()
subject=subj.get()
#This allow you to include a subject by adding from, to and subject
line
main_message=body.get('1.0', 'end-1c')
Body="""From: Name here <usermail>
To: <receivermail>
Subject:%s
%s
""" %(subject,main_message )
try:
server=smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(usermail, pass_word )
server.sendmail(usermail,receivermail, Body )
text.insert(1.0, 'message sent')
#error handling
except (smtplib.SMTPException,ConnectionRefusedError,OSError):
text.insert(1.0, 'message not sent')
finally:
server.quit()
#Gui interface
from tkinter import*
root= Tk(className=" Gmail app " )
root.config(bg="brown", )
#user mail
user_email = Label(root, text="Enter your Gmail address: ")
user_email.pack()
user_email.config(bg="black", fg="white")
user_email = Entry(root, bd =8)
user_email.pack(fill=X)
#receiver email
receiver_email = Label(root, text="Enter the recipient's email address:
")
receiver_email.pack( )
receiver_email.config(bg="black", fg="white")
receiver_email = Entry(root, bd =8)
receiver_email.pack(fill=X)
#subject line
subj= Label(root, text="Enter your subject here: ")
subj.pack( )
subj.config(bg="black", fg="white")
subj = Entry(root, bd =8)
subj.pack(fill=X)
#Body of the message
body = Text(root, font="Tahoma", relief=SUNKEN , bd=8)
body.config(bg="pink", height=15)
body.pack(fill=BOTH, expand=True)
#password widget
password = Label(root, text="Enter your Gmail password: ")
password.pack()
password.config(bg="black", fg="white")
password= Entry(root, show='*', bd =8)
password.pack(fill=X)
#submit button
submit_mail = Button(root, bd =8, text="Click here to submit the mail",
command=gmail)
submit_mail.pack(fill=X)
#feed back
text = Text(root, font="Tahoma", relief=SUNKEN , bd=8)
text.config(bg="pink", height=2)
text.pack(fill=BOTH, expand=True)
root.mainloop()
我上面的代码的问题是它不能发送主题,只有
身体可以发送。我也是两个月自学的业余爱好者。我会
建议你从这个论坛,yputube和一本书中学习tkinter
被David Beazely称为“tkinter的介绍”。希望这有帮助
答案 2 :(得分:0)
您可以通过替换
来缩短发送电子邮件的行session = smtplib.SMTP('mail.optusnet.com.au',25)
session.ehlo()
session.starttls()
session.ehlo()
session.login(sender,'passwrd')
session.sendmail(sender,receiver,message)
session.quit()
与
session = SMTP_SSL('mail.optusnet.com.au',465) ## Makes a connection with SSL, no starttls() required
session.login(sender, 'passwrd')
session.sendmail(sender, receiver ,message)
session.quit()
端口465用于通过TLS / SSL进行身份验证的SMTP。
您不必编写函数ehlo()
和starttls()
,因为它们是自动调用的。只有在您使用starttls()
时才会调用SMTP_SLL()
。
你实际上甚至可以忽略函数quit()
,因为它在一段时间后被调用。