ruby1.9.2でメールを送信する
rubyバージョン
$ ruby -v ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]
メール送信するクラスを作ってみた
class SendMail require 'net/smtp' def initialize(from,to,sub,server,port=25) @from = from @to = to @sub = sub @server = server @port = port end def make_header @header = "From: #{@from}\n"+ "To: #{@to}\n"+ "Subject: #{@sub}\n"+ "Date:#{Time::now}\n" "Mime-Version: 1.0\n"+ "Content-Type: text/plain; charset=UTF-8\n"+ "Content-Transfer-Encoding: 8bit\n" return @header end private :make_header def make_body @body = "" File.open('./template/hoge.txt') {|f| f.each{|line| @body +=line} } return @body end private :make_body def send @msg = make_header() + make_body() Net::SMTP.start(@server,@port) {|smtp| smtp.send_mail(@msg,@from,@to) } end end
上のクラスを使って送信する
#!/usr/bin/ruby -Ku require './class/SendMail.rb' sm = SendMail.new( "from_hoge@hoge.com", "to_hoge@hoge.com", "Title", "hoge.or.jp" ) sm.send()
ruby1.9では文字列処理の関係で怒られるみたい
/usr/lib/ruby/1.9.1/net/protocol.rb:305: warning: regexp match /.../n against to UTF-8 string
こんなふうに。
でも、気にしない。