Send email to multiple recipients using Bamboo

Hi, as a learning exercise I am trying to build a newsletter functionality. I already know how to set up bamboo/swosh to send an email to one person (e.g. register email confirmation or contact form). My question for help is, how to send an email message to all registered users? I found in Bamboo docs how to handle multiple recipients. I tried to implement it but I got an error saying:

undefined function users/0

My newsletter.ex

defmodule Shopify.Newsletters do
  import Bamboo.Email

  users = for user <- users do
    {user.firstname, user.email}
  end

  def newsletter_email(%{"email" => email, "message" => message}) do
    new_email(to: users)
    |> from("newsletter@quickshop.com")
    |> subject("Newsletter")
    |> html_body("Message: #{message}")
    |> text_body("Message: #{message}")
  end
end

And controller:

defmodule ShopifyWeb.NewsletterController do

  use ShopifyWeb, :controller

  def index(conn, _params) do

    render(conn, "index.html")

  end

  def create(conn, params) do

    params

    |> Newsletters.users()

    |> Mailer.deliver_now()

    conn

    |> put_flash(:info, "Message sent")

    |> redirect(to: Routes.contact_path(conn, :index))

  end

Do not use that as it will leak all your recipients. Either:

  • Send messages to each receiver independently
  • Send to all but use BCC field

Otherwise you are opening yourself to GDPR violations.

I understand it, but i guess it is too complicated for me (at least for now). I will not use it outside my development environment, that’s why I tried to make it work this way. Thanks for your advice.

for user <- users do
  newsletter_email(user.email....)
  |> Mailer.deliver....
end