python - 使用 smtplib 从 Gmail 接收回复 - Python

标签 python gmail smtplib reply

好的,我正在开发一种系统,这样我就可以通过短信在我的电脑上开始操作。我可以让它发送初始消息:

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()

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

最佳答案

除了用于发送电子邮件的 SMTP,您应该使用 POP3 或 IMAP(后者更可取)。 使用 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

无耻地盗自here

关于python - 使用 smtplib 从 Gmail 接收回复 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18156485/

相关文章:

python - 使用 Python 2.3 通过 SSL 发送邮件

python - 我的 Facebook 应用程序应该使用哪种授权

python - 如何根据数值对嵌套列表进行排序?

oauth-2.0 - Google Oauth token 限制

python - Django 1.8 使用 gmail SMTP 发送邮件

javascript - Gmail API : unable to request unread message

python - 尝试发送电子邮件 Python 时出错

python - 使用 Tweepy 给自己发送电子邮件提醒。但是电子邮件内容正在累积每条推文。我想让它覆盖

python - 使用 Python 计算图中的波浪数

python - 在 Python 2.7 中使用多个占位符格式化程序打印字符串