@kyanny's blog

My thoughts, my life. Views/opinions are my own.

TMail で画像の添付ファイルつきメールを作成する

#!/usr/bin/env ruby
require 'rubygems'
gem 'tmail'
require 'tmail'
require 'base64'

mail = TMail::Mail.new
mail.to = 'yyyyyy@example.com'
mail.from = 'xxxxxx@example.com'
mail.subject = 'test'
mail.date = Time.now
mail.mime_version = '1.0'
mail.body = 'test mail'

attachment = TMail::Mail.new
attachment.body = Base64.encode64 File.open('xxx_orig.jpg', 'rb').read
attachment.set_content_type 'image', 'jpg', 'name' => 'xxx_orig.jpg'
attachment.set_content_disposition 'attachment', 'filename' => 'xxx_orig.jpg'
attachment.transfer_encoding = 'base64'

mail.parts.push attachment

File.open('xxx_mail.txt', 'w') { |f|
  f.write(mail.encoded)
}

xxx_orig.jpg がカレントディレクトリにあるものとする。

Ruby RMagick exifr TMail · GitHub