通过Python脚本删除最近收到的电子邮件?

发布于 2024-08-21 10:17:42 字数 666 浏览 7 评论 0原文

我使用 Gmail 和一个应用程序,该应用程序会在我收到新电子邮件时通知我,并在工具提示中包含其标题。 (GmailNotifier 与 Miranda-IM)我收到的大多数电子邮件都是我不想阅读的电子邮件,并且必须在连接速度较慢的情况下登录 Gmail 才能删除所述电子邮件,这很烦人。我相信插件是闭源的。

我一直在尝试编写一个脚本来登录并删除“热门”电子邮件(最近收到的一封),但没有成功。然而这并不像我想象的那么容易。

我首先尝试使用 imaplib,但发现它不包含任何我希望包含的方法。它有点像 dbapi 规范,仅包含最少的功能,以防 imap 规范发生更改。然后我尝试阅读 imap RFC (rfc3501)。写到一半时,我意识到我不想编写整个邮件客户端,因此决定尝试使用 pop3。

poplib 也很小,但似乎有我需要的东西。然而,pop3 似乎没有按照我熟悉的任何顺序对消息进行排序。如果我想查看收到的日期,我必须在每封电子邮件上调用 top() 或 retr() 来读取标题。

我可能可以遍历每个消息标题,搜索最近的日期,但这很丑陋。如果可能的话,我想避免解析我的整个邮箱。我也不想“弹出”邮箱并下载任何其他邮件。

现在已经过去了 6 个小时,但我感觉并没有比开始时更接近解决方案。我是否忽略了一些简单的事情?我可以尝试另一个图书馆吗? (我找到了一个“chilkat”,但它太臃肿了,我希望用标准库来做到这一点)

I use Gmail and an application that notifies me if I've received a new email, containing its title in a tooltip. (GmailNotifier with Miranda-IM) Most of the emails I receive are ones I don't want to read, and it's annoying having to login to Gmail on a slow connection just to delete said email. I believe plugin is closed source.

I've been (unsuccessfully) trying to write a script that will login and delete the 'top' email (the one most recently received). However this is not as easy I thought it would be.

I first tried using imaplib, but discovered that it doesn't contain any of the methods I hoped it would. It's a bit like the dbapi spec, containing only minimal functionality incase the imap spec is changed. I then tried reading the imap RFC (rfc3501). Halfway through it, I realized I didn't want to write an entire mail client, so decided to try using pop3 instead.

poplib is also minimal but seemingly has what I need. However pop3 doesn't appear to sort the messages in any order I'm familiar with. I have to either call top() or retr() on every single email to read the headers if I want to see the date received.

I could probably iterate through every single message header, searching for the most recent date, but that's ugly. I want to avoid parsing my entire mailbox if possible. I also don't want to 'pop' the mailbox and download any other messages.

It's been 6 hours now and I feel no closer to a solution than when I started. Am I overlooking something simple? Is there another library I could try? (I found a 'chilkat' one, but it's bloated to hell, and I was hoping to do this with the standard library)

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

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

发布评论

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

评论(1

浊酒尽余欢 2024-08-28 10:17:42
import poplib

#connect to server
mailserver = poplib.POP3_SSL('pop.gmail.com')
mailserver.user('recent:YOURUSERNAME') #use 'recent mode'
mailserver.pass_('YOURPASSWORD') #consider not storing in plaintext!

#newest email has the highest message number
numMessages = len(mailserver.list()[1])

#confirm this is the right one, can comment these out later
newestEmail = mailserver.retr(numMessages)
print newestEmail

#most servers will not delete until you quit
mailserver.dele(numMessages)
mailserver.quit()

我最近使用 poplib 编写了一个非常原始的电子邮件客户端。我用我的电子邮件服务器(不是 gmail)在一些测试电子邮件上对此进行了测试,它似乎工作正常。我会先给自己发送几封虚拟电子邮件进行测试。

注意事项:

希望这会有所帮助,这应该足以让您继续前进!

import poplib

#connect to server
mailserver = poplib.POP3_SSL('pop.gmail.com')
mailserver.user('recent:YOURUSERNAME') #use 'recent mode'
mailserver.pass_('YOURPASSWORD') #consider not storing in plaintext!

#newest email has the highest message number
numMessages = len(mailserver.list()[1])

#confirm this is the right one, can comment these out later
newestEmail = mailserver.retr(numMessages)
print newestEmail

#most servers will not delete until you quit
mailserver.dele(numMessages)
mailserver.quit()

I worked with the poplib recently, writing a very primitive email client. I tested this with my email server (not gmail) on some test emails and it seemed to work correctly. I would send yourself a few dummy emails to test it out first.

Caveats:

Hope this helps, it should be enough to get you going!

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