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+\<([^<]+)\>/
USER_ERRORS =

Errors that are purely from users and not anything we can control

[
  Gitlab::Email::AutoGeneratedEmailError, Gitlab::Email::ProjectNotFound, Gitlab::Email::EmptyEmailError,
  Gitlab::Email::UserNotFoundError, Gitlab::Email::UserBlockedError, Gitlab::Email::UserNotAuthorizedError,
  Gitlab::Email::NoteableNotFoundError, Gitlab::Email::InvalidAttachment, Gitlab::Email::InvalidRecordError,
  Gitlab::Email::EmailTooLarge
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Receiver

Returns a new instance of Receiver.



21
22
23
# File 'lib/gitlab/email/receiver.rb', line 21

def initialize(raw)
  @raw = raw
end

Instance Method Details

#executeObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gitlab/email/receiver.rb', line 25

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 *USER_ERRORS => e
  # do not send a metric event since these are purely user errors that we can't control
  raise e
rescue StandardError => e
  Gitlab::Metrics::BackgroundTransaction.current&.add_event('email_receiver_error', error: e.class.name)
  raise e
end

#mailObject



62
63
64
# File 'lib/gitlab/email/receiver.rb', line 62

def mail
  strong_memoize(:mail) { build_mail }
end

#mail_metadataObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gitlab/email/receiver.rb', line 43

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