Convert an Outlook Email File to PNG in Ruby
2 min readAug 5, 2021
If you don’t have an Outlook account, opening an EML file can be a real pain. To avoid having to download and install a separate reader or program just to view the file, you can use the following API in Ruby to automatically convert the EML file to JPG/JPEG image array.
To begin, we will add the Ruby client to our Gemfile:
gem 'cloudmersive-convert-api-client', '~> 2.1.6'
Next, we can add the EML file and API key into the below example code to call the function:
# load the gem
require 'cloudmersive-convert-api-client'
# setup authorization
CloudmersiveConvertApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
endapi_instance = CloudmersiveConvertApiClient::ConvertDocumentApi.newinput_file = File.new('/path/to/inputfile') # File | Input file to perform the operation on.opts = {
quality: 56 # Integer | Optional; Set the JPEG quality level; lowest quality is 1 (highest compression), highest quality (lowest compression) is 100; recommended value is 75. Default value is 75.
}begin
#Convert Email EML file to JPG/JPEG image array
result = api_instance.convert_document_eml_to_jpg(input_file, opts)
p result
rescue CloudmersiveConvertApiClient::ApiError => e
puts "Exception when calling ConvertDocumentApi->convert_document_eml_to_jpg: #{e}"
end
This will also allow you to adjust the compression/quality level if you choose — the default is 75, but it can be set between 1–100 to meet your needs.