1. Introduction

Generally, email messages from popular email clients like Microsoft Outlook use their proprietary format with the extension .msg to save the message. There are efficient ways to see and access such file content on Linux computers, even if users don’t have native support.

In this tutorial, we’ll discuss the different steps involved in reading the .msg file in Linux Systems.

We tested the commands and code on Ubuntu 20.04 LTS with Bash 5.0.17, and it should work well in most POSIX-compliant environments unless mentioned otherwise.

2. Installation of msgconvert Module

To help parse and modify Microsoft Outlook email messages in Outlook Message (.msg) format, let’s install the libemail-outlook-message-perl module now. It installs the required perl dependencies from the selected package repositories.

The -y option tells the package management to presume yes in response to user prompts, making installation simple using the apt-get command. This option facilitates the installation of packages without the need for human participation:

$ sudo apt-get install libemail-outlook-message-perl -y
[sudo] password for labenv:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libalgorithm-c3-perl libb-hooks-endofscope-perl libb-hooks-op-check-perl
  libcapture-tiny-perl libclass-c3-perl libclass-c3-xs-perl libclass-load-perl
...
... output truncated ...
...
Setting up libemail-outlook-message-perl (0.921-1) ...
Setting up libdevel-partialdump-perl (0.20-1) ...
Processing triggers for man-db (2.10.2-1) ...

Now, the module installation is complete. Let’s see the installation path of msgconvert using the which command:

$ which msgconvert
/usr/bin/msgconvert

The msgconvert command on the /usr/bin path converts the messages from .msg to human readable .eml format.

3. Generating a .eml File

Now, we have an Outlook message in the current path in the .msg format. Let’s proceed by utilizing the tree command to obtain the path structure of the directory. The msgconvert command serves as the tool designed for this specific conversion process.

Here, we’ll provide the .msg file as the argument to the msgconvert command, indicating that the conversion should be applied to the Outlook message Welcome to Baeldung.msg:

$ tree
.
└── Welcome to Baeldung.msg

0 directories, 1 file

It transforms the proprietary Outlook message format into a more human-readable .eml format. Further, it enhances the flexibility in handling email messages across the Linux platform, which may not natively support the .msg format:

$ msgconvert Welcome\ to\ Baeldung.msg
$ tree
.
├── Welcome to Baeldung.eml
└── Welcome to Baeldung.msg

0 directories, 2 files

Additionally, the execution of the command generates a new file containing the .msg content in the .eml format, with the default name being the same as the .msg file.

Nevertheless, we can leverage the –outfile option to rename the output file:

$ msgconvert --outfile BaeldungMsg.eml Welcome\ to\ Baeldung.msg
$ tree
.
├── BaeldungMsg.eml
└── Welcome to Baeldung.msg

0 directories, 2 files

In this case, we’ve given Welcome to Baeldung.msg as the input file and BaeldungMsg.eml as the output file.

Additionally, the –verbose option assists in obtaining a comprehensive output of the conversion process:

$ msgconvert --verbose --outfile BaeldungMsg.eml Welcome\ to\ Baeldung.msg
Skipping DIR entry __nameid_version1 0 (Introductory stuff)
Skipping property 0102:0FF6 (Instance Key): ..&.
Skipping property 000B:3A40 (Send Rich Info): 1
...
... output truncated ...
...
Using    property 001F:0C1A (FROM): Sriram Ramanujam
Skipping property 001F:800C (UNKNOWN): 192.168.221.163

This feature proves beneficial not only for troubleshooting purposes but also for gaining deeper insights into each step of the process.

4. Reviewing Email Contents

Next, let’s open the BaeldungMsg.eml file generated using msgconvert. The file initiates the email message’s header section, typically containing essential metadata and routing details. It commences with standard elements such as date and MIME version.

The Content-Type specifies multiple content versions as multipart/alternative, followed by subject, sender, and recipient information, including a unique message identifier. Received headers trace the email’s path through servers, offering timestamps and server details:

$ cat BaeldungMsg.eml
Date: Tue, 6 Feb 2024 17:26:02 -0800
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary=17072693510.4ceA136bc.2978
Content-Transfer-Encoding: 7bit
Subject: Welcome to Baeldung
From: Sriram Ramanujam
To: Sriram Ramanujam
Message-Id:
Received: from FGT-EXCH-MBX34.baeldung-us.com (192.168.221.34) by
...
... output truncated ...
...
--17072693510.4ceA136bc.2978
Content-Type: text/plain; charset=UTF-8
Content-Disposition: inline
Content-Transfer-Encoding: 8bit

Hello Team,
Baeldung Welcomes You!
Regards,
Baeldung Team.


--17072693510.4ceA136bc.2978
Content-Type: application/rtf
...
... output truncated ...
...

Subsequently, the body section reveals plain text content with UTF-8 encoding. Content-Disposition: inline suggests inline display, and 8bit encoding ensures efficient transmission. Following this, the original email content emerges, starting with Hello Team and concluding with Baeldung Team:

$ awk '/Content-Transfer-Encoding: 8bit/,/--/' Welcome\ to\ Baeldung.eml | sed '1d;$d'
Hello Team,
Baeldung Welcomes You!
Regards,
Baeldung Team.

Furthermore, we use the awk command to parse the .eml file, displaying only the message content.

4. Conclusion

In this article, we’ve discussed the steps involved in reading the .msg file in Linux Systems. In summary, the msgconvert is a common choice for converting .msg files to more universally readable formats like .eml.

By deploying the msgconvert tool, users can seamlessly view and work with Outlook messages, bridging the gap between proprietary formats and open-source environments.

It also further empowers users to manage their email content effectively.