【发布时间】:2012-03-02 20:37:55
【问题描述】:
我有一个要通过电子邮件发送的 test.html 文件(我指的是页面内容)。有没有办法从 html 中获取信息并将其作为电子邮件发送?如果您有任何其他想法,请分享。
【问题讨论】:
-
您可以使用 python 或任何其他脚本语言来执行此操作。只是谷歌有很多例子,你所要做的就是确保你在电子邮件标题中设置 Content-type: text/html 以便目标会相应地解释它。
我有一个要通过电子邮件发送的 test.html 文件(我指的是页面内容)。有没有办法从 html 中获取信息并将其作为电子邮件发送?如果您有任何其他想法,请分享。
【问题讨论】:
这是我刚刚编写的一个快速而肮脏的脚本,它可能是您正在寻找的。p>
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)
【讨论】:
make_links_absolute() 非常方便 :) 很高兴能帮上忙。
在python中有很多读取文件的方法,也有在python中发送电子邮件的方法。您为什么不查找文档并返回一些编码错误?
在 python 中发送电子邮件:http://docs.python.org/library/email-examples.html
在python中读取文件:http://docs.python.org/tutorial/inputoutput.html
【讨论】: