通过python电子邮件脚本

时间:2018-04-12 21:10:41

标签: python python-3.x

寻找一种方法来发送每天使用此脚本生成一次的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()

1 个答案:

答案 0 :(得分:0)

你真的很亲近。三个变化:

不要以二进制模式打开html文件。将文件直接读入html字符串

report_file = open('export.html')
html = report_file.read()

删除后续分配给html var

html = """\

"""

将msg对象发送为构造

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

这对我有用。另请注意,默认情况下,gmail设置可能不允许您的脚本发送邮件。如果是这样,您需要更新设置以允许"不安全" (意思是非谷歌)应用程序发送邮件。