I have a test.html file that I want to send via email(I am refering about the page content). Is there a way for getting the information from the html and sending it as a email? If you have any other ideas please share.
-
1you can do so with python or anyother scripting language. Just google there are many examples, all you have to do is to make sure you set Content-type: text/html in email headers so that target will interpret it accordingly.Johnydep– Johnydep2012-02-10 11:12:21 +00:00Commented Feb 10, 2012 at 11:12
Add a comment
|
2 Answers
Here's a quick and dirty script I just wrote which might be what you're looking for.
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 <[email protected]>
To: Stuart Powers <[email protected]>
MIME-Version: 1.0
Content-type: text/html
Subject: %s
%s""" %(page, content)
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.starttls()
smtpserver.login("[email protected]",os.environ["GPASS"])
smtpserver.sendmail('[email protected]', ['[email protected]'], message)
2 Comments
Florin Dita
Hi! Thank you for the answer even if I had to manually make lxml from binary file since i didn't find a 2.7 installer, this is what I wanted.
sente
haha, yeah lxml can be a pain to install but
make_links_absolute() is pretty handy :) glad to have helped.There are many ways of reading files in python and there are also ways to send emails in python. Why don't you look up the documentation and come back with some coding error ?
Sending emails in python: http://docs.python.org/library/email-examples.html
Reading files in python: http://docs.python.org/tutorial/inputoutput.html
1 Comment
Florin Dita
Hello, I've look at the documentation, what I am asking is if there is a way to have already a page saved in a html file and import the content to an email and send it. The example I saw is how to build a html email, that will mean i will have to create the html again. Is that the only way?