Mail.app mit Python steuern
Mittwoch, den 31. März 2010Ich bin ein großer Fan von Mail.app. Die sqlitebasierte Suche ist super schnell und hilft mir im beruflichen Alltag ungemein. Da ich regelmäßig viele Dateien versenden muss, und nein, ich kann die nicht irgendwo zum Download anbieten, habe ich nach einer smarten Lösung gesucht um Mail.app von der Konsole aus zu steuern. Genauer gesagt wollte ich einen Skript haben das ich mit den benötigten Informationen wie div. Emfpänger, Betreff und eben jede Menge Dateien aufrufe. Das Skript öffnet anschließend Mail.app, erstellt eine neue Nachricht, trägt die Empfänger und den Betreff ein und fügt die Dateien als Anhänge hinzu.
Bei SuperUser.com wurde ich fündig:http://superuser.com/questions/125707/write-an-email-with-attachment-with-mail-app-from-terminal/. Doch wollte ich das ganz nicht mit nem billigen Shellskript sondern dauerhaft mit Python machen. Da kam mir appscript gerade recht.
Appscript bietet eine Schnittstelle zu Apple Script, sowohl für Ruby als auch für Python. Wuhuuu. Damit kann man so ziemlich jede Anwendung steuern, Stichwort Interprozesskommunikation, oder kurz IPC. Nach ein wenig Spielerei kam dabei das hier raus:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | #!/usr/bin/env python # encoding: utf-8 """ mailapp.py Created by Fabian Schächter on 2010-03-31. Copyright (c) 2010 . All rights reserved. """ __author__ = 'Fabian Schächter <fabian@schaechter.info>' __version__ = '0.1' import optparse import appscript import mactypes import os def talk_to_mailapp(recipients, subject, files): """talks to mail.app through appscript""" mail = appscript.app('Mail') k = appscript.k message = mail.make( new=k.outgoing_message, with_properties={ k.visible: True, k.subject: unicode(subject), } ) for recipient in recipients: message.to_recipients.end.make( new=k.to_recipient, with_properties={ k.address: recipient, } ) if files is not None and len(files) > 0: for f in files: message.content.make( at=message.content.paragraphs.first.before, new=k.paragraph, with_data='nn', ) file_path = os.path.realpath(f) app_file = mactypes.Alias(file_path) message.content.make( at=message.content.paragraphs.first.after, new=k.attachment, with_properties={ k.file_name: app_file, } ) def main(): """main""" parser = optparse.OptionParser() parser.add_option( '-t', '--to', dest='recipient', action='append', help='recipient' ) parser.add_option( '-s', '--subject', dest='subject', help='subject', ) parser.add_option( '-f', '--file', dest='files', action='append', help='files to be added as an attachment' ) (options, args) = parser.parse_args() if options.recipient == None: parser.error('recipient is required') if options.subject == None: parser.error('subject is required') talk_to_mailapp(options.recipient, options.subject, options.files) if __name__ == '__main__': main() |
Nun reicht mit ein einfaches mailapp.py -t aneine@addresse.de -t undnoch@eine.de -s “der betreff” -f datei1.txt -f datei2.txt -f datei3.txt und schon habe ich eine Mail in Mail.app angelegt und alle Dateien sind schon angehängt.