[Ruby on Rails 5] Use WorkMail to set an email address for your own domain. [Work Mail]

environment

Ruby 2.5.7 Rails 5.2.4

gem gem 'dotenv-rails'

Premise

Qiita --Simple implementation of inquiry function It will be a flow to rewrite with reference to this article. We hope that you will implement and check the operation with your Gmail account in advance, and then practice the method introduced here.

The original domain has been acquired. AWS is registered.

Background

Since I got my own domain, I wanted to receive the message sent from the inquiry by the mail I got in my own domain, so I came up with this implementation. However, when I searched the net, I found that only the implementation method using Gmail as an example, so I would like to apply it this time.

procedure

  1. Get your own domain
  2. Obtain an email address with AWS WorkMail using the acquired unique domain
  3. Reflect the SMTP settings of WorkMail (IMAP / POP of the incoming mail server will not be touched this time)

The goal is that the content of the inquiry sent from the contact form on the site will be sent to the email address obtained with the original domain.

1. Get your own domain

Obtained using Google Domains. How to reflect it in your own site URL is different from the main subject, so I will omit it.

2. Obtain an email address with AWS WorkMail using the acquired unique domain

AWS's WorkMail service allows you to use your own domain, hogehoge.com, as the email address [email protected]. I referred to the following site for WorkMail settings. MISO --I tried to build a mail service of my own domain using Amazon WorkMail

If you follow the procedure and you can confirm the transmission and reception normally, it is OK.

3. Reflect the SMTP settings of WorkMail (SMTP settings)

This time, only the Rails application sends to WorkMail, so IMAP and POP, which are the receiving servers, are not set.

First, check the WorkMail setting information of here. The linked page contains two setting information, IMAP and SMTP, but this time we will refer to the SMTPS setting information at the bottom. SMTP

Next is the code. We will rewrite the reference site of the Gmail account introduced in the premise for WorkMail and introduce it.

First, change the destination.

app/mailers/contact_mailer.rb



class ContactMailer < ApplicationMailer

  ...

  def contact_mail(contact)
    @contact = contact
    mail to: "Email address obtained with your own domain", subject: "Email title"
  end

  ...

end

The part of mail to: is changed from the Gmail address to the email address obtained by the original domain. The reference site in Gmail has made it an environment variable, but I decided that it would be okay to publish the address, so I wrote it solidly. If you are uncertain, please continue to change it in the .env file.

Next is the mail settings.

config/initializers/mail_config.rb



ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
  address: 'smtp.mail.us-west-2.awsapps.com',
  domain: 'Unique domain name',
  port: 465,
  user_name: ENV['WORKMAIL_USERNAME'],
  password: ENV['WORKMAIL_PASSWORD'],
  ssl: true
}

The WorkMail protocol says SMTPS, but you can leave delivery_method as: smtp. The address depends on the region where you built your WorkMail. I chose the US West (Oregon) region, so it looks like the above. The regions where WorkMail can be built are limited, and Asia Pacific (Tokyo) is not included in them.

Correspondence table `Eastern United States (N. Virginia)` → smtp.mail.us-east-1.awsapps.com `Western United States (Oregon)` → smtp.mail.us-west-2.awsapps.com `Europe (Ireland)` → smtp.mail.eu-west-1.awsapps.com

For domain, enter the acquired original domain name (example.com) as it is. Specify 465 as the port number. Since user_name and password are information that you do not want to publish as source code, we will use the .env file. (See below.) Since there is nothing to authenticate and authentication works even if you do not specify it, delete it. In the Secure connection item of WorkMail, it says that SSL is required, so add ssl: true. In addition, it says (STARTTLS is not supported), so you don't need to enable it for auto-detection, so remove enable_starttls_auto: true.

Next, let's take a look at the .env file of the environment variables mentioned earlier.

.env



  ...

  WORKMAIL_USERNAME =Email address obtained with your own domain
  WORKMAIL_PASSWORD =Password to log in to AWS

  ...

By making these environment variables, login information will not be published on the site or on GitHub. Also, since this is a file that does not go up on GitHub, you need to write it in the same way on the .env file of the production environment. If you forget to write it in .env in the production environment, the environment variable in this mail setting will not be recognized and it will not work.

Summary

The source code I'm actually using is published on my GitHub. In addition, there may be parts that are different from the reference site from the time of Gmail account operation, so please check that as well m (_ _) m

If you have any questions, differences in interpretation, or discomfort in the description method, we would appreciate it if you could point them out in the comments.

My Twitter also publishes such techniques, impressions, and thoughts every day, so if you are interested, please take a look (´ ー `)

Thank you for reading until the end.

Reference site

For more practical usage, I have published the file I am actually using on my GitHub, so please refer to that as well! GitHub - MasaoSasaki/matchi

Other Qiita --Simple implementation of inquiry function MISO --I tried to build a mail service of my own domain using Amazon WorkMail AWS - Setting up IMAP for Amazon WorkMail

Recommended Posts