python群发邮件并将excel附件添加到正文

本文几个目的:
1。使用smtp库群发邮件
2。添加邮件的附件
3。将Excel附件添加到正文中

"""
to_addr表示群发集,使用形如('abc.163.com,add.163.com,3243@qq.com')
html表示要展示在正文中的网页或者表格等。
password字段表示邮箱的验证码,在打开smtp协议时记得,并不是邮箱的密码。
"""
def send_email(to_addr,html):
    from_addr = 'wangxingfan316@163.com'
    password = '*********'
    smtp_server = 'smtp.163.com'
    # msg=MIMEText('hello,send by python...','plain','utf-8')
    msg = MIMEMultipart('alternative')
    msg['From'] = u'M10<wangxingfan316@163.com>'
    to_addrs = to_addr.split(',')
    msg['To'] = ','.join(to_addrs)
    #msg['To'] = to_addr
    msg['Subject'] = Header(time.strftime("%Y-%m-%d 自动化日报", time.localtime()), 'utf-8')

    with open(filepath, 'rb') as f:
        # 设置附件的MIME和文件名,这里是png类型:
        mime = MIMEBase('1', 'xlsx', filename='1.xlsx')
        # 加上必要的头信息:
        mime.add_header('Content-Disposition', 'attachment', filename='1.xlsx')
        mime.add_header('Content-ID', '<0>')
        mime.add_header('X-Attachment-Id', '0')
        # 把附件的内容读进来:
        mime.set_payload(f.read())
        # 用Base64编码:
        encoders.encode_base64(mime)
        # 添加到MIMEMultipart:
        msg.attach(mime)
        msg.attach(MIMEText(html, 'html', 'utf-8'))
        try:
            server = smtplib.SMTP(smtp_server, 25)
            server.set_debuglevel(1)
            server.starttls()
            server.login(from_addr, password)
            server.sendmail(from_addr, [to_addr], msg.as_string())
            server.quit()
            print('发送成功')
        except smtplib.SMTPException:
            print('发送失败')

"""
filepath是Excel文件的地址
return的file就是html格式的,可以用于上个函数直接在邮箱附件主页中展示的。
"""
def excel_to_html(filepath):
    xd = pd.ExcelFile(filepath)
    df = xd.parse()
    with codecs.open('/Users/wangxingfan/Desktop/1.html', 'w', 'utf-8') as html_file:
        html_file.write(df.to_html(header=True, index=False))
    file = open('/Users/wangxingfan/Desktop/1.html').read()
    return file
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,以下是一个示例代码,可以将邮件内容添加Excel 文件中,并将其作为附件发送出去: ```python import imaplib import email from openpyxl import load_workbook import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.application import MIMEApplication # 连接邮箱服务器 mail = imaplib.IMAP4_SSL('imap.163.com') mail.login('your_email', 'your_password') mail.select() # 获取邮件 typ, data = mail.search(None, 'ALL') for num in data[0].split(): typ, data = mail.fetch(num, '(RFC822)') msg = email.message_from_bytes(data[0][1]) # 解析邮件内容,提取需要的信息 # ... # 打开 Excel 文件,将信息写入 wb = load_workbook('your_excel_file.xlsx') ws = wb.active ws.append(['info1', 'info2', 'info3']) wb.save('your_excel_file.xlsx') # 发送邮件 from_email = 'your_email' to_email = 'recipient_email' msg = MIMEMultipart() msg['From'] = from_email msg['To'] = to_email msg['Subject'] = 'Mail with Excel attachment' # 添加邮件正文 body_text = 'Please see the attached Excel file for more information.' body = MIMEText(body_text, 'html') msg.attach(body) # 添加 Excel 文件作为附件 with open('your_excel_file.xlsx', 'rb') as f: part = MIMEApplication(f.read(), Name='your_excel_file.xlsx') part['Content-Disposition'] = 'attachment; filename="your_excel_file.xlsx"' msg.attach(part) # 发送邮件 server = smtplib.SMTP('smtp.163.com', 25) server.login('your_email', 'your_password') server.sendmail(from_email, to_email, msg.as_string()) server.quit() # 关闭连接 mail.close() mail.logout() ``` 需要注意的是,示例中的邮件服务器和账号密码信息需要您根据实际情况进行修改。另外,具体的邮件内容解析和 Excel 文件写入方法需要根据实际情况进行编写。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值