LoginSignup
0

More than 5 years have passed since last update.

SendGridのカテゴリ名をメソッド名を基に楽に設定する

Posted at

SendGridのgemを使っている場合、以下のようなコードを書くことでメールのタイトルをダッシュボード上のカテゴリとして使える。

sendgrid_category :use_subject_lines

しかし、"Category Stats"上では日本語のカテゴリ名が表示されない。
問い合わせたところ、ASCII文字を使うことを強く推奨された。

Categories must be in 7bit encoding using the US-ASCII character set. Even though the categories are passed in your emails, there will be issues when trying to look for them in the stats page. We strongly recommend using ASCII characters for categories.

ということで、代替案としてapp/mailers/application_mailer.rbあたりに以下を書いて、メソッド名を使うと1つ1つのメーラーにカテゴリ名を書かなくてよくなり便利です。

def sendgrid_category_with_method_name
  caller_file_name = caller[0][%r{\/[^\/]+?\.rb\:}][1..-5]
  caller_method_name = caller[0][/`.*'/][1..-2]
  sendgrid_category "#{caller_file_name}:#{caller_method_name}"
end

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
0