通过Python发送Html文件
我有一个 test.html 文件,里面是我想通过电子邮件发送的页面内容。有没有办法把这个 HTML 文件里的信息提取出来,然后发送成邮件呢?如果你有其他的好主意,请分享一下。
2 个回答
1
在Python中,有很多种方法可以读取文件,也有很多方法可以发送邮件。你可以去查一下相关的文档,然后再回来看看有没有什么代码错误。
在Python中发送邮件的相关信息: http://docs.python.org/library/email-examples.html
在Python中读取文件的相关信息: http://docs.python.org/tutorial/inputoutput.html
2
这是我刚写的一个简单脚本,可能正是你需要的东西。
https://gist.github.com/1790847
"""
this is a quick and dirty script to send HTML email - emphasis on dirty :)
python emailpage.py http://www.sente.cc
made to answer: http://stackoverflow.com/questions/9226719/sending-a-html-file-via-python
Stuart Powers
"""
import lxml.html
import smtplib
import sys
import os
page = sys.argv[1] #the webpage to send
root = lxml.html.parse(page).getroot()
root.make_links_absolute()
content = lxml.html.tostring(root)
message = """From: Stuart Powers <stuart.powers@gmail.com>
To: Stuart Powers <stuart.powers@gmail.com>
MIME-Version: 1.0
Content-type: text/html
Subject: %s
%s""" %(page, content)
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.starttls()
smtpserver.login("stuart.powers@gmail.com",os.environ["GPASS"])
smtpserver.sendmail('stuart.powers@gmail.com', ['stuart.powers@gmail.com'], message)