Skip to content

Instantly share code, notes, and snippets.

@kamykaze
Created December 7, 2012 22:32
Show Gist options
  • Save kamykaze/4237084 to your computer and use it in GitHub Desktop.
Save kamykaze/4237084 to your computer and use it in GitHub Desktop.
Script for processing an html email file (converts css to inline style and updates image paths)
#!/usr/bin/env python
import sys, getopt
from premailer import Premailer
#############################
# Compiles html email for delivery
# usage:
# python compile_email.py -i template.html -o compiled.html -b http://www.mydomain.com/static/
#
# requirements: (install via pip install)
# - premailer
# - cssselect
#
def main(script_name, argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:b:",["ifile=","ofile=","baseurl="])
except getopt.GetoptError:
print '%s -i <inputfile> -o <outputfile>'%script_name
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
elif opt in ("-b", "--baseurl"):
baseurl = arg
print 'Loading html file: %s'%inputfile
print 'Saving output file: %s'%outputfile
html = open(inputfile, 'r').read()
if baseurl:
p = Premailer(html, remove_classes=False, base_url=baseurl)
else:
p = Premailer(html, remove_classes=False)
out_html = p.transform()
outf = open(outputfile, 'w')
outf.write(out_html)
if __name__ == "__main__":
main(sys.argv[0], sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment