LoginSignup
5
6

More than 5 years have passed since last update.

Ruby mailに添付ファイルをつける

Posted at

Rubyでメールを送信する時に、添付ファイルつけるやり方

require "mail"

mail = Mail.new do
  from    "from@example.co.jp"
  to      "to@example.co.jp"
  subject "subject text"
  body    File.read("body.txt")
end

# カレントディレクトリのpicture.jpgを添付
mail.add_file = "./picture.jpg"


# カレントディレクトリのpicture.jpgをphoto.jpgとして添付
mail.attachments["photo.jpg"] = File.binread("./picture.jpg")


# add_fileはnewのブロック内でも使用可能
mail = Mail.new do
  add_file "./picture.jpg"
end
5
6
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
5
6