前提・実現したい
今作っているアプリにメール送信機能を付けたいと思い
色々なサイトを参考に
まず「ユーザー登録をしたらメールが送られる」ようにしてみようとやってみました。
参考にしたサイト
https://qiita.com/hirotakasasaki/items/ec2ca5c611ed69b5e85e
https://qiita.com/annaaida/items/81d8a3f1b7ae3b52dc2b
https://www.sejuku.net/blog/48739
https://freesworder.net/rails-mail/
発生している問題・エラーメッセージ
ユーザー登録をすると
ユーザー登録は出来ていますが「メールを送る」段階で上記のエラーが出ます。
原因と解決方法が分かりません。
試したこと
調べて出て来たGmail側の設定の
二段階認証や安全性の低いアプリの設定は「オン」にしました。
「メール転送とPOP/IMAP」の所で、IMAPを有効にしています。
ソースコード
config/environments/development.rb
rails
Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. # In the development environment your application's code is reloaded on # every request. This slows down response time but is perfect for development # since you don't have to restart the web server when you make code changes. config.cache_classes = false # Do not eager load code on boot. config.eager_load = false # Show full error reports. config.consider_all_requests_local = true # Enable/disable caching. By default caching is disabled. # Run rails dev:cache to toggle caching. if Rails.root.join('tmp', 'caching-dev.txt').exist? config.action_controller.perform_caching = true config.cache_store = :memory_store config.public_file_server.headers = { 'Cache-Control' => "public, max-age=#{2.days.to_i}" } else config.action_controller.perform_caching = false config.cache_store = :null_store end # Store uploaded files on the local file system (see config/storage.yml for options) config.active_storage.service = :local # Don't care if the mailer can't send. config.action_mailer.raise_delivery_errors = true config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { port: 587, address: 'smtp.gmail.com', domain: 'smtp.gmail.com', user_name: 'gmailアドレス', password: '16桁のパスワード', authentication: 'login', enable_starttls_auto: true } config.action_mailer.perform_caching = false # Print deprecation notices to the Rails logger. config.active_support.deprecation = :log # Raise an error on page load if there are pending migrations. config.active_record.migration_error = :page_load # Highlight code that triggered database queries in logs. config.active_record.verbose_query_logs = true # Debug mode disables concatenation and preprocessing of assets. # This option may cause significant delays in view rendering with a large # number of complex assets. config.assets.debug = true # Suppress logger output for asset requests. config.assets.quiet = true # Raises error for missing translations # config.action_view.raise_on_missing_translations = true # Use an evented file watcher to asynchronously detect changes in source code, # routes, locales, etc. This feature depends on the listen gem. config.file_watcher = ActiveSupport::EventedFileUpdateChecker end
app/mailers/application_mailer.rb
rails
class ApplicationMailer < ActionMailer::Base default from: '管理者より' layout 'mailer' end
app/mailers/nishibako_mailer.rb
rails
class NishibakoMailer < ApplicationMailer def send_when_create(user) @user = user mail to: user.email, subject: 'こんにちは!新しいユーザーが追加されました' end end
app/controllers/users_controller.rb
rails
class UsersController < ApplicationController before_action :require_user_logged_in, only: [:index, :show] def index @users = User.order(id: :desc).page(params[:page]).per(25) end def new @user = User.new end def create @user = User.new(user_params) if @user.save NishibakoMailer.send_when_create(@user).deliver flash[:success] = 'ユーザーを登録しました。' redirect_to users_path else flash.now[:danger] = 'ユーザーの登録に失敗しました。' render :new end end def edit @user = User.find(params[:id]) end def update @user = User.find(params[:id]) if @user.update(user_params) flash[:success] = "正常に更新されました。" redirect_to users_path else flash.now[:danger] = "更新されませんでした。" render :edit end end def destroy @user = User.find(params[:id]) @user.destroy flash[:success] = '削除されました。' redirect_to users_url end private def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation) end end
まだ回答がついていません
会員登録して回答してみよう