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@domain.com'

msg['To'] = 'user1@domain.com'

# 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@domain.com',['user1@domain.com'], msg.as_string())

server.quit()

print "Email successfully sent!"

print "Files moved successfully"

print "done"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于 Python 的示例代码,用于从 Excel 文件读取电子邮件地址和内容,并将内容作为附件发送给相应的电子邮件地址: ```python import pandas as pd import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication # 读取 Excel 文件电子邮件地址和内容 df = pd.read_excel("file.xlsx", sheet_name="Sheet1") # SMTP 服务器设置 smtp_server = "smtp.example.com" smtp_port = 587 smtp_username = "your_username" smtp_password = "your_password" # 登录 SMTP 服务器 smtp = smtplib.SMTP(smtp_server, smtp_port) smtp.starttls() smtp.login(smtp_username, smtp_password) # 循环遍历每个电子邮件地址并发送邮件 for index, row in df.iterrows(): to_email = row["Email"] subject = "Your subject here" body = row["Content"] # 构建邮件主体 message = MIMEMultipart() text = MIMEText(body) message.attach(text) # 添加附件 with open("attachment_file", "rb") as f: attach = MIMEApplication(f.read(), _subtype="pdf") attach.add_header("Content-Disposition", "attachment", filename="attachment_filename") message.attach(attach) message["Subject"] = subject message["From"] = smtp_username message["To"] = to_email # 发送邮件 smtp.sendmail(smtp_username, to_email, message.as_string()) # 关闭 SMTP 连接 smtp.quit() ``` 请注意,您需要将代码的以下值替换为适当的值: - `file.xlsx`:包含电子邮件地址和内容的 Excel 文件名。 - `Sheet1`:包含电子邮件地址和内容的工作表名称。 - `smtp_server`:您的 SMTP 服务器的主机名或 IP 地址。 - `smtp_port`:您的 SMTP 服务器的端口号。 - `smtp_username`:您的 SMTP 服务器的用户名。 - `smtp_password`:您的 SMTP 服务器的密码。 - `attachment_file`:要附加到电子邮件文件路径和名称。 - `attachment_filename`:要附加到电子邮件文件名称
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值