开发者

Any suggestions for how to format an RFC822 email as JSON?

开发者 https://www.devze.com 2023-03-23 17:25 出处:网络
I am wanting to insert RFC822 emails into a JSON database. I\'m wanting to envisage a \"good\" way to structure the email as JSON (excluding attachments).

I am wanting to insert RFC822 emails into a JSON database.

I'm wanting to envisage a "good" way to structure the email as JSON (excluding attachments).

Does anyone know of an example of a "good" JSON lay开发者_JAVA百科out for an email, alternatively can anyone suggest one?

thanks

UPDATE: I'm actually looking to see the JSON output rather than the code to do the conversion.


Not done it specifically, but the Mail gem includes a method for serializing methods to YAML, and also for parsing messages from YAML. It does this by constructing a Hash object and then serializing that object to JSON. That also means it should be easy to convert to JSON.

This would prolly look (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 mikel's Mail gem.

You could take a similar approach in any language. If you're using Ruby though, this would fit great with your existing tooling (the Mail gem).


You probably want some variant of something that lists the headers and body parts.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号