6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

HTMLメールで画像を載せる [rails][初心者]

Last updated at Posted at 2020-02-03

#HTMLメールに画像を載せたい

####すでにメール送信はできるけど、画像がうまく表示されない
これを記述する:point_down_tone2:

mailer.rb
attachments.inline['画像file名'] = File.read('app/assets/images/画像file名')

おそらくここの部分'app/assets/images/画像file名'
はpublic配下とかでも大丈夫だと思います。

mailer.rb

def send_when_creater(user)
    attachments.inline['vegetable.png'] = File.read('app/assets/images/vegetable.png')
    @user = user
    mail to: user.email,subject: "こんにちは"
  end
end

###mailer view
画像を表示したいところに記述しましょう:point_down_tone2:

html.erb
省略
<body>
  <%= image_tag attachments['vegetable.png'].url, alt: 'Vegetable' %>
</body>

##メール結果

こんにちは_-ryosukedapyon_gmail_com-_Gmail.png

うまく表示できました。
いろいろな記事を参考にしましたが、railsガイドを参考にしています。
もっと綺麗な方法やhelperを使ったやり方もあるみたいなので、参考にしたいと思いました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?