2022-04-11 17:22:32 +02:00
|
|
|
ActiveSupport.on_load(:action_mailer) do
|
|
|
|
module Dolist
|
2023-04-18 16:15:47 +02:00
|
|
|
class IgnorableError < StandardError
|
|
|
|
end
|
|
|
|
|
2022-04-11 17:22:32 +02:00
|
|
|
class SMTP < ::Mail::SMTP
|
|
|
|
def deliver!(mail)
|
|
|
|
mail.from(ENV['DOLIST_NO_REPLY_EMAIL'])
|
|
|
|
mail.sender(ENV['DOLIST_NO_REPLY_EMAIL'])
|
|
|
|
mail['X-ACCOUNT-ID'] = Rails.application.secrets.dolist[:account_id]
|
|
|
|
mail['X-Dolist-Sending-Type'] = 'TransactionalService' # send even if the target is not active
|
|
|
|
|
|
|
|
super(mail)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-31 13:44:50 +01:00
|
|
|
class ApiSender
|
|
|
|
def initialize(mail); end
|
|
|
|
|
|
|
|
def deliver!(mail)
|
2023-04-03 16:25:45 +02:00
|
|
|
client = Dolist::API.new
|
|
|
|
response = client.send_email(mail)
|
2023-02-03 15:40:16 +01:00
|
|
|
if response&.dig("Result")
|
|
|
|
mail.message_id = response.dig("Result")
|
2023-01-31 13:44:50 +01:00
|
|
|
else
|
2023-04-18 16:15:47 +02:00
|
|
|
_, invalid_contact_status = client.ignorable_error?(response, mail)
|
2023-04-03 16:25:45 +02:00
|
|
|
|
2023-04-18 16:15:47 +02:00
|
|
|
if invalid_contact_status
|
|
|
|
raise Dolist::IgnorableError.new("DoList delivery error. contact unreachable: #{invalid_contact_status}")
|
2023-04-03 16:25:45 +02:00
|
|
|
else
|
|
|
|
fail "DoList delivery error. Body: #{response}"
|
|
|
|
end
|
2023-01-31 13:44:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-04-11 17:22:32 +02:00
|
|
|
|
2023-01-31 13:44:50 +01:00
|
|
|
ActionMailer::Base.add_delivery_method :dolist_smtp, Dolist::SMTP
|
|
|
|
ActionMailer::Base.dolist_smtp_settings = {
|
2022-04-11 17:22:32 +02:00
|
|
|
user_name: Rails.application.secrets.dolist[:username],
|
|
|
|
password: Rails.application.secrets.dolist[:password],
|
|
|
|
address: 'smtp.dolist.net',
|
|
|
|
port: 587,
|
|
|
|
authentication: 'plain',
|
|
|
|
enable_starttls_auto: true
|
|
|
|
}
|
2023-01-31 13:44:50 +01:00
|
|
|
|
|
|
|
ActionMailer::Base.add_delivery_method :dolist_api, Dolist::ApiSender
|
2022-04-11 17:22:32 +02:00
|
|
|
end
|