'Saving to .msg file in Python, or alternatively, sending mail to the file system
I'm using the emails library to send mail, but I also need to save it as .msg file. I've done some research and also read the msg format specification and stumbled upon this SO answer that shows how to send mail to the file system in C# and I was wondering if it was possible in Python as well.
Solution 1:[1]
it is doable in Python, I tried the following code which saves outlook messages as .msg in a folder. Note:make sure that outlook has write access to the destination folder, by default the destination folder is the location of Python script
from win32com.client import Dispatch
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.items
for msg in messages:
name = msg.subject
name = str(name)
name = name + ".msg"
msg.saveas(name)
Solution 2:[2]
Yes it's possible. There are modules for these purposes and it is called MSG PY. For example:
from independentsoft.msg import Message
from independentsoft.msg import Recipient
from independentsoft.msg import ObjectType
from independentsoft.msg import DisplayType
from independentsoft.msg import RecipientType
from independentsoft.msg import MessageFlag
from independentsoft.msg import StoreSupportMask
message = Message()
recipient1 = Recipient()
recipient1.address_type = "SMTP"
recipient1.display_type = DisplayType.MAIL_USER
recipient1.object_type = ObjectType.MAIL_USER
recipient1.display_name = "John Smith"
recipient1.email_address = "[email protected]"
recipient1.recipient_type = RecipientType.TO
recipient2 = Recipient()
recipient2.address_type = "SMTP"
recipient2.display_type = DisplayType.MAIL_USER
recipient2.object_type = ObjectType.MAIL_USER
recipient2.display_name = "Mary Smith"
recipient2.email_address = "[email protected]"
recipient2.recipient_type = RecipientType.CC
message.subject = "Test"
message.body = "Body text"
message.display_to = "John Smith"
message.display_cc = "Mary Smith"
message.recipients.append(recipient1)
message.recipients.append(recipient2)
message.message_flags.append(MessageFlag.UNSENT)
message.store_support_masks.append(StoreSupportMask.CREATE)
message.save("e:\\message.msg")
Solution 3:[3]
I think the answer to this (but the question is not very clear, and parts of it are in the comments) is:
import email
from email.message import EmailMessage
If msg is something created using msg = EmailMessage() and populated with appropriate calls (see https://docs.python.org/3/library/email.examples.html) then msg can be saved using
with open('mymessage.msg', 'wb') as f:
f.write(bytes(msg))
and that file can be recovered using:
with open('mymessage.msg', 'rb') as fp:
msg = email.message_from_binary_file(fp)
Solution 4:[4]
As per python's official examples - https://docs.python.org/3/library/email.examples.html it is possible to save an email as a .msg file, and then later on load it.
from email.message import EmailMessage
from email.parser import BytesParser
# Create the base text message.
msg = EmailMessage()
msg['Subject'] = "Ayons asperges pour le déjeuner"
msg['From'] = Address("Pepé Le Pew", "pepe", "example.com")
msg['To'] = (Address("Penelope Pussycat", "penelope", "example.com"),
Address("Fabrette Pussycat", "fabrette", "example.com"))
msg.set_content("""\
Salut!
Cela ressemble à un excellent recipie[1] déjeuner.
[1] http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718
--Pepé
""")
# Make a local copy of what we are going to send.
with open('outgoing.msg', 'wb') as f:
f.write(bytes(msg))
# Load local copy of email
with open('outgoing.msg', 'rb') as fp:
loaded_msg = BytesParser(policy=policy.default).parse(fp)
Solution 5:[5]
I've managed to complete this using Win32Com...
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application")
mapi = outlook.GetNamespace("MAPI")
inbox = mapi.GetDefaultFolder(6)
messages = inbox.Items
message.SaveAs(os.path.join("output folder", (message.Subject + ".msg")))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | creimers |
| Solution 2 | Arne S |
| Solution 3 | CPBL |
| Solution 4 | Sneha Tunga |
| Solution 5 | Raphael Frei |
