使用smtplib - Python接收来自Gmail的回复

Rog*_*III 8 python gmail smtplib reply

好的,我正在研究一种类型的系统,以便我可以使用sms消息在我的计算机上开始操作.我可以让它发送初始消息:

import smtplib  

fromAdd = 'GmailFrom'  
toAdd  = 'SMSTo'  
msg = 'Options \nH - Help \nT - Terminal'  

username = 'GMail'  
password = 'Pass'  

server = smtplib.SMTP('smtp.gmail.com:587')  
server.starttls()  
server.login(username , password)  
server.sendmail(fromAdd , toAdd , msg)  
server.quit()
Run Code Online (Sandbox Code Playgroud)

我只需要知道如何等待回复或从Gmail本身提取回复,然后将其存储在变量中以供以后的功能使用.

Uku*_*kit 14

您应该使用POP3或IMAP(后者更可取),而不是用于发送电子邮件的SMTP.使用SMTP的示例(代码不是我的,请参阅下面的URL以获取更多信息):

import imaplib
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('myusername@gmail.com', 'mypassword')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.

result, data = mail.search(None, "ALL")

ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_id = id_list[-1] # get the latest

result, data = mail.fetch(latest_email_id, "(RFC822)") # fetch the email body (RFC822) for the given ID

raw_email = data[0][1] # here's the body, which is raw text of the whole email
# including headers and alternate payloads
Run Code Online (Sandbox Code Playgroud)

这里无耻地偷走