Question
Can I use Google Workspace Gmail for sending emails from a Rails app?
In the Rails guide for Action Mailer I see this example code for Gmail:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.com',
user_name: '<username>',
password: '<password>',
authentication: 'plain',
enable_starttls_auto: true,
open_timeout: 5,
read_timeout: 5
}
However it doesn’t seem to work for Google Workspace (i.e. email on a custom domain).
Does anyone know how to configure Action Mailer for Gmail on custom domain?
Answer
You can use Google Workspace Gmail for sending emails from Action Mailer in a Rails application.
Basically you can use Gmail on custom domain (e.g. support@example.com) also for transactional emails: this ensures a reliable delivery through Gmail servers / IPs.
The main disadvantage compared to solutions like Sendgrid is that you are quite limited in the number of emails that you can send (the rate limits are based on the number of user licenses that you have in your Google Workspace account).
First of all you need to allow SMTP relay in your Google Workspace account. When asked, enable TLS and SMTP authentication with username and password.
Then you may need to go to your Gmail user account, then in the security settings allow less secure apps (alternatively create an app password in case you have 2FA enabled).
Finally you can use this configuration in config/environments/production.rb
:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp-relay.gmail.com',
:port => 587,
:user_name => ENV['SMTP_USER_NAME'],
:password => ENV['SMTP_PASSWORD'],
:domain => 'example.com',
:authentication => 'plain',
:enable_starttls => true
}