python实现读取excel文件到邮件正文发送邮件功能

python自学笔记(一)
我自己的学习完全是在需求和实践中摸索的,没有系统的学习,所以代码写得不是特别好,希望可以和大家互相交流学习。
代码实现功能说明:
读取固定的一个excel文件的内容,以html的形式将excel文件的内容展示在邮件正文中,显示为表格的形式,同时在邮件正文中拼接其他的文件内容,另将读取的excel文件作为邮件附件发送。

import smtplib
from email.mime.multipart import  MIMEMultipart
from email.header import Header
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import xlrd
import datetime



#创建连接和登陆,smtp服务器地址,端口,发件人账号和密码,
con = smtplib.SMTP_SSL('smtp.163.com',465)
con.login('****@163.com','***password***')


#准备基础数据,主题、发件人、收件人
msg = MIMEMultipart()
#邮件主题
msg['Subject'] = Header('邮件主题测试','utf-8').encode()
#发件人
msg['From'] = '*****@163.com <****@163.com>'
#收件人
msg['To'] = 'bbbb@qq.com'



#写邮件,读取excel文件内容作为邮件正文
def mailWrite():
    #表格的标题和头
    header = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>'
    th = '<body text="#000000" ><table border="1" cellspacing="0" cellpadding="3" bordercolor="#000000" width="180" align="left" ><tr bgcolor="#F79646" align="left" ><th>本地地址</th><th>PBU</th></tr>'
    #打开文件
    #filepath设置详细的文件地址
    filepath = 'E:\报盘配置报表-20200512.xlsx'
    book = xlrd.open_workbook(filepath)
    sheet = book.sheet_by_index(0)
    #获取行列的数目,并以此为范围遍历获取单元数据
    #nrows 行数,ncols 列数
    nrows = sheet.nrows - 1
    ncols = sheet.ncols
    body = ''
    cellData = 1
    for i in range(1,nrows+1):
        td = ''
        for j in range(ncols):
            cellData = sheet.cell_value(i , j)
            #读取单元格数据,赋给cellData变量供写入HTML表格中
            tip = '<td>' + cellData + '</td>'
            td = td + tip
            tr = '<tr>' + td + '</tr>'
            #tr = tr.encode('utf-8')
        body = body + tr
        tail = '</table></body></html>'
        mailcontent = header+th+body+tail
    #将excel文件的内容转换为html格式,后续在邮件中拼接
    return mailcontent


#邮件正文内容
content = mailWrite()
cs = """
<p>今日报盘配置信息如下:</p>
"""
contents = cs + content
html = MIMEText(contents,'html','utf-8')
msg.attach(html)

#邮件附件
part = MIMEApplication(open('E:\报盘配置报表-20200512.xlsx', 'rb').read())
#filename=邮件附件中显示的文件的名称,可自定义
part.add_header('Content-Disposition', 'attachment', filename="报盘配置报表-20200512.xlsx")
msg.attach(part)


#发送邮件
#sendmail(发件人,收件人,内容)
con.sendmail('*****@163.com','bbbb@qq.com',msg.as_string())
con.quit()
print('发送成功!')

  • 7
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,你可以使用Python中的openpyxl库来读取Excel文件,使用smtplib库来发送邮件。以下是一个简单的示例代码: ```python import openpyxl import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication # 读取Excel文件 wb = openpyxl.load_workbook('emails.xlsx') sheet = wb.active # 邮件信息 sender_email = 'your_email@example.com' sender_password = 'your_password' subject = '邮件主题' body = '邮件正文' # 登录邮箱服务器 smtp_server = 'smtp.example.com' smtp_port = 587 smtp = smtplib.SMTP(smtp_server, smtp_port) smtp.starttls() smtp.login(sender_email, sender_password) # 发送邮件 for row in sheet.iter_rows(min_row=2, values_only=True): recipient_email = row[0] recipient_name = row[1] message = MIMEMultipart() message['From'] = sender_email message['To'] = recipient_email message['Subject'] = subject # 正文 message.attach(MIMEText(f'{recipient_name},{body}', 'plain')) # 附件 with open('attachment.pdf', 'rb') as f: attachment = MIMEApplication(f.read(), _subtype='pdf') attachment.add_header('Content-Disposition', 'attachment', filename='attachment.pdf') message.attach(attachment) smtp.sendmail(sender_email, recipient_email, message.as_string()) # 关闭邮箱服务器连接 smtp.quit() ``` 以上代码将Excel文件中第一列作为接收人的邮件地址,第二列作为接收人的姓名。邮件正文中包含接收人的姓名,附件为一个名为`attachment.pdf`的PDF文件。你需要将`your_email@example.com`和`your_password`替换为你自己的邮箱地址和密码,将`smtp.example.com`替换为你的邮箱提供商的SMTP服务器地址,将`587`替换为SMTP服务器的端口号。同时,你还需要将`emails.xlsx`和`attachment.pdf`替换为你自己的Excel文件和附件文件名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值