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) 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

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

netoum

You will need the Swoosh gmail adapter. Swoosh.Adapters.Gmail — Swoosh v1.18.2
It works great, we use in dev and production

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement