python发html邮件_通过python电子邮件scrip发送HTML文件内容

寻找一种方法来发送使用下面的脚本每天生成一次的HTML文件的内容。遇到阻碍它工作的路障。我可以发送HTML并查看它,只是不知道如何打印出文件的内容并发送它。

文件格式为export-MM-DD-YY.html

我宁愿让它在电子邮件而不是HTML文件中显示HTML的内容。#!/usr/bin/env python3

import smtplib

import config

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

filename = 'name_of_file'

# Read a file and encode it into base64 format

fo = open(filename, "rb")

filecontent = fo.read()

encodedcontent = base64.b64encode(filecontent) # base64

filename = os.path.basename(filename)

# me == my email address

# you == recipient's email address

me = "From@example.com"

you = "TO@example.com"

subject = 'Test Subject v5'

# Create message container - the correct MIME type is multipart/alternative.

msg = MIMEMultipart('alternative')

msg['Subject'] = subject

msg['From'] = me

msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).

text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttps://www.python.org"

html = """\

**INSERT HTML FILE CONTENTS HERE...**

"""

# Record the MIME types of both parts - text/plain and text/html.

part1 = MIMEText(text, 'plain')

part2 = MIMEText(html, 'html')

# Attach parts into message container.

# According to RFC 2046, the last part of a multipart message, in this case

# the HTML message, is best and preferred.

msg.attach(part1)

msg.attach(part2)

# Send the message via local SMTP server.

server = smtplib.SMTP('smtp.gmail.com:587')

server.starttls()

server.login(config.EMAIL_ADDRESS, config.PASSWORD)

server.sendmail(me,you,msg.as_string())

server.quit()

所以我想我已经开始工作了,但我确信这里的代码比需要的多(如果有人看到我可以清理的东西?)#!/usr/bin/env python3

import smtplib

import os

import config

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

raport_file = open('export.html','rb')

alert_msg = MIMEText(raport_file.read(),"html", "utf-8")

# me == my email address

# you == recipient's email address

me = "From@example.com"

you = "TO@example.com"

subject = 'Test Subject v5'

# Create message container - the correct MIME type is multipart/alternative.

msg = MIMEMultipart('alternative')

msg['Subject'] = subject

msg['From'] = me

msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).

text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttps://www.python.org"

html = """\

"""

# Record the MIME types of both parts - text/plain and text/html.

part1 = MIMEText(text, 'plain')

part2 = MIMEText(html, 'html')

# Attach parts into message container.

# According to RFC 2046, the last part of a multipart message, in this case

# the HTML message, is best and preferred.

msg.attach(part1)

msg.attach(part2)

# Send the message via local SMTP server.

server = smtplib.SMTP('smtp.gmail.com:587')

server.starttls()

server.login(config.EMAIL_ADDRESS, config.PASSWORD)

server.sendmail(me,you,alert_msg.as_string())

server.quit()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值