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.



19
20
21
# File 'lib/gitlab/email/receiver.rb', line 19

def initialize(raw)
  @raw = raw
end

Instance Method Details

#executeObject



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

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



64
65
66
67
68
69
70
71
# File 'lib/gitlab/email/receiver.rb', line 64

def mail
  # See https://github.com/mikel/mail/blob/641060598f8f4be14d79bad8d703e9f2967e1cdb/spec/mail/message_spec.rb#L569
  # for mail structure
  Mail::Message.new(@raw)
rescue Encoding::UndefinedConversionError,
  Encoding::InvalidByteSequenceError => e
  raise EmailUnparsableError, e
end

#mail_metadataObject



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

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),
    x_delivered_to: x_delivered_to.map(&:value),
    envelope_to: envelope_to.map(&:value),
    x_envelope_to: x_envelope_to.map(&:value),
    x_original_to: x_original_to.map(&:value),
    x_forwarded_to: x_forwarded_to.map(&:value),
    cc_address: cc,
    # 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