Last active
July 12, 2022 12:05
-
-
Save honnamkuan/4604fa7220169209487fdfbb07acf4d0 to your computer and use it in GitHub Desktop.
osx_msg_outlook_preview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on run {input, parameters} | |
repeat with inp in input | |
set f to quoted form of (POSIX path of (inp as text)) | |
do shell script "open -n -a OutlookPreview.app --args " & f | |
end repeat | |
end run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
runpy3 () { | |
/usr/bin/env python3 <<'EOF' - "$@" | |
import extract_msg | |
import os | |
import subprocess | |
import sys | |
from email import generator | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
import tempfile | |
from email.mime.application import MIMEApplication | |
class Email(object): | |
def __init__(self, msg, filename): | |
self.__msg = msg | |
self.__filename = filename | |
self.__createEmail() | |
def __createEmail(self): | |
sender = self.__msg.sender | |
recepient = self.__msg.to | |
subject = self.__msg.subject | |
msg = MIMEMultipart('mixed') | |
msg['Subject'] = subject | |
msg['From'] = sender | |
msg['To'] = recepient | |
for a in self.__msg.attachments: | |
if not isinstance(a.data, extract_msg.Message): | |
msg.attach(MIMEApplication(a.data, Name=a.longFilename)) | |
else: | |
m = a.data | |
for b in m.attachments: | |
msg.attach(MIMEApplication(b.data, Name=b.longFilename)) | |
html = self.__msg.htmlBody if not self.__msg.htmlBody is None else self.__msg.body | |
try: | |
html = html.decode('utf-8') | |
except UnicodeDecodeError as e: | |
html = html.decode('iso-8859-1') | |
except AttributeError as e: | |
html = html | |
part = MIMEText(html, 'html') if not self.__msg.htmlBody is None else MIMEText(html, 'plain') | |
msg.attach(part) | |
outfile_name = self.__saveToFile(msg) | |
def open(self): | |
filename = self.__eml_file_name | |
try: | |
subprocess.run(["open", filename, "-a", "Microsoft Outlook.app"], check = True) | |
except: | |
subprocess.run(["open", filename, "-a", "Mail.app"], check = True) | |
def __saveToFile(self,msg): | |
old_name = self.__filename | |
old_name = os.path.basename(old_name) | |
pre, ext = os.path.splitext(old_name) | |
outfile_name = pre + ".eml" | |
temp_dir = tempfile.TemporaryDirectory().name | |
os.mkdir(temp_dir) | |
outfile_path = temp_dir + "/" + outfile_name | |
with open(outfile_path, 'w') as outfile: | |
gen = generator.Generator(outfile) | |
gen.flatten(msg) | |
self.__eml_file_name = outfile_path | |
class OutlookPreview(object): | |
def __init__(self, argv): | |
file_name = None | |
for a in argv: | |
if a.endswith(".msg"): | |
file_name = a | |
msg = extract_msg.Message(file_name) | |
email = Email(msg, file_name) | |
email.open() | |
OutlookPreview(sys.argv) | |
EOF | |
} | |
runpy3 "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I et the following error in Automator:
The action “Run Shell Script” encountered an error: “Traceback (most recent call last):
File "", line 1, in
ModuleNotFoundError: No module named 'extract_msg'”