Rails Tutorial Chapter 11 Dealing with Unable to Send Emails Due to Unauthenticated Sender ID on SendGrid

Event

In "11.4 Sending an email in a production environment" of Rails tutorial, when I tried to send an email for user registration from heroku, "We're sorry, but something went wrong." Was displayed.

When I check the log with heroku logs, the following error log is displayed, Since information such as the content of the email was output, there seems to be an error in the transmission processing part by SendGrid.

Net::SMTPFatalError (550 The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements)

Investigation

Since I'm setting it as it is in the tutorial, isn't "[email protected]" set as the sender authenticated and I don't receive an email from SendGrid? I googled it, but I couldn't find a similar event in the Rails tutorial.

I tried to execute the following command from heroku run rails console, but the same error as above occurred and the mail did not arrive.

ActionMailer::Base.mail(from: "[email protected]", to: "<Receiving email address>", subject: "subject", body: "body").deliver_now

I looked at the URL shown in the log and looked it up, but it seems that SendGrid requires authentication of the sender. https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ https://sendgrid.com/docs/ui/sending-email/sender-verification/

However, "[email protected]" is not a domain created by myself, and I do not know how to authenticate with this address, so this time I decided to set a personal address as the sender and send it. ..

Correspondence

Authenticate and use your personal email address as the sending email address and as the sender with SendGrid.

Log in to SendGrid and set "Single Sender Verification" by referring to the following article. https://sendgrid.kke.co.jp/docs/Tutorials/B_Marketing_Mail/marketing_campaigns1.html

After authentication, when the following command was executed, an email arrived from the sending email address to the receiving email address.

ActionMailer::Base.mail(from: "<Email address for sending>", to: "<Receiving email address>", subject: "subject", body: "body").deliver_now

Therefore, when the processing of the mail sending part was changed as follows and the sender of the test part was also changed, the event was resolved on heroku and the mail sending worked without problems.

/sample_app/app/mailers/application_mailer.rb


class ApplicationMailer < ActionMailer::Base
  default from: "<Email address for sending>"
  layout 'mailer'
end

Personal opinion

Since it seems that the actual web service uses the email address or domain that it owns as the sender, sending by "[email protected]" that it does not own has been postponed.

However, I haven't caught up with the understanding of whether or not to send to an address I don't own, so I'll add it when I find something.

Recommended Posts

Rails Tutorial Chapter 11 Dealing with Unable to Send Emails Due to Unauthenticated Sender ID on SendGrid
Tutorial 1 for building apps with Rails (First steps to Yay! You're on Rails!)
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
Rails Tutorial Chapter 1 From Zero to Deployment [Try]