我在转发带有附件的邮件时遇到问题。有人能指出我正确的方向吗?我猜它在 Fetch 语句中,但我不确定。
import sys
import uuid
import re
import smtplib
import email
address = ''
username = ''
password = ''
def checkMail():
M = imaplib.IMAP4_SSL(address)
M.login(username, password)
M.select("INBOX", readonly=True)
typ, data = M.search(None, 'Unseen')
messageCount = len(data[0].split())
print('messageCount: %', messageCount)
if messageCount > 0:
for num in data[0].split():
typ, data = M.fetch(num, '(BODY[TEXT])')
foundAt = data[0][1].find('Content-Type: application')
if(foundAt > 0):
print('attachmentfound')
sendMail(data[0][1])
M.close()
M.logout()
def sendMail(raw_message):
toAddress = ''
fromAddress = ''
LOGIN = ''
PASSWORD = ''
server = smtplib.SMTP('', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(fromAddress, toAddress, raw_message)
server.quit()
def main():
checkMail()
main()
如果您想使用 imaplib,为什么不直接获取整个消息(RFC822)?看看这个类似的问题:
如何在 python 中使用 imaplib 获取电子邮件正文?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)