跳至内容 跳至搜索

Message-ID 按照 rfc822 的规定,应该是该电子邮件的唯一标识符。这使其成为调试和取证的理想跟踪令牌,就像 X-Request-Id 对 Web 请求所做的那样。

如果传入的电子邮件未按照 rfc822 的规定指定 Message-ID,则将使用 Mail::MessageIdField 中的方法生成一个 Message-ID。

方法
C
E
G

实例公共方法

create_and_extract_message_id!(source, **options)

从电子邮件的原始 source 中创建一个新的 InboundEmail,该电子邮件被上传为名为 raw_email 的 Active Storage 附件。在上传之前,从 source 中提取 Message-ID 并将其设置为新 InboundEmail 上的属性。

# File actionmailbox/app/models/action_mailbox/inbound_email/message_id.rb, line 16
def create_and_extract_message_id!(source, **options)
  message_checksum = OpenSSL::Digest::SHA1.hexdigest(source)
  message_id = extract_message_id(source) || generate_missing_message_id(message_checksum)

  create! raw_email: create_and_upload_raw_email!(source),
    message_id: message_id, message_checksum: message_checksum, **options
rescue ActiveRecord::RecordNotUnique
  nil
end

create_and_upload_raw_email!(source)

# File actionmailbox/app/models/action_mailbox/inbound_email/message_id.rb, line 37
def create_and_upload_raw_email!(source)
  ActiveStorage::Blob.create_and_upload! io: StringIO.new(source), filename: "message.eml", content_type: "message/rfc822",
                                         service_name: ActionMailbox.storage_service
end

extract_message_id(source)

# File actionmailbox/app/models/action_mailbox/inbound_email/message_id.rb, line 27
def extract_message_id(source)
  Mail.from_source(source).message_id rescue nil
end

generate_missing_message_id(message_checksum)

# File actionmailbox/app/models/action_mailbox/inbound_email/message_id.rb, line 31
def generate_missing_message_id(message_checksum)
  Mail::MessageIdField.new("<#{message_checksum}@#{::Socket.gethostname}.mail>").message_id.tap do |message_id|
    logger.warn "Message-ID couldn't be parsed or is missing. Generated a new Message-ID: #{message_id}"
  end
end