LoginSignup
2
1

More than 3 years have passed since last update.

rails turorial 11章あたりでハマるだろうこと

Posted at

送信するメールがエンコードされててリンクを踏めない

サインアップ時、nameに日本語を入れる

スクリーンショット 2020-07-29 18.53.57.png

railsのログをみてみると

スクリーンショット 2020-07-29 18.52.59.png

http://localhost:3000/account_activations/AOSiSUDhma-B1bwZe3Wd_g/edit?email=3Dhogehoge%40gmail.com=0D

クエリのemailに謎の"3D"がついている。
その他所々で謎の0Dや3Dが、、、

ログからこのリンクを踏もうとすると

User.find_by(email: '3Dhogehoge@gmail.com')

を探査してしまいrootに飛ばされる。

quoted-printable

スクリーンショット 2020-07-29 18.57.10.png

Content-Transfer-Encoding: quoted-printableが原因で
=が=3Dに変換される。

メール本文に日本語があると、7bit文字に変換できないため、
Content-Transfer-Encoding: quoted-printableが登場する。
Content-Transfer-Encoding: quoted-printabl
は8bit文字を7bit文字に変換してエンコードする。

What does 3D mean in this html email?
quoted-printable

quoted-printablがデコードされる前のurlを踏んでいるので、正しいリンクに飛ばない。

解決策

本番環境ではちゃんとデコードされたリンクを踏むので、解決策とまでは言わないが一応

mail-iso-2022-jpを使う


group :development, :test do
       ・
       ・
       ・
  gem 'mail-iso-2022-jp'
end

bundle installして

application_mailer.rb
class ApplicationMailer < ActionMailer::Base
  default from: 'noreply@example.com',
  charset: 'ISO-2022-JP'
  layout 'mailer'
end

最後に

本番環境ではちゃんとデコードしてくれるので、charset指定は変えずにokです。

参考
日本語メールの仕組み

日々精進

2
1
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
2
1