11
9

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 5 years have passed since last update.

Rails3でメールにインラインで画像を埋め込む方法

Last updated at Posted at 2013-01-22

HTMLメールで最初から画像が入った状態で表示させたかったので試しに作ってみました。

mailer側のメソッドでメール本文中に表示させたい画像ファイルを設定。
content_id=>"rails.png"部分をメールのview側で使うので好きな名前で設定。

test_mailer.rb
class TestMailer < ActionMailer::Base
  default from: "mail@testmailer.com"
  def test_mailer
    attachments.inline["rails.png"] = {:content=> File.read("#{Rails.root.to_s}/app/assets/images/rails.png") ,:content_id=>"rails.png"}
    mail(:to=>"mail@testmailer.com",:subject=> 'New account information')
  end
end

cidに、test_mailerで設定したcontent_idを指定。これでOK。

views/test_mailer/test_mailer.html.erb
image test<br />
<img src="cid:rails.png" /><br />
11
9
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
11
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?