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 · GitHub) 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.26.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 · GitHub) 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!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 8 of 8 Posts
netoum
You will need the Swoosh gmail adapter. Swoosh.Adapters.Gmail — Swoosh v1.26.2
It works great, we use in dev and production
odeac
I already use it or at least I try to do so. I mentioned in my post that
I forgot to say in my original post that I added a configuration entry for my
MySecondApp.Mailermodule that looks like this:…but the Swoosh.Adapters.Gmail documentation says that I should also provide the access_token configuration like this
If I follow the documentation and do the configuration of the Mailer module like above and then send the email without specifying an
access_tokenin the call toMailer.deliver:I’m getting the error:
My understanding is that somebody is supposed to set the system env
GMAIL_API_ACCESS_TOKENsuch thatSwoosh.Adapters.Gmailcan find it.This is why I tried to get the access token by calling
Goth.fetchbut this call fails withSo, I’m totally confused. All I want is to obtain the access token the same way as the original application did and be able to use the same Goth configuration file, which works just fine in MyFirstApp when using Bamboo.
FlyingNoodle
you are supposed to set the
GMAIL_API_ACCESS_TOKENon the system where the code is supposed to run.odeac
But I don’t have an access token. The existing application (MyFirstApp) is using goth to obtain the OAuth access token automatically. Goth is configured with a configuration file like this:
Maybe my question should not be “how to send email with Swoosh?” but instead “how to use Goth with Swoosh.Adapters.Gmail?”
FlyingNoodle
Check the code for how Bamboo gmail does it:
https://github.com/parkerduckworth/bamboo_gmail/blob/master/lib/bamboo/adapters/gmail_adapter.ex#L1
As an example you are trying to specify different scopes.
You have:
however, BambooGmail only wants the
@gmail_auth_scopewhich is"https://www.googleapis.com/auth/gmail.send".Check that code and I think you can make it work.
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
Lets add our deps and run
mix deps.getCreate a
config/config.exsCreate
lib/sample/mailer.exand 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)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 )We also need to start the app in
mix.exsPlease 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
Run
source .envandiex -S mixAt this point you shouldn’t have any log errors
To test Goth you can run
Goth.fetch!(Sample.Goth)It should return
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.
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
odeac
To be honest I don’t really know what I did wrong in my first attempt but I started from scratch and followed these steps and the email sending worked fine.
Thanks a lot for the detailed explanation!
dimitarvp
You can still do a directory-wide diff between the first variant and the working variant. Could be curious to see what’s different.