odeac
How to send emails with Swoosh and Gmail API?
I’m not that experienced with Elixir and I’m totally new to Gmail API. Also the post is pretty long but I tried to give as much info as possible.
My situation is the following: I have an Elixir application (MyFirstApp), written by a previous developer, and it works fine and it successfully sends emails via Gmail API using Bamboo (). Now I need to add email sending logic to another Elixir application (MySecondApp) and I’m trying to understand how to migrate the email sending code from the first to the second application.
The way I understand how the email works with MyFirstApp is this:
In mix.exs Bamboo and Bamboo gmail adapter are listed as dependencies
{:bamboo, "~> 1.3"},
{:bamboo_gmail, "0.2.0"},
…and bamboo app is listed as an “extra_application”
def application do
[
extra_applications: [
...
:bamboo,
...
],
...
]
end
There is a Mailer module which uses Bamboo:
defmodule MyFirstApp.Mailer do
use Bamboo.Mailer, otp_app: :myfirstapp
end
There is a goth configuration that looks like this:
config :goth,
json: "priv/email/service.json" |> File.read!()
…and the above goth.json file looks like this:
{
"type": "service_account",
"project_id": ***,
"private_key_id": ***,
"private_key": ***,
"client_email": ***,
"client_id": ***,
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": ***,
"universe_domain": "googleapis.com"
}
…and then the emails are sent using MyFirstApp.Mailer.deliver_now.
My understanding is that Bamboo starts :goth app, which does all the Oauth work and provides the access token to the Bamboo Gmail adapter, the email gets sent and everybody is happy.
Please, correct me if I’m wrong!
Now I have to add email sending logic that uses exactly the same configuration to a new application, MySecondApp. The problem is that this new application is built with a different set of libraries and if I add Bamboo as a dependency I’m getting some library conflicts because of the Bamboo Gmail adapter version depending on an older version of some library (I don’t remember exactly which one). Please note that Bamboo Gmail Adapter doesn’t seem to be actively maintained and the last commit was 4 years ago. For this reason I decided to migrate the email sending code to use Swoosh so I did the following:
In mix.exs I listed Swoosh ( GitHub - swoosh/swoosh: Compose, deliver and test your emails easily in Elixir) as a dependency:
{:swoosh, "~> 1.17"}
I defined the mailer module:
defmodule MySecondApp.Mailer do
use Swoosh.Mailer, otp_app: :mysecondapp
end
…and I’m trying to send the emails (as explained here: Swoosh.Adapters.Gmail — Swoosh v1.18.2)
new()
|> to("test_email@example.com")
|> subject("Hello!")
|> text_body("some email body")
|> Mailer.deliver(access_token: gmail_access_token)
Now I guess I should obtain the gmail_access_token. Remember that I have a configuration file that works fine with Bamboo (see goth.json above) and I need to use the same account so I guess I should use Goth (GitHub - peburrows/goth: Elixir package for Oauth authentication via Google Cloud APIs) in MySecondApp and I add goth to my deps:
{:goth, "~> 1.4"}
…and start Goth in my supervisor:
scopes = [
"https://www.googleapis.com/auth/gmail.send",
"https://www.googleapis.com/auth/gmail.compose",
"https://www.googleapis.com/auth/gmail.readonly"
]
|> Enum.join(" ")
credentials = "/path/to/goth.json" |> File.read!() |> Jason.decode!()
source =
{
:service_account,
credentials,
[
claims: %{
"sub" => ***,
"scope" => scopes
}
]
}
[
...
{Goth, name: MySecondApp.Goth, source: source}
]
|> Supervisor.start_link([strategy: :one_for_one, name: MySecondApp.Supervisor])
…and then obtain the token like this, in order to use it in the above:
{:ok, gmail_access_token} = Goth.fetch(MySecondApp.Goth)
…but when I do that I get the following error:
{:error, %RuntimeError{message: "unexpected status 401 from Google\n\n{\n \"error\": \"unauthorized_client\",\n \"error_description\": \"Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.\"\n}\n"}}
The credentials are certainly fine because I’m using exactly the same goth.json from MyFirstApp so I don’t understand what’s the difference between the way I’m using the Goth configuration in the two applications.
Now I’m completely stuck and I would highly appreciate any pointers.
Thanks a lot for your help!
Marked As Solved
netoum
Hi @odeac , sorry for our previous reply, we just realized that the “service account” integration is different.
Here is a step by step instruction on how to Send email using Swoosh, Swoosh Gmail Adapter and Goth with the “service account”
Please note that a Google Workspace account is required for it to work. For a personal Gmail account, you must use the “refresh_token” integration
First lets create a new mix app, let’s call it Sample, as it is done in Swoosh documentation
mix new sample
Lets add our deps and run mix deps.get
{:swoosh, "~> 1.18"},
{:hackney, "~> 1.23"},
{:mail, "~> 0.4.3"},
{:goth, "~> 1.4"}
Create a config/config.exs
import Config
config :sample, Sample.Mailer,
adapter: Swoosh.Adapters.Gmail
Create lib/sample/mailer.ex
defmodule Sample.Mailer do
use Swoosh.Mailer, otp_app: :sample
end
and last for Swoosh a lib/sample/user_email.ex ( change your email and name to the the email indicated in the “sub” option in your Application (see below)
defmodule Sample.UserEmail do
import Swoosh.Email
def welcome(user) do
new()
|> to({user.name, user.email})
|> from({"Your Name", "your_email@domain.com"})
|> subject("Hello, Avengers!")
|> html_body("<h1>Hello #{user.name}</h1>")
|> text_body("Hello #{user.name}\n")
end
end
For Goth to work we must create an lib/sample/application.ex ( change the “sub” email to one of the email connected to your Workspace account )
defmodule Sample.Application do
use Application
def start(_type, _args) do
credentials =
"GOOGLE_APPLICATION_CREDENTIALS_JSON"
|> System.fetch_env!()
|> Jason.decode!()
source =
{
:service_account,
credentials,
[
claims: %{
"sub" => "sending_email@domain.com",
"scope" => "https://www.googleapis.com/auth/gmail.compose"
}
]
}
children = [
{Goth, name: Sample.Goth, source: source}
]
Supervisor.start_link(children, strategy: :one_for_one)
end
end
We also need to start the app in mix.exs
def application do
[
extra_applications: [:logger],
mod: {Sample.Application, []}
]
end
Please note that in this example we use env variable are we dont feel confortable putting the credentials into a json file, but feel free to change this.
Create an `.env’ at the root of the project ( make sure also that it is added to your .gitignore
export GOOGLE_APPLICATION_CREDENTIALS_JSON='{
"type": "service_account",
"project_id": "****",
"private_key_id": "***",
"private_key": "***",
"client_email": "***.iam.gserviceaccount.com",
"client_id": "***",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "***",
"universe_domain": "googleapis.com"
}';
Run source .env and iex -S mix
At this point you shouldn’t have any log errors
To test Goth you can run Goth.fetch!(Sample.Goth)
It should return
%Goth.Token{
token: "****",
type: "Bearer",
scope: "https://www.googleapis.com/auth/gmail.compose",
sub: "sending_email@domain.com",
expires: ***,
account: nil
}
Now to send the email. Please note that we dont define the access token in the configuration but as an argument to the deliver function.
# In an IEx session
email = Sample.UserEmail.welcome(%{name: "Tony Stark", email: "tony.stark@example.com"})
Sample.Mailer.deliver(email, access_token: Goth.fetch!(Sample.Goth).token)
Regarding the Google account setup , you should be all set as you were previously using it but for others there is couple of things to setup
-Have a workspace account
-Enable Gmail API
-Create a Service Account
-Add compose scope to the ID of the service agent in Domain Wide Delegation ( only https://www.googleapis.com/auth/gmail.compose is required )
https://admin.google.com/ac/owl/domainwidedelegation?hl=en_US
Voila ! I hope this helps you and others. Feel free to ask questions if any
Also Liked
netoum
You will need the Swoosh gmail adapter. Swoosh.Adapters.Gmail — Swoosh v1.18.2
It works great, we use in dev and production







