Class: Mail2cb::EmailContent

Inherits:
Object
  • Object
show all
Defined in:
lib/mail2cb/email_content.rb

Instance Method Summary collapse

Constructor Details

#initialize(email) ⇒ EmailContent

Returns a new instance of EmailContent.



4
5
6
7
8
9
10
11
12
13
# File 'lib/mail2cb/email_content.rb', line 4

def initialize(email)
  @email = email

  if @email.multipart?
    parse_parts(@email.parts)
  else
    @text_body = @email.body.decoded.force_encoding("ASCII-8BIT").encode('UTF-8', undef: :replace, replace: '')
  end
  @body = @html_body || @text_body
end

Instance Method Details

#bodyObject



36
37
38
# File 'lib/mail2cb/email_content.rb', line 36

def body
  @body
end

#body=(value) ⇒ Object



39
40
41
# File 'lib/mail2cb/email_content.rb', line 39

def body=(value)
  @body = value
end

#htmlObject



28
29
30
# File 'lib/mail2cb/email_content.rb', line 28

def html
  @html_body
end

#parse_parts(parts) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mail2cb/email_content.rb', line 15

def parse_parts(parts)
  parts.each do |part|
    content_type = part.content_type.split(";")[0]
    if content_type == "text/html"
      @html_body = part.body.decoded.force_encoding("ASCII-8BIT").encode('UTF-8', undef: :replace, replace: '')
    elsif content_type == "text/plain"
      @text_body = part.body.decoded.force_encoding("ASCII-8BIT").encode('UTF-8', undef: :replace, replace: '')
    elsif content_type == "multipart/related"
      parse_parts(part.parts)
    end
  end
end

#textObject



32
33
34
# File 'lib/mail2cb/email_content.rb', line 32

def text
  @text_body
end