LoginSignup
0
0

More than 1 year has passed since last update.

rails開発環境からgmailでメール送信する方法

Posted at

おおまかな流れ :sunny:

  1. gmailの2段階認証プロセスの設定とパスワード取得
  2. credentials.yml.encの設定をコマンドラインにて行う
  3. 環境設定(config/environments/development.rb)
  4. メール作成(Action Mailerのマニュアルに従う)

gmailの2段階認証プロセスの設定とパスワード取得 :hatching_chick:

Screenshot 2021-10-23 at 18.57.01.png

指示通り進め、16桁のパスワードを取得。

credentials.yml.encの設定をコマンドラインにて行う :koala:

EDITOR="vi" bin/rails credentials:edit

こちらでエディターのvimを起動。
Screenshot 2021-10-23 at 19.09.25.png

こちらの上にgmailのアドレス、先ほど取得したパスワードを入力。
<書き始め>
i => insert
<書きおわり>
esc => escape key
:wq => write and quit

gmail:
    user_name: your.account@gmail.com
    password: abcdabcdabcdabcd

環境設定(config/environments/development.rb) :house:

  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_caching = true
  config.action_mailer.default_url_options = { host: 'localhost:3000' }
  config.action_mailer.perform_deliveries = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
  address:              'smtp.gmail.com',
  port:                 587,
  domain:               'gmail.com',
  user_name:            Rails.application.credentials.gmail[:user_name],
  password:             Rails.application.credentials.gmail[:password],
  authentication:       :login}

これらを追記。

メール作成(Action Mailerのマニュアルに従う) :airplane:

公式マニュアルのAction Mailer Basicsに従う。

  • mailerをコマンドラインより作成。
  • app/mailersapplication_mailer.rb
  • app/mailers/user_mailer.rb
  • app/views/user_mailer下にてviewページhtml、textファイル作成。

実際に届いたメール :envelope:

Screenshot 2021-10-23 at 19.32.15.png

参照リンク :santa_tone1:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0