Class: LetterThief::Interceptor

Inherits:
Object
  • Object
show all
Defined in:
lib/letter_thief/interceptor.rb

Class Method Summary collapse

Class Method Details

.delivering_email(mail) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/letter_thief/interceptor.rb', line 3

def self.delivering_email(mail)
  string_io = StringIO.new(mail.to_s)
  email = EmailMessage.create!(
    to: mail.to,
    from: mail.from,
    sender: mail.sender,
    cc: mail.cc,
    bcc: mail.bcc,
    subject: mail.subject,
    body_text: mail.text_part&.decoded || mail.body.decoded,
    body_html: mail.html_part&.decoded,
    headers: mail.header.to_s,
    content_type: mail.content_type,
    intercepted_at: Time.current
  )

  Array(mail.attachments).each do |attachment|
    ar_attachment = email.attachments.attach(
      io: StringIO.new(attachment.body.decoded),
      filename: attachment.filename,
      content_type: attachment.mime_type
    ).last
    ar_attachment.blob.["cid"] = attachment.cid
    ar_attachment.blob.save!
  end

  email.raw_email.attach(
    io: string_io,
    filename: "message-#{email.id}.eml",
    content_type: "message/rfc822"
  )
rescue => e
  Rails.logger.error("[LetterThief] Failed to store intercepted email: #{e.message}")
end