-1

我正在尝试编写一个 Python 脚本,它将: 1. 在一天中的预定时间运行。2. 将收集特定目录(例如 C:\myFiles)中的任何文件(.mobi 格式)并将它们作为附件通过电子邮件发送到特定电子邮件地址(电子邮件 ID 保持不变)。3. C:\myFiles 目录中的文件会随着时间的推移不断变化(因为我有另一个脚本对这些文件执行一些归档操作并将它们移动到不同的文件夹)。但是,新文件将不断出现。我在开始时有一个 if 条件检查以确定文件是否存在(只有这样它才会发送电子邮件)。

我无法检测到任何 mobi 文件(使用 *.mobi 不起作用)。如果我明确添加文件名,那么我的代码就可以工作,否则就不行。

如何让代码在运行时自动检测 .mobi 文件?

这是我到目前为止所拥有的:

import os

# Import smtplib for the actual sending function
import smtplib
import base64

# For MIME type
import mimetypes

# Import the email modules 
import email
import email.mime.application


#To check for the existence of .mobi files. If file exists, send as email, else not
for file in os.listdir("C:/Users/srayan/OneDrive/bookManager/EmailSenderModule"):
    if file.endswith(".mobi"):
           # Create a text/plain message
            msg = email.mime.Multipart.MIMEMultipart()
            #msg['Subject'] = 'Greetings'
            msg['From'] = 'sender@gmail.com'
            msg['To'] = 'receiver@gmail.com'

            # The main body is just another attachment
            # body = email.mime.Text.MIMEText("""Email message body (if any) goes here!""")
            # msg.attach(body)

            # File attachment
            filename='*.mobi'   #Certainly this is not the right way to do it?
            fp=open(filename,'rb')
            att = email.mime.application.MIMEApplication(fp.read(),_subtype="mobi")
            fp.close()
            att.add_header('Content-Disposition','attachment',filename=filename)
            msg.attach(att)


            server = smtplib.SMTP('smtp.gmail.com:587')
            server.starttls()
            server.login('sender@gmail.com','gmailPassword')
            server.sendmail('sender@gmail.com',['receiver@gmail.com'], msg.as_string())
            server.quit()
4

3 回答 3

1

只是想抛出使用yagmail发送带有附件的电子邮件是多么容易(完全公开:我是开发人员)。

import yagmail
yag = yagmail.SMTP('sender@gmail.com', your_password)
yag.send('receiver@gmail.com', 'Greetings', contents = '/local/path/to/file.mobi')

你可以用内容做各种各样的事情:如果你有一个东西列表,它会很好地组合它。例如,一个文件名列表将使其全部附加。将它与一些消息混合,它就会有一个消息。

将附加任何作为有效文件的字符串,其他字符串只是文本。

一次添加所有 mobi 文件:

my_path = "C:/Users/srayan/OneDrive/bookManager/EmailSenderModule"
fpaths = [file for file in os.listdir(my_path) if file.endswith(".mobi")]
yag.send('receiver@gmail.com', contents = fpaths)

或者

yag.send('receiver@gmail.com', contents = ['Lots of files attached...'] + fpaths)

我鼓励您阅读github 文档以查看其他不错的功能,例如,您不必使用密钥环在脚本中包含您的密码/用户名(额外安全)。设置一次,你会很高兴....

哦,是的,而不是你这里的 41 行代码,可以使用 yagmail 用 5 行来完成;)

于 2015-07-28T06:31:54.120 回答
0

这就是我最终解决它的方法。PascalvKooten 有一个有趣的解决方案,这将使我的工作变得更加轻松,但是由于我正在学习 Python,所以我想从头开始构建它。谢谢大家的回答:) 你可以在这里找到我的解决方案

于 2015-08-05T21:21:23.430 回答
0

如下使用glob过滤文件夹中的文件

文件名 = glob.glob("C:\Temp\*.txt")

遍历文件名并使用以下命令发送它们:

  for file in filesNames:
  part = MIMEBase('application', "octet-stream")
  part.set_payload( open(file,"rb").read() )
  Encoders.encode_base64(part)
  part.add_header('Content-Disposition', 'attachment; filename="%s"'
               % os.path.basename(file))
   msg.attach(part)

  s.sendmail(fro, to, msg.as_string() )
  s.close()

要安排电子邮件,请参阅使用 python 的 cron 作业

于 2015-07-28T02:09:51.580 回答