Files
danbooru/app/mailers/application_mailer.rb
evazion b94cb7d824 emails: include logging information in email headers.
Log the following information in email headers:

* X-Danbooru-User: the user's name and ID.
* X-Danbooru-IP: the user's IP.
* X-Danbooru-Session: the users' session ID.
* X-Danbooru-URL: the page that triggered the email.
* X-Danbooru-Job-Id: the ID of the background job that sent the email.
* X-Danbooru-Enqueued-At: when the email was queued as a background job.
* X-Danbooru-Dmail: for Dmail notifications, the link to the Dmail.
* X-Request-Id: the request ID of the HTTP request that triggered the email.

Also make it so we log an event in the APM when we send an email.
2022-09-29 04:36:11 -05:00

50 lines
1.9 KiB
Ruby

# frozen_string_literal: true
# The base class for emails sent by Danbooru.
#
# @see https://guides.rubyonrails.org/action_mailer_basics.html
# @see app/logical/email_interceptor.rb
class ApplicationMailer < ActionMailer::Base
helper :application
helper :users
include UsersHelper
default from: "#{Danbooru.config.canonical_app_name} <#{Danbooru.config.contact_email}>", content_type: "text/html"
default "Message-ID": -> { "<#{SecureRandom.uuid}@#{Danbooru.config.hostname}>" }
def mail(user, require_verified_email:, **options)
# https://www.rfc-editor.org/rfc/rfc8058#section-3.1
#
# A mail receiver can do a one-click unsubscription by performing an HTTPS POST to the HTTPS URI in the
# List-Unsubscribe header. It sends the key/value pair in the List-Unsubscribe-Post header as the request body.
# The List-Unsubscribe-Post header MUST contain the single key/value pair "List-Unsubscribe=One-Click".
headers["List-Unsubscribe"] = "<#{disable_email_notifications_url(user)}>"
headers["List-Unsubscribe-Post"] = "List-Unsubscribe=One-Click"
headers["X-Danbooru-User"] = "#{user.name} <#{user_url(user)}>"
if params.to_h[:request]
headers["X-Danbooru-URL"] = params[:request][:url]
headers["X-Danbooru-IP"] = params[:request][:remote_ip]
headers["X-Danbooru-Session"] = params[:request][:session_id]
headers["X-Request-Id"] = params[:request][:request_id]
end
headers(params.to_h[:headers].to_h)
message = super(to: user.email_address&.address, **options)
message.perform_deliveries = user.can_receive_email?(require_verified_email: require_verified_email)
message
end
def self.with_request(request)
with(
request: {
url: "#{request.method} #{request.url}",
remote_ip: request.remote_ip.to_s,
request_id: request.request_id.to_s,
session_id: request.session.id.to_s,
}
)
end
end