Skip to content

Instantly share code, notes, and snippets.

@martinmev
Created November 8, 2011 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinmev/1348485 to your computer and use it in GitHub Desktop.
Save martinmev/1348485 to your computer and use it in GitHub Desktop.
Script is useful for creating the advanced HTML newsletter. It reads the file with the email message created by Mozilla Thunderbird (variable filename) with attached images. It replaces image filenames (it uses MD5 checksum; variables fileImages, ...
#!/usr/bin/env python2.7
# Copyright Martin Mevald 2011.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
# settings
fileImages = ['hl1.png','Afrika_party_212px.jpg','vlnovka.png','f.png','distribuce.jpg','teachers_first round_demonstration180px.jpg','vlnovka0.png','rusinga_island_rustinga_1_180px.jpg','IMG_0075_180px.jpg','3_180px.jpg','afrika_nevsednima_ocima_program_partnerstvi_partnerstvi_180px.jpg','zapati.png']
imagesPath = 'C:\\Users\Martin\\Documents\\Bulletin\\'
filename='C:\\Users\\Martin\\Documents\\Bulletin Centra Narovinu.eml'
"""
Script is useful for creating the advanced HTML newsletter.
It reads the file with the email message created by Mozilla Thunderbird (variable filename) with attached images.
It replaces image filenames (it uses MD5 checksum; variables fileImages, imagesPath) with their Content-ID.
Script writes the output to [filename].output .
"""
import md5
import base64
md5Images = {}
#force
#md5Images['95e735cd48776c326967a722e2517abd']='x.jpg'
# load images
for fileImage in fileImages:
f = open(imagesPath+fileImage,"rb")
image = f.read()
f.close()
digest = md5.md5(image).hexdigest()
print fileImage,digest
md5Images[digest] = fileImage
#endfor
f = open(filename,"r")
lines = []
for line in f.readlines():
lines.append(line.replace('\n','').replace('\r',''))
#endfor
f.close()
#print len(lines)
readingImage = False
images = []
for line in lines:
if not readingImage:
img = ''
contentId = ''
if line[0:11] == 'Content-ID:':
contentId=line.split(' ')[1].replace('<','').replace('>','')
readingImage = True
print contentId
#endif
else:
if line[0:5]=='-----':
# complete image
image=base64.b64decode(img)
digest = md5.md5(image).hexdigest()
if not md5Images.has_key(digest):
raise(Warning("Digest: %s, no image for Content-ID: %s, img: %s" % (digest,contentId,img)))
#endif
images.append((contentId,md5Images[digest]))
readingImage = False
elif line=='' or (line.find('Content-Disposition:')>=0) or (line.find(' filename')>=0):
continue
else:
img+=line
#endif
#endif
#endfor
print
print
print images
# write
f = open(filename+".output","w")
for line in lines:
for image in images:
output = line.replace(image[1],'cid:'+image[0])
if line!=output:
break
#endif
#endif
f.write(output+'\n')
#endfor
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment