Class: Replyr::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/replyr/email.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mail) ⇒ Email

Returns a new instance of Email.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/replyr/email.rb', line 5

def initialize(mail)
  self.mail = mail
  self.to = mail.to.first
  self.from = mail.from.first
  self.subject = mail.subject
  self.body = mail.multipart? ? mail.text_part.decoded : mail.decoded

  # Extract Attachments and store as StringIO in files
  # can later be processed by e.g. carrierwave
  #
  self.files = mail.attachments.map do |attachment|
    file = StringIO.new(attachment.decoded)
    file.class.class_eval { attr_accessor :original_filename, :content_type }
    file.original_filename = attachment.filename
    file.content_type = attachment.mime_type
    file
  end
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



3
4
5
# File 'lib/replyr/email.rb', line 3

def body
  @body
end

#filesObject

Returns the value of attribute files.



3
4
5
# File 'lib/replyr/email.rb', line 3

def files
  @files
end

#fromObject

Returns the value of attribute from.



3
4
5
# File 'lib/replyr/email.rb', line 3

def from
  @from
end

#mailObject

Returns the value of attribute mail.



3
4
5
# File 'lib/replyr/email.rb', line 3

def mail
  @mail
end

#subjectObject

Returns the value of attribute subject.



3
4
5
# File 'lib/replyr/email.rb', line 3

def subject
  @subject
end

#toObject

Returns the value of attribute to.



3
4
5
# File 'lib/replyr/email.rb', line 3

def to
  @to
end

Class Method Details

.process(mail) ⇒ Object



24
25
26
27
# File 'lib/replyr/email.rb', line 24

def self.process(mail)
  reply_mail = new(mail)
  reply_mail.process
end

Instance Method Details

#failed_permanently?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/replyr/email.rb', line 43

def failed_permanently?
  !mail.retryable? || false
end

#is_bounce_email?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/replyr/email.rb', line 39

def is_bounce_email?
  mail.bounced? || false
end

#is_reply_email?Boolean

Checks if this incoming mail is a reply email

Returns:

  • (Boolean)


31
32
33
# File 'lib/replyr/email.rb', line 31

def is_reply_email?
  to.starts_with?(Replyr.config.reply_prefix)
end

#processObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/replyr/email.rb', line 47

def process
  if is_bounce_email?
    if bounce_address = BounceAddress.new_from_address(to)
      bounce_address.model.handle_bounce(mail.final_recipient)
      true
    else
      false
    end
  elsif is_reply_email?
    if reply_address = ReplyAddress.new_from_address(to)
      reply_address.model.handle_reply(reply_address.user, stripped_body, files)
      true
    else
      false
    end
  end
end

#stripped_bodyObject



35
36
37
# File 'lib/replyr/email.rb', line 35

def stripped_body
  EmailReplyParser.parse_reply(body, from).to_s.force_encoding("UTF-8")
end