Sha256: 7e01e3425ef9c91ef9f1391959dcb159b33ef37ac2db44f37d8c8fce903d5de1
Contents?: true
Size: 976 Bytes
Versions: 13
Compression:
Stored size: 976 Bytes
Contents
require 'nokogiri' module Spreewald class MailToPlaintextConverter def initialize(mail, type = '') @mail = mail @type = type end def run body = '' if mail.multipart? body = if mail.html_part && type != 'plain-text' text_from_html(mail.html_part.body.to_s) elsif mail.text_part && type != 'HTML' mail.text_part.body.to_s else mail.body.to_s end else if body_text_html? body = text_from_html(mail.body.to_s) else body = mail.body.to_s end end body.gsub("\r\n", "\n") # The mail gem (>= 2.7.1) switched from \n to \r\n line breaks (LF to CRLF) in plain text mails. end private attr_reader :mail, :type def body_text_html? mail.body.to_s.include? "<html>" end def text_from_html(html) Nokogiri::HTML(html).at_css('body').text.gsub(/[\r\n]+/, "\n") end end end
Version data entries
13 entries across 13 versions & 1 rubygems