Python for experts - A kind of Magic
This turned out to have a simple and very neat solution, but boy was I struggling before.
The problem was that of calling the default mail client with an attachment ready to be sent, once my program finished parsing the XML log files (the attachment being the statistical analysis file).
Sounds simple? Yeah, right.
First, I struggled to find out how to get that done in VB or C#, replicating the behavior of right-clicking on a file and choosing the 'send to mail recipient' option. A reply from the python mailing-list hinted towards this direction too.
Integrating that with the python code wasn't much of a problem but the solutions didn't work very well, certainly weren't cross-platform, and were everything but neat.
Then, also from the python-mailing-list came a precious tip: create a MIME encoded .eml file and run it from within python.
VoilĂ !
So, I saved an email draft of my own (with a text file attachment), opened it up, studied its contents, and created a file of my own from within my python program.
Works like a charm.
4-5 lines of code, excluding the .eml-specific content. Check out the recipe for yourself:
try:
email_output = codecs.open("<filename>.eml", "wb")
email_output.write(<dump .eml required blurb plus own content>)
finally:
email_ouput.close()
import os
os.startfile("<filename>.eml')
Launching the file through os.startfile(<filename>) will cause the Operating System to open it with whatever program is associated with that extension, typically the default mail client.
Nice.
No comments:
Post a Comment