Rubyでメール送信する例文

メール送信メソッド

def send_mail(from_mail, from_name, to, subject, body, host="localhost", port=25)

  require "nkf"
  require "net/smtp"
  if from_name == "" then from_name = from_mail.dup end

  content = <<-"EOB"
From: #{NKF.nkf("-WMm0", from_name)}<#{from_mail}>
To: #{to.to_a.join(",\n ")}
Subject: #{NKF.nkf("-WMm0", subject)}
Date: #{Time::now.strftime("%a, %d %b %Y %X %z")}
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-2022-JP
Content-Transfer-Encoding: 7bit

#{NKF.nkf("-Wjm0", body)}
  EOB

  Net::SMTP.start(host, port) do |smtp|
    smtp.send_mail content, from_mail, to
  end

end