使用 python 计算电子邮件帐户中的邮件数量

发布于 2024-09-05 13:24:54 字数 90 浏览 5 评论 0原文

有没有什么方法,在Python中,可以访问电子邮件帐户(我需要这个用于gmail,但如果有的话更好)并且能够查看收件箱中的邮件数量(甚至可能只是未读邮件) ? 谢谢。

is there any way, in Python, to have access to an e-mail account (I'll need this for gmail but better if any works) and be able to see the number of messages in the inbox (maybe even unread messages only)?
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半暖夏伤 2024-09-12 13:24:54

尝试

import imaplib  
obj = imaplib.IMAP4_SSL('imap.gmail.com', 993)  
obj.login('username', 'password')  
obj.select('Inbox')      <-- it will return total number of mail in Inbox i.e 
('OK', ['50'])  
obj.search(None,'UnSeen')  <-- it will return the list of uids for Unseen mails  

Try

import imaplib  
obj = imaplib.IMAP4_SSL('imap.gmail.com', 993)  
obj.login('username', 'password')  
obj.select('Inbox')      <-- it will return total number of mail in Inbox i.e 
('OK', ['50'])  
obj.search(None,'UnSeen')  <-- it will return the list of uids for Unseen mails  
往事随风而去 2024-09-12 13:24:54

看一下python标准库的 POP3IMAP 包。

Take a look at the python standard library's POP3 and IMAP packages.

饭团 2024-09-12 13:24:54

基于 Avadhesh 的回答:

#! /usr/bin/env python3.4

import getpass
import imaplib

mail = imaplib.IMAP4_SSL('imap.server.com')
mypassword = getpass.getpass("Password: ")
address = '[email protected]'
mail.login(address, mypassword)
mail.select("inbox")
print("Checking for new e-mails for ",address,".", sep='')
typ, messageIDs = mail.search(None, "UNSEEN")
messageIDsString = str( messageIDs[0], encoding='utf8' )
listOfSplitStrings = messageIDsString.split(" ")
if len(listOfSplitStrings) == 0:
    print("You have no new e-mails.")
elif len(listOfSplitStrings) == 1:
    print("You have",len(listOfSplitStrings),"new e-mail.")
else:
    print("You have",len(listOfSplitStrings),"new e-mails.")

Building on Avadhesh's answer:

#! /usr/bin/env python3.4

import getpass
import imaplib

mail = imaplib.IMAP4_SSL('imap.server.com')
mypassword = getpass.getpass("Password: ")
address = '[email protected]'
mail.login(address, mypassword)
mail.select("inbox")
print("Checking for new e-mails for ",address,".", sep='')
typ, messageIDs = mail.search(None, "UNSEEN")
messageIDsString = str( messageIDs[0], encoding='utf8' )
listOfSplitStrings = messageIDsString.split(" ")
if len(listOfSplitStrings) == 0:
    print("You have no new e-mails.")
elif len(listOfSplitStrings) == 1:
    print("You have",len(listOfSplitStrings),"new e-mail.")
else:
    print("You have",len(listOfSplitStrings),"new e-mails.")
我纯我任性 2024-09-12 13:24:54

用于查找未读邮件的替代 Gmail 特定解决方案:

Gmail 优惠消息的原子提要。例如:

https://mail.google.com/mail/feed/atom/(收件箱中未读邮件)
http://mail.google.com/mail/feed/atom/labelname/< /a> (标签名称中的未读消息)
http://mail.google.com/mail/feed/atom/unread/< /a> (所有未读消息)

因此您可以使用优秀的 feedparser 库来获取提要并计算条目数。

不过,现在我正在查看它,似乎未读消息源仅返回最多 20 个条目,因此这可能有点有限。

An alternate gmail specific solution for finding unread messages:

Gmail offers atom feeds for messages. For example:

https://mail.google.com/mail/feed/atom/ (unread messages in inbox)
http://mail.google.com/mail/feed/atom/labelname/ (unread messages in labelname)
http://mail.google.com/mail/feed/atom/unread/ (all unread messages)

So you could use the excellent feedparser library to grab the feed and count the entries.

Now that I'm looking at it, though, it appears that the unread messages feed only returns up to 20 entries, so this might be a bit limited.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文