200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > python 邮件发送附件 本目录下所有文件_为python中的每个txt文件发送附件电子邮件...

python 邮件发送附件 本目录下所有文件_为python中的每个txt文件发送附件电子邮件...

时间:2022-10-02 13:29:31

相关推荐

python 邮件发送附件 本目录下所有文件_为python中的每个txt文件发送附件电子邮件...

我有多个文件的一部分预定义的路径,我正在尝试为每一个txt文件生成电子邮件。

下面的代码只工作一次,但是每封电子邮件的每个文件都会递增。在

你的意见/建议会很有帮助的。

谢谢,

艾尔#!/usr/bin/python

import sys, os, shutil, time, fnmatch

import distutils.dir_util

import distutils.util

import glob

from os.path import join, getsize

from email.mime.multipart import MIMEMultipart

from email.mime.application import MIMEApplication

# Import smtplib for the actual sending function

import smtplib

import base64

# For guessing MIME type

import mimetypes

# Import the email modules we'll need

import email

import email.mime.application

sourceFolder = "/root/email_python/"

destinationFolder = r'/root/email_python/sent'

# Create a text/plain message

msg=email.mime.Multipart.MIMEMultipart()

#msg['Subject'] = '

msg['From'] = 'john@'

msg['To'] = 'user1@'

# The main body is just another attachment

# body = email.mime.Text.MIMEText("""Email message body (if any) goes here!""")

# msg.attach(body)

#To check if the directory is empty.

#If directory is empty program exits and no email/file copy operations are carried out

if os.listdir(sourceFolder) ==[]:

print "No attachment today"

else:

for iFiles in glob.glob('*.txt'):

print (iFiles)

print "The current location of the file is " +(iFiles)

part = MIMEApplication(open(iFiles).read())

part.add_header('Content-Disposition',

'attachment; filename="%s"' % os.path.basename(iFiles))

shutil.move(iFiles, destinationFolder)

msg.attach(part)

#shutil.move(iFiles, destinationFolder)

#Mail trigger module

server = smtplib.SMTP('IP:25')

server.sendmail('john@',['user1@'], msg.as_string())

server.quit()

print "Email successfully sent!"

print "Files moved successfully"

print "done"

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。