因此,对于练习,我尝试编写一个简单的程序来读取/写入来自终端/ python shell的电子邮件。我的问题是,每当我尝试输入无效的响应时,它都会结束程序,而不是返回主菜单,'可以这么说。当我尝试选择退出程序并要求用户确认他们想要退出时,同样的问题。这是我的代码:
import smtplib
import imaplib
import email
print 'Welcome to NhuMail'
print '\n \nPlease enter your login information: \n'
user = raw_input('Email User:')
pw = raw_input("Password:")
def check_mail(user, pw):
# Login to INBOX
imap = imaplib.IMAP4_SSL("imap.gmail.com", 993)
imap.login(user, pw)
imap.select('INBOX')
# Use search(), not status()
status, response = imap.search('INBOX', '(UNSEEN)')
unread_msg_nums = response[0].split()
# Print the count of all unread messages
print '\n\nYou have %s unread messages.' % (len(unread_msg_nums))
def read_mail():
imap = imaplib.IMAP4_SSL("imap.gmail.com", 993)
imap.login(user, pw)
imap.list()
imap.select("inbox")
status, response = imap.search('INBOX', '(UNSEEN)')
unread_msg_nums = response[0].split()
result, data = imap.uid('search', None, "ALL") # search and return uids instead
latest_email_uid = data[0].split()[-1]
result, data = imap.uid('fetch', latest_email_uid, '(RFC822)')
raw_email = data[0][1]
email_message = email.message_from_string(raw_email)
print email_message['To']
print email.utils.parseaddr(email_message['From']) # for parsing "Yuji Tomita" <yuji@grovemade.com>
email_items = email_message.items()
# print all headers
print email_items[1]
print email_items[2]
print email_items[5]
print '\n'
b = email.message_from_string(raw_email)
if b.is_multipart():
for payload in b.get_payload():
# if payload.is_multipart(): ...
print payload.get_payload()
else:
print b.get_payload()
def new_mail():
from_add = user
to_add = raw_input('Enter TO address:')
msg = raw_input('Enter desired message:')
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(user,pw)
server.sendmail(from_add,to_add,msg)
def exit_mail():
confirm_exit = raw_input('Are you sure you want to exit? (Yes/No):')
server = smtplib.SMTP('smtp.gmail.com:587')
if confirm_exit == 'Yes':
server.quit()
print 'Nice Nhu-ing you! Come again soon :)'
elif confirm_exit == 'No':
return prompt
def mail_options():
prompt = raw_input('Say \'Nhu\' to compose a message \nOR \nSay \'Read\' to read unread message \nOR \nSay \'Escape\' to exit NhuMail: \n')
while True:
if prompt == 'Nhu':
new_mail()
return prompt
elif prompt == 'Read':
read_mail()
return prompt
elif prompt == 'Escape':
exit_mail()
break
else:
print 'You didn\'t enter one of the options!'
return prompt
check_mail(user,pw)
mail_options()
答案 0 :(得分:0)
您的计划中的问题在于mail_options()
。您有一个无限循环,只能运行一次,然后由于使用return
语句而每次都退出。进行无限循环以持续显示mail_options()
,它应该可以解决您的问题。你只调用一次,这就是它退出的原因。
你可以这样做:
while True:
mail_options()
另一种选择是询问循环内的提示,并消除return语句的功能。
def mail_options():
while True:
prompt = raw_input('Say \'Nhu\' to compose a message \nOR \nSay \'Read\' to read unread message \nOR \nSay \'Escape\' to exit NhuMail: \n')
if prompt == 'Nhu':
new_mail()
elif prompt == 'Read':
read_mail()
elif prompt == 'Escape':
exit_mail()
return # Get out of the function.
else:
print 'You didn\'t enter one of the options!'
答案 1 :(得分:0)
您似乎想继续致电mail_options()
。你可以用无限循环来做到这一点:
while True:
mail_options()
您也可以将循环放在mail_options()
:
def mail_options():
while True:
prompt = raw_input('Say \'Nhu\' to compose a message \nOR \nSay \'Read\' to read unread message \nOR \nSay \'Escape\' to exit NhuMail: \n')
while True:
if prompt == 'Nhu':
new_mail()
elif prompt == 'Read':
read_mail()
elif prompt == 'Escape':
exit_mail()
break
else:
print 'You didn\'t enter one of the options!'
答案 2 :(得分:0)
尝试将mail_options()更改为:
def mail_options():
while True:
prompt = raw_input('Say \'Nhu\' to compose a message \nOR \nSay \'Read\' to read unread message \nOR \nSay \'Escape\' to exit NhuMail: \n')
if prompt == 'Nhu':
new_mail()
elif prompt == 'Read':
read_mail()
elif prompt == 'Escape':
exit_mail()
break
else:
print 'You didn\'t enter one of the options!'
您提示一次,然后在获得结果后立即返回他们在运行指定函数后所说的内容,然后因为您使用return
关键字,它会突破循环和函数并退出该计划。