WindowsのRuby1.9.2でGmailを使ってメールを送る
使ったのはRubyInstaller 1.9.2-p290(http://rubyinstaller.org/)だけです。gemは使ってません。
参考にしたページ(http://d.hatena.ne.jp/tondol/20100103/1262490043)
kconvは使わないでnkfに丸投げで、ISO-2022-JPにするのもnkfにお願いしてみました。
最初はencode('ISO-2022-JP')を使っていたのですが、「〜」を変換するとエラーが出たのでやめました。
エラーが出たコード。
# coding: CP932 require 'kconv' '〜'.tojis # 通る '〜'.encode('ISO-2022-JP') # エラー
gmail経由でメールを送るサンプル。
# coding: CP932 require 'net/smtp' require 'time' require 'nkf' class Mail attr_accessor :to, :from, :subject, :body def encoded encoded_subject = NKF.nkf('-j --mime', @subject) header = <<HEADER_END To: #{@to} From: #{@from} Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: Text/Plain; charset=iso-2022-jp Subject: #{encoded_subject} Date: #{Time.now.rfc2822} HEADER_END NKF.nkf('-j', header + "\n" + @body).force_encoding("US-ASCII") end end mail = Mail.new mail.to = 'hoge@gmail.com' mail.from = 'hogehoge@gmail.com' mail.subject = 'タイトル' mail.body = '本文' smtp = Net::SMTP.new('smtp.gmail.com', Net::SMTP.default_ssl_port) smtp.enable_ssl smtp.start('localhost.localdomain', 'hogehoge@gmail.com', 'password') do |smtp| smtp.sendmail(mail.encoded, mail.from, mail.to) puts 'send mail.' end