Today I'll show you how to send email with Ruby:
1 require 'net/smtp'
2 SMTP_HOST = "192.168.60.99"
3
4 def send(from, to, subject, msg)
5 mail = "To: #{to}\r\n" +
6 "From: #{from}\r\n" +
7 "Subject: #{subject}\r\n" +
8 "\r\n" +
9 msg
10
11 Net::SMTP.start(SMTP_HOST) do |smtp|
12 smtp.send_mail(mail, from, to)
13 end
14 end
15
16 from = "blake@192.168.60.99"
17 to = ["blake@192.168.60.99"]
18 send(from, to, "test", "Just a test!\ntest")
Notice:
1. 'mail' is the email body, it use "\r\n" to separate the mail header and mail context. So if you only want to send context without header, you should write: "mail = "\r\n" + msg". Otherwise your email will lost the context.
Technorati Tags: Ruby