odeac

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!

First 8 of 8 Posts Switch mode

netoum

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

odeac OP

I already use it or at least I try to do so. I mentioned in my post that

I’m trying to send the emails (as explained here: Swoosh.Adapters.Gmail — Swoosh v1.26.2)

I forgot to say in my original post that I added a configuration entry for my MySecondApp.Mailer module that looks like this:

config :mysecondapp, MySecondApp.Mailer, 
    adapter: Swoosh.Adapters.Gmail

…but the Swoosh.Adapters.Gmail documentation says that I should also provide the access_token configuration like this

config :mysecondapp, MySecondApp.Mailer, 
    adapter: Swoosh.Adapters.Gmail
  access_token: {:system, "GMAIL_API_ACCESS_TOKEN"}

If I follow the documentation and do the configuration of the Mailer module like above and then send the email without specifying an access_token in the call to Mailer.deliver:

new()
|> to("test_email@example.com")
|> subject("Hello!")
|> text_body("some email body")
|> Mailer.deliver()

I’m getting the error:

2025-03-17 12:57:28.391 [error] GenServer MySecondApp.Services.EmailSender terminating
** (ArgumentError) expected [:access_token] to be set, got: [adapter: Swoosh.Adapters.Gmail, access_token: nil, otp_app: :mysecondapp]

My understanding is that somebody is supposed to set the system env GMAIL_API_ACCESS_TOKEN such that Swoosh.Adapters.Gmail can find it.

This is why I tried to get the access token by calling Goth.fetch but this call fails with

{: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"}}

So, 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

FlyingNoodle

you are supposed to set the GMAIL_API_ACCESS_TOKEN on the system where the code is supposed to run.

odeac

odeac OP

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:

{
  "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"
}

Maybe my question should not be “how to send email with Swoosh?” but instead “how to use Goth with Swoosh.Adapters.Gmail?”

FlyingNoodle

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:

scopes = [
      "https://www.googleapis.com/auth/gmail.send",
      "https://www.googleapis.com/auth/gmail.compose",
      "https://www.googleapis.com/auth/gmail.readonly"
    ]
    |> Enum.join(" ")

however, BambooGmail only wants the @gmail_auth_scope which is "https://www.googleapis.com/auth/gmail.send".

Check that code and I think you can make it work.

netoum

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

odeac

odeac OP

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

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.

— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement