前提・実現したいこと
Ruby on rails でSendGridを使用時に発生する TypeError: Mail is not a module
の解決
発生している問題・エラーメッセージ
TypeError: Mail is not a module
の自力解決ができない状態です。
https://github.com/sendgrid/sendgrid-ruby を参考にSendGridのAPIを用いてメール送信機能を実装しているのですが、冒頭のエラーに悩まされています...
なんでこのようなエラーが出てくるかも理解できていない状況です。
{ "status": 500, "error": "Internal Server Error", "exception": "#<TypeError: Mail is not a module>", "traces": { "Application Trace": [ { "id": 22, "trace": "app/mailers/application_mailer.rb:1:in `<main>'" }, { "id": 43, "trace": "app/mailers/user_confirm_mailer.rb:3:in `<main>'" }, { "id": 74, "trace": "app/interactors/users/register_interactor.rb:26:in `call'" }, { "id": 80, "trace": "app/controllers/api/registrations_controller.rb:4:in `create'" } ],
該当のソースコード
app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base end
app/mailers/user_confirm_mailer.rb
require 'sendgrid-ruby' include SendGrid class UserConfirmMailer < ApplicationMailer def send_user_registration_mail(user) from = SendGrid.Email.new(email: 'hoge@sample.com') subject = '登録認証メール' to = user.email template_id = 'ここにid' access_token = user.refresh_token sg_mail = SendGrid::Mail.new(from,subject,to,template_id,access_token) sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) begin response = sg.client.mail._("send").post(request_body: sg_mail.to_json) if response.status_code != 200 raise "会員登録認証メール送信エラー" end end end end
app/interactors/users/register_interactor.rb
require "bcrypt" class Users::RegisterInteractor include Hanami::Interactor include BCrypt expose :access_token def initialize(email, password) @email = email @password = password end def call # 新規登録 user = User.create!( email: @email, encrypted_password: Password.create(@password), refresh_token: SecureRandom.hex(32) ) # Jwtの発行 @access_token = ::JsonWebToken.encode(user_id: user.id, exp_date: Time.zone.now.since(2.weeks), refresh_token: user.refresh_token) # 認証メールを送信する UserConfirmMailer.send_user_registration_mail(user).deliver_now end private def valid? if User.find_by(email: @email).present? error! "そのメールアドレスは既に存在します" end if @password.empty? error! "パスワードが短すぎます" end true end end
app/controllers/api/registrations_controller.rb
class Api::RegistrationsController < ::ApplicationController # 新規登録 def create result = Users::RegisterInteractor.new(params[:email], params[:password]).call if result.failure? render json: { message: result.errors } and return end # Access_Tokenを返す render json: { access_token: result.access_token } end end
試したこと
APIのPOSTメソッドでemailとpasswoedを渡しています
https://github.com/sendgrid/sendgrid-ruby/issues/246
https://github.com/sendgrid/sendgrid-ruby/blob/master/TROUBLESHOOTING.md#rails-specifics
を参考にMail
とSendGrid::Mail
が競合している可能性は考えてみました。
しかし、そこが原因には自分には見えなくて困っています...
補足情報(FW/ツールのバージョンなど)
Rails version: 5.2.2
Ruby version: 2.5.3 (x86_64-linux)
あなたの回答
tips
プレビュー