microsoft_graph_mailer
This gem allows delivery of emails using Microsoft Graph API with OAuth 2.0 client credentials flow.
The reason for this gem
See https://gitlab.com/groups/gitlab-org/-/epics/8259.
Installation
Add this line to your application's Gemfile:
gem 'microsoft_graph_mailer'
And then execute:
bundle
Or install it yourself as:
gem install microsoft_graph_mailer
Settings
To use the Microsoft Graph API to send mails, you will need to create an application in the Azure Active Directory. See the Microsoft instructions for more details:
- Sign in to the Azure portal.
- Search for and select
Azure Active Directory. - Under
Manage, selectApp registrations>New registration. - Enter a
Namefor your application, such asMicrosoftGraphMailer. Users of your app might see this name, and you can change it later. - If
Supported account typesis listed, select the appropriate option. - Leave
Redirect URIblank. This is not needed. - Select
Register. - Under
Manage, selectCertificates & secrets. - Under
Client secrets, selectNew client secret, and enter a name. - Under
Expires, selectNever, unless you plan on updating the credentials every time it expires. - Select
Add. Record the secret value in a safe location for use in a later step. - Under
Manage, selectAPI Permissions>Add a permission. SelectMicrosoft Graph. - Select
Application permissions. - Under the
Mailnode, selectMail.Send. Then select Add permissions. - If
User.Readis listed in the permission list, you can delete this. - Click
Grant admin consentfor these permissions.
user_id- The unique identifier for the user. To use Microsoft Graph on behalf of the user.tenant- The directory tenant the application plans to operate against, in GUID or domain-name format.client_id- The application ID that's assigned to your app. You can find this information in the portal where you registered your app.client_secret- The client secret that you generated for your app in the app registration portal.
Usage
require "microsoft_graph_mailer"
microsoft_graph_mailer = MicrosoftGraphMailer::Delivery.new(
{
user_id: "YOUR-USER-ID",
tenant: "YOUR-TENANT-ID",
client_id: "YOUR-CLIENT-ID",
client_secret: "YOUR-CLIENT-SECRET-ID"
# Defaults to "https://login.microsoftonline.com".
azure_ad_endpoint: "https://login.microsoftonline.us",
# Defaults to "https://graph.microsoft.com".
graph_endpoint: "https://graph.microsoft.us"
}
)
= Mail.new do
from "[email protected]"
to "[email protected]"
subject "GitLab Mission"
html_part do
content_type "text/html; charset=UTF-8"
body "It is GitLab's mission to make it so that <strong>everyone can contribute</strong>."
end
end
microsoft_graph_mailer.deliver!()
Usage with ActionMailer
ActionMailer::Base.delivery_method = :microsoft_graph
ActionMailer::Base.microsoft_graph_settings = {
user_id: "YOUR-USER-ID",
tenant: "YOUR-TENANT-ID",
client_id: "YOUR-CLIENT-ID",
client_secret: "YOUR-CLIENT-SECRET-ID"
# Defaults to "https://login.microsoftonline.com".
azure_ad_endpoint: "https://login.microsoftonline.us",
# Defaults to "https://graph.microsoft.com".
graph_endpoint: "https://graph.microsoft.us"
}