python收邮件保存附件_如何从电子邮件获取csv附件并保存我

使用Python的imaplib和poplib库,通过登录邮箱服务器检索带有'检测'主题的邮件,查找并保存其中的CSV附件。首先,通过IMAP4连接到邮箱,搜索指定主题的邮件,然后解析邮件内容,找到附件并保存到指定目录。如果使用POP3,同样可以获取邮件并保存附件。
摘要由CSDN通过智能技术生成

有效的代码:import imaplib

import email

import os

svdir = 'c:/downloads'

mail=imaplib.IMAP4('mailserver')

mail.login("username","password")

mail.select("DetReport")

typ, msgs = mail.search(None, '(SUBJECT "Detection")')

msgs = msgs[0].split()

for emailid in msgs:

resp, data = mail.fetch(emailid, "(RFC822)")

email_body = data[0][1]

m = email.message_from_string(email_body)

if m.get_content_maintype() != 'multipart':

continue

for part in m.walk():

if part.get_content_maintype() == 'multipart':

continue

if part.get('Content-Disposition') is None:

continue

filename=part.get_filename()

if filename is not None:

sv_path = os.path

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值