お問い合わせフォームを作っています。
SendGridというherokuアドオンを使用してメールを送信する方法をとりました。
しかし本番環境でメール送ろうとした際にエラーが表示されます。
解決できないため質問させていただきました。
heroku config:set SENDGRID_API_KEYの設定済みです。
heroku logs下記です
herokulogs
12020-01-06T03:05:14.125875+00:00 app[web.1]: I, [2020-01-06T03:05:14.125793 #4] INFO -- : [0cb8c601-4c83-45d3-a45d-503619347e40] Completed 500 Internal Server Error in 622ms (ActiveRecord: 12.3ms) 22020-01-06T03:05:14.127790+00:00 app[web.1]: F, [2020-01-06T03:05:14.127694 #4] FATAL -- : [0cb8c601-4c83-45d3-a45d-503619347e40] 32020-01-06T03:05:14.127892+00:00 app[web.1]: F, [2020-01-06T03:05:14.127798 #4] FATAL -- : [0cb8c601-4c83-45d3-a45d-503619347e40] Net::SMTPSyntaxError (501 Recipient syntax error 42020-01-06T03:05:14.127895+00:00 app[web.1]: ): 52020-01-06T03:05:14.127980+00:00 app[web.1]: F, [2020-01-06T03:05:14.127900 #4] FATAL -- : [0cb8c601-4c83-45d3-a45d-503619347e40] 62020-01-06T03:05:14.128196+00:00 app[web.1]: F, [2020-01-06T03:05:14.127995 #4] FATAL -- : [0cb8c601-4c83-45d3-a45d-503619347e40] app/controllers/contacts_controller.rb:14:in `create' 7kobayashi-no-MacBook-Air:my-homepage MISIAN$ 8
heroku logsではSMTPSyntaxError出てるのでproduction.rbに問題があるのかな?と思いましたがイマイチどこの記載が悪いのかわかりません。
Net::SMTPSyntaxError (501 Recipient syntax error
production環境に下記追記しました。
ファイル名production.rb config.action_mailer.raise_delivery_errors = true config.action_mailer.default_url_options = { host: 'mighty-fortress-25311.herokuapp.com' } ActionMailer::Base.delivery_method = :smtp ActionMailer::Base.smtp_settings = { user_name: ENV['SENDGRID_USERNAME'], password: ENV['SENDGRID_PASSWORD'], domain: "heroku.com", address: "smtp.sendgrid.net", port: 587, authentication: :plain, enable_starttls_auto: true } config.action_mailer.perform_caching = false
$ heroku config:set MAIL=[送信先のメールアドレス]
で環境変数設定しています。
ファイル名contact_mailer.rb class ContactMailer < ApplicationMailer def contact_mail(contact) @contact = contact mail to: ENV['MAIL'], subject: "メールのタイトル" end end
ファイル名application_mailer.rb class ApplicationMailer < ActionMailer::Base default from: 'from@example.com' layout 'mailer' end
ファイル名contacts_controller.rb class ContactsController < ApplicationController def index end def new @contact = Contact.new end def create @contact = Contact.new(contact_params) if @contact.save ContactMailer.contact_mail(@contact).deliver flash[:notice] = "お問い合わせを受けました" redirect_to root_path else render :new end end private def contact_params params.require(:contact).permit(:email, :message) end end
あなたの回答
tips
プレビュー