Need pointers on how to configure email notification when using phx.gen.auth

I used phx.gen.auth to setup user authentication. Now I’d like to set up email notifications. The documentation simply says “It is your responsibility to integrate with the proper system after generation”, but I don’t what steps I need to take. Could someone provide a a few bullet points that outline what needs to be done for this? FYI - I am deploying my app to fly.io. Thanks

1 Like

You need a SMTP relay to be able to send emails, I don’t think fly.io offers this. In production I would always use Amazon SES, since they have dirt cheap pricing, 0.07$ for 1000 emails.

For dev or personal use, you can use your personal email account for this, for example gmail offers this for free: Send email from a printer, scanner, or app - Google Workspace Admin Help

I assume that receiving confirmation reply emails also needs to be looked after. Wonder how this works?

This is simple, and I think it was built-in the generator the last time I used it, basically you put a token in the user confirm link that can be checked for validity on the server.

If you don’t already know about the /dev/mailbox route that is built-in, that’s worth knowing about. Phoenix simulates an email server out of the box so you can just go to the localhost:4000/dev/mailbox URL in your server whenever your server “sends” an email, and go from there. This is due to the behavior of the default Swoosh (the module that sends email) adapter, which is set to use the Local adapter.

Out of the box, phx.gen.auth does the following IIRC:

  • Adds a confirmed_at attribute for each user, which is nil by default
  • Sends a confirmation email to the user containing a confirmation URL during registration (which will, of course, confirm their email address). There is also a built-in route that can be used for the user to request a new confirmation email.
  • When that email is opened, it confirms that user by setting the user’s confirmed_at attribute to the current data

Here’s a commit I made that sets up email sending via Amazon SES (in the commit, I it configured to send in dev so I could make sure it works, you’ll probably just want to use the default Local adapter in dev most of the time so that you can just go to /dev/mailbox):

Swoosh has adapters for different services (like Amazone SES). I believe Postmark is a great email provider with a good-enough free tier (at least that’s what I recall from when I last looked into it).

2 Likes