Any suggestions on RFC822 email formatting as JSON?

I want to insert RFC822 emails in a JSON database.

I want to provide a “good” way to structure email as JSON (excluding attachments).

Does anyone know an example of a “good” JSON layout for email, or can anyone suggest it?

thanks

UPDATE: I am really looking for JSON output for output, not code for conversion.

+4
source share
2 answers

Not done on purpose, but the Mail gem includes a method for serializing methods for YAML, as well as for parsing messages from YAML. He does this by creating a Hash object and then serializing that object in JSON. It also means that it is easy to convert to JSON.

It will look like (ish) like this ...

 require 'json' module Mail class Message def to_json(opts = {}) hash = {} hash['headers'] = {} header.fields.each do |field| hash['headers'][field.name] = field.value end hash['delivery_handler'] = delivery_handler.to_s if delivery_handler hash['transport_encoding'] = transport_encoding.to_s special_variables = [:@header, :@delivery_handler, :@transport_encoding] (instance_variables.map(&:to_sym) - special_variables).each do |var| hash[var.to_s] = instance_variable_get(var) end hash.to_json(opts) end end end 

From the Message#to_yaml method of the Message#to_yaml mailbox.

You can use a similar approach in any language. If you use Ruby, it will go well with the existing tool (mail pearls).

0
source

You probably need some kind of option that lists the headers and body parts.

  { headers: [ { name: value }, { name2: value} ], body: [ { mime: type, content: stuff }, { }, . . . { } ] } 
0
source

All Articles