Class: Gitlab::Email::Receiver

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/email/receiver.rb

Direct Known Subclasses

ServiceDeskReceiver

Constant Summary collapse

RECEIVED_HEADER_REGEX =
/for\s+\<(.+)\>/.freeze

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(raw) ⇒ Receiver

Returns a new instance of Receiver.


13
14
15
# File 'lib/gitlab/email/receiver.rb', line 13

def initialize(raw)
  @raw = raw
end

Instance Method Details

#executeObject


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gitlab/email/receiver.rb', line 17

def execute
  raise EmptyEmailError if @raw.blank?

  ignore_auto_reply!

  raise UnknownIncomingEmail unless handler

  handler.execute.tap do
    Gitlab::Metrics::BackgroundTransaction.current&.add_event(handler.metrics_event, handler.metrics_params)
  end
rescue StandardError => e
  Gitlab::Metrics::BackgroundTransaction.current&.add_event('email_receiver_error', error: e.class.name)
  raise e
end

#mailObject


51
52
53
# File 'lib/gitlab/email/receiver.rb', line 51

def mail
  strong_memoize(:mail) { build_mail }
end

#mail_metadataObject


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gitlab/email/receiver.rb', line 32

def 
  {
    mail_uid: mail.message_id,
    from_address: from,
    to_address: to,
    mail_key: mail_key,
    references: Array(mail.references),
    delivered_to: delivered_to.map(&:value),
    envelope_to: envelope_to.map(&:value),
    x_envelope_to: x_envelope_to.map(&:value),
    # reduced down to what looks like an email in the received headers
    received_recipients: recipients_from_received_headers,
    meta: {
      client_id: "email/#{from.first}",
      project: handler&.project&.full_path
    }
  }
end