Get attachments from .msg files using Ruby

A colleague had a lot of .msg and .msg.pst email files where they needed to extract the attachments from them, Ruby to the rescue!

I used the msg gem from https://github.com/aquasync/ruby-msg

I put all the files in a directory called emails/ and when the script is done running it will output the attachments to a directory named attachments/.

require 'bundler/inline'
gemfile do
  source 'https://rubygems.org'

  # We're using a git reference here because https://github.com/aquasync/ruby-msg/issues/14 is 
  # only fixed in master
  gem 'ruby-msg', github: 'aquasync/ruby-msg', ref: '34fb32e43e29b3d5fa535537faf7123a176146a8'
end

require 'mapi/msg'
require 'fileutils'

directory_path = 'attachments'
FileUtils.rm_rf(directory_path) if Dir.exist?('attachments')
FileUtils.mkdir_p(directory_path)

Dir.glob(File.join('emails', '*.{msg,msg.pst}')).each_with_index do |msg_file, mindex|
  puts "Processing #{msg_file}"

  msg = Mapi::Msg.open(msg_file)
  msg.attachments.each_with_index do |a, aindex|
    attachment_location = File.join('attachments', "#{mindex}-#{aindex}-#{File.basename(a.filename || 'attachment')}")
    raise if File.exist?(attachment_location)
    puts ".. output: #{attachment_location}"
    a.save(open(attachment_location, 'wb'))
    puts ''
  end
end

 
0
Kudos
 
0
Kudos

Now read this

Zero-downtime upgrades with AWS Elastic Loadbalancers (ELBs) and Haproxy

I have a Classic Loadbalancer configured in my infrastructure with Terraform: resource "aws_elb" "ingress" { # (...) # Regular traffic: listener { lb_port = 80 lb_protocol = "tcp" instance_port = 8888 instance_protocol = "tcp" } listener... Continue →