如何自动过滤电子邮件附件Python中的最新文件(How to automaticlly filter most recent file in email attachments Python)

编程入门 行业动态 更新时间:2024-10-17 19:22:06
如何自动过滤电子邮件附件Python中的最新文件(How to automaticlly filter most recent file in email attachments Python)

我有一些代码使用MIME模块附加电子邮件中的文件但是每次我发送电子邮件时我都希望它自动发送文件中的前5个最新图片。

import os, re import sys import smtplib from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText SMTP_SERVER = 'smtp.gmail.com' SMTP_PORT = 587 sender = '***@gmail.com' password = "*******" recipient = '***@gmail.com' subject = 'Python emaillib Test' message = 'Images attached.' directory = "images/" def main(): msg = MIMEMultipart() msg['Subject'] = 'Python emaillib Test' msg['To'] = recipient msg['From'] = sender #this is where it searches for the image files = os.listdir(directory) jpgsearch = re.compile(".jpg", re.IGNORECASE) files = filter(jpgsearch.search, files) for filename in files: path = os.path.join(directory, filename) if not os.path.isfile(path): continue img = MIMEImage(open(path, 'rb').read(), _subtype="jpg") img.add_header('Content-Disposition', 'attachment', filename = filename) msg.attach(img) part = MIMEText('text', "plain") part.set_payload(message) msg.attach(part) session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) session.ehlo() session.starttls() session.ehlo session.login(sender, password) session.sendmail(sender, recipient, msg.as_string()) session.quit() if __name__ == '__main__': main()

我是Python的初学者,所以请帮助我们

I have got some code to attach files from an email using MIME module however each time I send the email I want it to automatically send only the first 5 most recent pictures in the file.

import os, re import sys import smtplib from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText SMTP_SERVER = 'smtp.gmail.com' SMTP_PORT = 587 sender = '***@gmail.com' password = "*******" recipient = '***@gmail.com' subject = 'Python emaillib Test' message = 'Images attached.' directory = "images/" def main(): msg = MIMEMultipart() msg['Subject'] = 'Python emaillib Test' msg['To'] = recipient msg['From'] = sender #this is where it searches for the image files = os.listdir(directory) jpgsearch = re.compile(".jpg", re.IGNORECASE) files = filter(jpgsearch.search, files) for filename in files: path = os.path.join(directory, filename) if not os.path.isfile(path): continue img = MIMEImage(open(path, 'rb').read(), _subtype="jpg") img.add_header('Content-Disposition', 'attachment', filename = filename) msg.attach(img) part = MIMEText('text', "plain") part.set_payload(message) msg.attach(part) session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) session.ehlo() session.starttls() session.ehlo session.login(sender, password) session.sendmail(sender, recipient, msg.as_string()) session.quit() if __name__ == '__main__': main()

I am a beginner to Python so help would be appreciated

最满意答案

使用os.stat获取atime,ctime或mtime。 然后简单比较时间戳(或使用基于datetime.datetime.fromtimestamp的其他逻辑)

Use os.stat to fetch atime, ctime or mtime. Then simple compare timestamps (or use some other logic based on datetime.datetime.fromtimestamp)

更多推荐

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

发布评论

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

>www.elefans.com

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