<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/unbottling-msg-files-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2017/08/25/unbottling-msg-files-in-r/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://i2.wp.com/rud.is/b/wp-content/uploads/2017/08/shutterstock_203013673-copy.jpg?ssl=1" target="_blank"><img alt="" src="https://i2.wp.com/rud.is/b/wp-content/uploads/2017/08/shutterstock_203013673-copy.jpg?resize=780%2C520&ssl=1" class="wp-image-6182" data-recalc-dims="1"></a></p>
<p>There was a discussion on Twitter about the need to read in “.msg” files using R. The “MSG” file format is one of the many binary abominations created by Microsoft to lock folks and users into their platform and tools. Thankfully, they (eventually) provided documentation for the MSG file format which helped me throw together a <a rel="nofollow" href="https://github.com/hrbrmstr/msgxtractr" target="_blank">small R package</a> — <code>msgxtractr</code> — that can read in these ‘.msg’ files and produce a list as a result.</p>
<p>I had previously creatred a quick version of this by wrapping a Python module, but that’s a path fraught with peril and did not work for one of the requestors (yay, not-so-cross-platform UTF woes). So, I cobbled together some bits and pieces from the C to provide a singular function <code>read_msg()</code> that smashes open bottled up msgs, grabs sane/useful fields and produces a <code>list()</code> with them all wrapped up in a bow (an example is at the end and in the GH README).</p>
<p>Thanks to <a rel="nofollow" href="https://github.com/r-hub/rhub" target="_blank">rhub</a>, <a rel="nofollow" href="https://win-builder.r-project.org/" target="_blank">WinBuilder</a> and <a rel="nofollow" href="https://rud.is/b/2017/08/25/unbottling-msg-files-in-r/travis-ci.org/hrbrmstr/msgxtractr" target="_blank">Travis</a> the code works on macOS, Linux and Windows and even has pretty decent <a rel="nofollow" href="https://codecov.io/gh/hrbrmstr/msgxtractr" target="_blank">code coverage</a> for a quick project. That’s a resounding testimony to the work of many members of the R community who’ve gone to great lengths to make testing virtually painless for package developers.</p>
<p>Now, I literally have a singular ‘.msg’ file to test with, so if folks can kick the tyres, <a rel="nofollow" href="https://github.com/hrbrmstr/msgxtractr/issues" target="_blank">file issues</a> (with errors or feature suggestions) and provide some more ‘.msg’ files for testing, it would be most appreciated.</p>
<pre><code>devtools::install_github(&quot;hrbrmstr/msgxtractr&quot;)

library(msgxtractr)

print(str(read_msg(system.file(&quot;extdata/unicode.msg&quot;, package=&quot;msgxtractr&quot;))))

## List of 7
##  $ headers         :Classes 'tbl_df', 'tbl' and 'data.frame':    1 obs. of  18 variables:
##   ..$ Return-path               : chr &quot;&lt;brizhou@gmail.com>&quot;
##   ..$ Received                  :List of 1
##   .. ..$ : chr [1:4] &quot;from st11p00mm-smtpin007.mac.com ([17.172.84.240])\nby ms06561.mac.com (Oracle Communications Messaging Server &quot;| __truncated__ &quot;from mail-vc0-f182.google.com ([209.85.220.182])\nby st11p00mm-smtpin007.mac.com\n(Oracle Communications Messag&quot;| __truncated__ &quot;by mail-vc0-f182.google.com with SMTP id ie18so3484487vcb.13 for\n&lt;brianzhou@me.com>; Mon, 18 Nov 2013 00:26:25 -0800 (PST)&quot; &quot;by 10.58.207.196 with HTTP; Mon, 18 Nov 2013 00:26:24 -0800 (PST)&quot;
##   ..$ Original-recipient        : chr &quot;rfc822;brianzhou@me.com&quot;
##   ..$ Received-SPF              : chr &quot;pass (st11p00mm-smtpin006.mac.com: domain of brizhou@gmail.com\ndesignates 209.85.220.182 as permitted sender)\&quot;| __truncated__
##   ..$ DKIM-Signature            : chr &quot;v=1; a=rsa-sha256; c=relaxed/relaxed;        d=gmail.com;\ns=20120113; h=mime-version:date:message-id:subject:f&quot;| __truncated__
##   ..$ MIME-version              : chr &quot;1.0&quot;
##   ..$ X-Received                : chr &quot;by 10.221.47.193 with SMTP id ut1mr14470624vcb.8.1384763184960;\nMon, 18 Nov 2013 00:26:24 -0800 (PST)&quot;
##   ..$ Date                      : chr &quot;Mon, 18 Nov 2013 10:26:24 +0200&quot;
##   ..$ Message-id                : chr &quot;&lt;CADtJ4eNjQSkGcBtVteCiTF+YFG89+AcHxK3QZ=-Mt48xygkvdQ@mail.gmail.com>&quot;
##   ..$ Subject                   : chr &quot;Test for TIF files&quot;
##   ..$ From                      : chr &quot;Brian Zhou &lt;brizhou@gmail.com>&quot;
##   ..$ To                        : chr &quot;brianzhou@me.com&quot;
##   ..$ Cc                        : chr &quot;Brian Zhou &lt;brizhou@gmail.com>&quot;
##   ..$ Content-type              : chr &quot;multipart/mixed; boundary=001a113392ecbd7a5404eb6f4d6a&quot;
##   ..$ Authentication-results    : chr &quot;st11p00mm-smtpin007.mac.com; dkim=pass\nreason=\&quot;2048-bit key\&quot; header.d=gmail.com header.i=@gmail.com\nheader.&quot;| __truncated__
##   ..$ x-icloud-spam-score       : chr &quot;33322\nf=gmail.com;e=gmail.com;pp=ham;spf=pass;dkim=pass;wl=absent;pwl=absent&quot;
##   ..$ X-Proofpoint-Virus-Version: chr &quot;vendor=fsecure\nengine=2.50.10432:5.10.8794,1.0.14,0.0.0000\ndefinitions=2013-11-18_02:2013-11-18,2013-11-17,19&quot;| __truncated__
##   ..$ X-Proofpoint-Spam-Details : chr &quot;rule=notspam policy=default score=0 spamscore=0\nsuspectscore=0 phishscore=0 bulkscore=0 adultscore=0 classifie&quot;| __truncated__
##  $ sender          :List of 2
##   ..$ sender_email: chr &quot;brizhou@gmail.com&quot;
##   ..$ sender_name : chr &quot;Brian Zhou&quot;
##  $ recipients      :List of 2
##   ..$ :List of 3
##   .. ..$ display_name : NULL
##   .. ..$ address_type : chr &quot;SMTP&quot;
##   .. ..$ email_address: chr &quot;brianzhou@me.com&quot;
##   ..$ :List of 3
##   .. ..$ display_name : NULL
##   .. ..$ address_type : chr &quot;SMTP&quot;
##   .. ..$ email_address: chr &quot;brizhou@gmail.com&quot;
##  $ subject         : chr &quot;Test for TIF files&quot;
##  $ body            : chr &quot;This is a test email to experiment with the MS Outlook MSG Extractor\r\n\r\n\r\n-- \r\n\r\n\r\nKind regards\r\n&quot;| __truncated__
##  $ attachments     :List of 2
##   ..$ :List of 4
##   .. ..$ filename     : chr &quot;importOl.tif&quot;
##   .. ..$ long_filename: chr &quot;import OleFileIO.tif&quot;
##   .. ..$ mime         : chr &quot;image/tiff&quot;
##   .. ..$ content      : raw [1:969674] 49 49 2a 00 ...
##   ..$ :List of 4
##   .. ..$ filename     : chr &quot;raisedva.tif&quot;
##   .. ..$ long_filename: chr &quot;raised value error.tif&quot;
##   .. ..$ mime         : chr &quot;image/tiff&quot;
##   .. ..$ content      : raw [1:1033142] 49 49 2a 00 ...
##  $ display_envelope:List of 2
##   ..$ display_cc: chr &quot;Brian Zhou&quot;
##   ..$ display_to: chr &quot;brianzhou@me.com&quot;
## NULL</code></pre>
<p>NOTE: Don’t try to read those TIFF images with <code>magick</code> or evan the <code>tiff</code> package. It seems to have some strange tags. But, saving it (use <code>writeBin()</code>) and opening with Preview (or your favorite image viewer) should work (it did for me and produces the following image that I’ve converted to png):</p>
<p><a rel="nofollow" href="https://i1.wp.com/rud.is/b/wp-content/uploads/2017/08/bottle.png?ssl=1" target="_blank"><img alt="" src="https://i1.wp.com/rud.is/b/wp-content/uploads/2017/08/bottle.png?resize=780%2C439&ssl=1" class="wp-image-6187" data-recalc-dims="1"></a></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://rud.is/b/2017/08/25/unbottling-msg-files-in-r/"> R – rud.is</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/D_CkEYpXXSM" height="1"></a></div>