通过Python脚本删除最近收到的电子邮件?
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最近使用 poplib 编写了一个非常原始的电子邮件客户端。我用我的电子邮件服务器(不是 gmail)在一些测试电子邮件上对此进行了测试,它似乎工作正常。我会先给自己发送几封虚拟电子邮件进行测试。
注意事项:
模式':
http://mail.google.com/support/bin/answer。 py?answer=47948
已启用:Gmail >设置>
转发和 POP/IMAP > “启用 POP
对于所有邮件”
希望这会有所帮助,这应该足以让您继续前进!
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:
mode':
http://mail.google.com/support/bin/answer.py?answer=47948
enabled: Gmail > Settings >
Forwarding and POP/IMAP > "Enable POP
for all mail"
Hope this helps, it should be enough to get you going!