diff options
Diffstat (limited to 'vendor/gems/microsoft_graph_mailer/lib')
5 files changed, 132 insertions, 0 deletions
diff --git a/vendor/gems/microsoft_graph_mailer/lib/microsoft_graph_mailer.rb b/vendor/gems/microsoft_graph_mailer/lib/microsoft_graph_mailer.rb new file mode 100644 index 00000000000..8bd252aac94 --- /dev/null +++ b/vendor/gems/microsoft_graph_mailer/lib/microsoft_graph_mailer.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require_relative "microsoft_graph_mailer/delivery" +require_relative "microsoft_graph_mailer/railtie" if defined?(Rails::Railtie) +require_relative "microsoft_graph_mailer/version" + +module MicrosoftGraphMailer + class Error < StandardError + end + + class ConfigurationError < Error + end + + class DeliveryError < Error + end +end diff --git a/vendor/gems/microsoft_graph_mailer/lib/microsoft_graph_mailer/client.rb b/vendor/gems/microsoft_graph_mailer/lib/microsoft_graph_mailer/client.rb new file mode 100644 index 00000000000..b779bb28c39 --- /dev/null +++ b/vendor/gems/microsoft_graph_mailer/lib/microsoft_graph_mailer/client.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require "oauth2" + +module MicrosoftGraphMailer + class Client + attr_accessor :user_id, :tenant, :client_id, :client_secret, :azure_ad_endpoint, :graph_endpoint + + def initialize(user_id:, tenant:, client_id:, client_secret:, azure_ad_endpoint:, graph_endpoint:) + @user_id = user_id + @tenant = tenant + @client_id = client_id + @client_secret = client_secret + @azure_ad_endpoint = azure_ad_endpoint + @graph_endpoint = graph_endpoint + end + + def send_mail(message_in_mime_format) + # https://docs.microsoft.com/en-us/graph/api/user-sendmail + token.post( + send_mail_url, + headers: { "Content-type" => "text/plain" }, + body: Base64.encode64(message_in_mime_format) + ) + end + + private + + def token + # https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow + OAuth2::Client.new( + client_id, + client_secret, + site: azure_ad_endpoint, + token_url: "/#{tenant}/oauth2/v2.0/token" + ).client_credentials.get_token({ scope: scope }) + end + + def scope + "#{graph_endpoint}/.default" + end + + def base_url + "#{graph_endpoint}/v1.0/users/#{user_id}" + end + + def send_mail_url + "#{base_url}/sendMail" + end + end +end diff --git a/vendor/gems/microsoft_graph_mailer/lib/microsoft_graph_mailer/delivery.rb b/vendor/gems/microsoft_graph_mailer/lib/microsoft_graph_mailer/delivery.rb new file mode 100644 index 00000000000..c807ee4319e --- /dev/null +++ b/vendor/gems/microsoft_graph_mailer/lib/microsoft_graph_mailer/delivery.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require_relative "client" + +module MicrosoftGraphMailer + class Delivery + attr_reader :microsoft_graph_settings + + def initialize(microsoft_graph_settings) + @microsoft_graph_settings = microsoft_graph_settings + + [:user_id, :tenant, :client_id, :client_secret].each do |setting| + unless microsoft_graph_settings[setting] + raise MicrosoftGraphMailer::ConfigurationError, "'#{setting}' is missing" + end + end + + @microsoft_graph_settings[:azure_ad_endpoint] ||= "https://login.microsoftonline.com" + @microsoft_graph_settings[:graph_endpoint] ||= "https://graph.microsoft.com" + end + + def deliver!(message) + # https://github.com/mikel/mail/pull/872 + if message[:bcc] + previous_message_bcc_include_in_headers = message[:bcc].include_in_headers + message[:bcc].include_in_headers = true + end + + message_in_mime_format = message.encoded + + client = MicrosoftGraphMailer::Client.new( + user_id: microsoft_graph_settings[:user_id], + tenant: microsoft_graph_settings[:tenant], + client_id: microsoft_graph_settings[:client_id], + client_secret: microsoft_graph_settings[:client_secret], + azure_ad_endpoint: microsoft_graph_settings[:azure_ad_endpoint], + graph_endpoint: microsoft_graph_settings[:graph_endpoint] + ) + + response = client.send_mail(message_in_mime_format) + + raise MicrosoftGraphMailer::DeliveryError unless response.status == 202 + + response + ensure + message[:bcc].include_in_headers = previous_message_bcc_include_in_headers if message[:bcc] + end + end +end diff --git a/vendor/gems/microsoft_graph_mailer/lib/microsoft_graph_mailer/railtie.rb b/vendor/gems/microsoft_graph_mailer/lib/microsoft_graph_mailer/railtie.rb new file mode 100644 index 00000000000..607d68cc454 --- /dev/null +++ b/vendor/gems/microsoft_graph_mailer/lib/microsoft_graph_mailer/railtie.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require_relative "delivery" + +module MicrosoftGraphMailer + class Railtie < Rails::Railtie + ActiveSupport.on_load(:action_mailer) do + add_delivery_method :microsoft_graph, MicrosoftGraphMailer::Delivery + end + end +end diff --git a/vendor/gems/microsoft_graph_mailer/lib/microsoft_graph_mailer/version.rb b/vendor/gems/microsoft_graph_mailer/lib/microsoft_graph_mailer/version.rb new file mode 100644 index 00000000000..057aebdad2b --- /dev/null +++ b/vendor/gems/microsoft_graph_mailer/lib/microsoft_graph_mailer/version.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +module MicrosoftGraphMailer + VERSION = "0.1.0" +end |