{"id":1122,"date":"2024-05-02T00:09:14","date_gmt":"2024-05-01T22:09:14","guid":{"rendered":"https:\/\/valentijn.sessink.nl\/?p=1122"},"modified":"2024-05-02T00:09:14","modified_gmt":"2024-05-01T22:09:14","slug":"rtf-body-rtf","status":"publish","type":"post","link":"https:\/\/valentijn.sessink.nl\/?p=1122","title":{"rendered":"rtf-body.rtf?"},"content":{"rendered":"\n<p>While recently getting a rather large mail archive to search through, in PST-format (<em>obviously<\/em>) some of the mail boxes contained e-mails that had &#8220;rtf-body.rtf&#8221; for body text. Yeah, Microsoft really likes Linux.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Anyway, I took a deep breath and wrote some sloppy Python. I now can fix those e-mails server side (a Dovecot imap server). This is just a small note to self &#8211; but should you happen to be in the same situation, please use the script below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/python3\nimport base64\nimport os\nimport sys\nimport re\nimport mimetypes\nimport email\nfrom email.policy import default\nfrom email.parser import BytesParser\nimport subprocess\n\nplcy=default.clone(refold_source='none')\nfor fname in sys.argv&#91;1:]:\n  print(fname)\n  try:\n    mail=open(fname,'rb')\n  except:\n    print(\"Not found\")\n    continue\n  msg = BytesParser(policy=plcy).parse(mail)\n  mail.close()\n  totaal=list(msg.walk())\n  if (totaal&#91;1].get_content_type() == 'application\/rtf'):\n    print(\"convert\")\n    html=subprocess.run(&#91;'\/usr\/bin\/unrtf'], input=totaal&#91;1].get_content(), capture_output=True).stdout\n    totaal&#91;1].set_content(html, maintype='text',subtype='html')\n    try:\n      mail=open(fname,'w')\n    except:\n      print(\"Error writing\")\n      continue\n    print(totaal&#91;0], file=mail)\n    mail.close()\n<\/code><\/pre>\n\n\n\n<p>As said: it&#8217;s a bit messy. It just reads the e-mail as a file, checks if the first mime message is <code>application\/rtf<\/code> and if so, pushes the content through the <code>unrtf<\/code> utility to make it <code>HTML<\/code>. Fun fact: the Python email module pushes the right buttons automatically, i.e. replacing totaal[1] with something else will keep the message intact, so the print statement at the end pushes out the e-mail with fixed content.<\/p>\n\n\n\n<p>Running it is simple: on the server, go to <code>\/var\/mail\/username\/.Some.mail.archive\/cur<\/code> and use <code>~\/rtftohtmlmail.py *<\/code><\/p>\n\n\n\n<p>Please note that there&#8217;s not much error checking. So please please please make a copy of your mail box first!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While recently getting a rather large mail archive to search through, in PST-format (obviously) some of the mail boxes contained e-mails that had &#8220;rtf-body.rtf&#8221; for body text. Yeah, Microsoft really likes Linux.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,22],"tags":[9],"class_list":["post-1122","post","type-post","status-publish","format-standard","hentry","category-happy-hacking","category-vrije-software","tag-linux"],"_links":{"self":[{"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=\/wp\/v2\/posts\/1122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1122"}],"version-history":[{"count":1,"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=\/wp\/v2\/posts\/1122\/revisions"}],"predecessor-version":[{"id":1123,"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=\/wp\/v2\/posts\/1122\/revisions\/1123"}],"wp:attachment":[{"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/valentijn.sessink.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}