如何在python 3中监听传入的电子邮件?

编程入门 行业动态 更新时间:2024-10-21 16:34:39
本文介绍了如何在python 3中监听传入的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为了拥有一个在python 3中运行并读取特定gmail帐户上的传入电子邮件的应用程序,人们将如何侦听这些电子邮件的接收情况?

With the objective of having an application that runs in python 3 and reads incoming emails on an specific gmail account, how would one listen for the reception of this emails?

它应该做的是等到收件箱中收到一封新邮件,再从电子邮件中读取主题和正文,然后从正文中获取文本(无格式).

What it should do is wait until a new mail is received on the inbox, read the subject and body from the email and get the text from the body (without format).

这是我到目前为止得到的:

This is what I got so far:

import imaplib import email import datetime import time mail = imaplib.IMAP4_SSL('imap.gmail', 993) mail.login(user, password) mail.list() mail.select('inbox') status, data = mail.search(None, 'ALL') for num in data[0].split(): status, data = mail.fetch(num, '(RFC822)') email_msg = data[0][1] email_msg = email.message_from_bytes(email_msg) maintype = email_msg.get_content_maintype() if maintype == 'multipart': for part in email_msg.get_payload(): if part.get_content_maintype() == 'text': print(part.get_payload()) elif maintype == 'text': print(email_msg.get_payload())

但这有两个问题:当邮件是多部分时,每个部分都会被打印,有时在此之后,最后一部分基本上是整个邮件,但采用html格式.

But this has a couple of problems: When the message is multipart each part is printed and sometimes after that the last part is basically the whole message but in html format.

此外,这会打印收件箱中的所有消息,一个人会如何使用imaplib侦听新电子邮件?或与其他库一起使用.

Also, this prints all the messages from the inbox, how would one listen for new emails with imaplib? or with other library.

推荐答案

您是否已检查来自这里由git用户nickoala发布?它是一个python 2脚本,在Python3中,您需要首先对带有电子邮件内容的字节进行解码.

Have you check below script (3_emailcheck.py) from here posted by git user nickoala? Its a python 2 script and in Python3 you need to decode the bytes with the email content first.

import time from itertools import chain import email import imaplib imap_ssl_host = 'imap.gmail' # imap.mail.yahoo imap_ssl_port = 993 username = 'USERNAME or EMAIL ADDRESS' password = 'PASSWORD' # Restrict mail search. Be very specific. # Machine should be very selective to receive messages. criteria = { 'FROM': 'PRIVILEGED EMAIL ADDRESS', 'SUBJECT': 'SPECIAL SUBJECT LINE', 'BODY': 'SECRET SIGNATURE', } uid_max = 0 def search_string(uid_max, criteria): c = list(map(lambda t: (t[0], '"'+str(t[1])+'"'), criteria.items())) + [('UID', '%d:*' % (uid_max+1))] return '(%s)' % ' '.join(chain(*c)) # Produce search string in IMAP format: # e.g. (FROM "me@gmail" SUBJECT "abcde" BODY "123456789" UID 9999:*) def get_first_text_block(msg): type = msg.get_content_maintype() if type == 'multipart': for part in msg.get_payload(): if part.get_content_maintype() == 'text': return part.get_payload() elif type == 'text': return msg.get_payload() server = imaplib.IMAP4_SSL(imap_ssl_host, imap_ssl_port) server.login(username, password) server.select('INBOX') result, data = server.uid('search', None, search_string(uid_max, criteria)) uids = [int(s) for s in data[0].split()] if uids: uid_max = max(uids) # Initialize `uid_max`. Any UID less than or equal to `uid_max` will be ignored subsequently. server.logout() # Keep checking messages ... # I don't like using IDLE because Yahoo does not support it. while 1: # Have to login/logout each time because that's the only way to get fresh results. server = imaplib.IMAP4_SSL(imap_ssl_host, imap_ssl_port) server.login(username, password) server.select('INBOX') result, data = server.uid('search', None, search_string(uid_max, criteria)) uids = [int(s) for s in data[0].split()] for uid in uids: # Have to check again because Gmail sometimes does not obey UID criterion. if uid > uid_max: result, data = server.uid('fetch', uid, '(RFC822)') # fetch entire message msg = email.message_from_string(data[0][1]) uid_max = uid text = get_first_text_block(msg) print 'New message :::::::::::::::::::::' print text server.logout() time.sleep(1)

更多推荐

如何在python 3中监听传入的电子邮件?

本文发布于:2023-10-12 23:35:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1486205.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:电子邮件   如何在   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!