Module: Rfc822Util
- Defined in:
- lib/rfc822_util.rb
Class Method Summary collapse
-
.discard_mail_body(content) ⇒ Object
discard everything after the first nn , i.e.
-
.extract_journalled_mail(mail, strip_content = true) ⇒ Object
if an X-MS-Journal-Report header is present, then extract the first message/rfc822 attachment from the RFC822 encoded content, and return it as a TMail::Mail.
- .logger ⇒ Object
- .logger=(l) ⇒ Object
-
.mail_to_hash(mail, strip_content = true) ⇒ Object
turn a TMail::Mail into a hash suitable for JSON representation if strip_content is true then neither subject nor body will be present.
-
.parse_address(address) ⇒ Object
parse an address to a hash.
-
.parse_addresses(addresses) ⇒ Object
parse one or more addresses to hash.
-
.strip_header(header) ⇒ Object
remove angle brackets from a header string.
-
.strip_headers(headers) ⇒ Object
remove angle brackets from one or more headers.
- .with_headers(headers) ⇒ Object
Class Method Details
.discard_mail_body(content) ⇒ Object
discard everything after the first nn , i.e. all message body content from an RFC822 encoded mail
39 40 41 |
# File 'lib/rfc822_util.rb', line 39 def discard_mail_body(content) content.gsub(/^(.*?)\r\n\r\n.*$/m, '\1') end |
.extract_journalled_mail(mail, strip_content = true) ⇒ Object
if an X-MS-Journal-Report header is present, then extract the first message/rfc822 attachment from the RFC822 encoded content, and return it as a TMail::Mail. if no X-MS-Journal-Report header is present then return the whole mail. if strip_content
is true then message content will be discarded, and only headers processed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rfc822_util.rb', line 22 def extract_journalled_mail(mail, strip_content=true) journal_mail = TMail::Mail.parse(mail) if mail.is_a?(String) return journal_mail if !journal_mail['X-MS-Journal-Report'] # it's not really a journal mail # get the attachment = journal_mail.parts.select{ |p| p.content_disposition == "attachment" || p.content_type == "message/rfc822" }.first # complain if the email has no attachment to extract raise "attempted to extract journalled mail, but message has no attachments: \n#{mail}\n\n" unless mail_content = strip_content ? discard_mail_body(.body) : .body TMail::Mail.parse(mail_content) end |
.logger ⇒ Object
11 12 13 |
# File 'lib/rfc822_util.rb', line 11 def logger @logger ||= Logger.new($stderr) end |
.logger=(l) ⇒ Object
7 8 9 |
# File 'lib/rfc822_util.rb', line 7 def logger=(l) @logger=l end |
.mail_to_hash(mail, strip_content = true) ⇒ Object
turn a TMail::Mail into a hash suitable for JSON representation if strip_content is true then neither subject nor body will be present
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rfc822_util.rb', line 78 def mail_to_hash(mail, strip_content=true) mail = TMail::Mail.parse(mail) if mail.is_a?(String) = strip_header(mail.) if mail. sent_at = mail.date.xmlschema in_reply_to = strip_headers(mail.in_reply_to).first if mail.in_reply_to references = strip_headers(mail.references) if mail.references from = parse_addresses(mail.from_addrs).first sender = parse_addresses(mail.sender).first to = parse_addresses(mail.to_addrs) cc=parse_addresses(mail.cc_addrs) bcc=parse_addresses(mail.bcc_addrs) h = { :message_id=>, :sent_at=>sent_at, :in_reply_to=>in_reply_to, :references=>references, :from=>from, :sender=>sender, :to=>to, :cc=>cc, :bcc=>bcc } if !strip_content h[:subject] = mail.subject end h end |
.parse_address(address) ⇒ Object
parse an address to a hash
58 59 60 61 |
# File 'lib/rfc822_util.rb', line 58 def parse_address(address) address = TMail::Address.parse(address) if address.is_a?(String) {:name=>address.name, :email_address=>address.address} end |
.parse_addresses(addresses) ⇒ Object
parse one or more addresses to hash. failures result in a warning logged
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rfc822_util.rb', line 64 def parse_addresses(addresses) [*(addresses||[])].map do |a| begin parse_address(a) rescue Exception=>e logger.warn("failure parsing: #{a}") logger.warn(e) nil end end.compact end |
.strip_header(header) ⇒ Object
remove angle brackets from a header string
44 45 46 |
# File 'lib/rfc822_util.rb', line 44 def strip_header(header) header.gsub(/^<(.*)>$/, '\1') end |
.strip_headers(headers) ⇒ Object
remove angle brackets from one or more headers
53 54 55 |
# File 'lib/rfc822_util.rb', line 53 def strip_headers(headers) with_headers(headers){|h| strip_header(h)} end |
.with_headers(headers) ⇒ Object
48 49 50 |
# File 'lib/rfc822_util.rb', line 48 def with_headers(headers) [*(headers||[])].map{|h| yield h} end |