harp:~ > cat a.rb
require "net/smtp"
class Email
class Error < ::StandardError; end
USER = ENV["USER"] || ENV["USERNAME"] || ENV["LOGNAME"]
attr_accessor "from"
attr_accessor "to"
attr_accessor "subject"
attr_accessor "message"
attr_accessor "sent"
alias_method "sent?", "sent"
def initialize *a, &b
options, messages = a.partition{|x| Hash === x}
opts = options.inject({}){|h,o| h.update o}
@to = opts["to"] || opts[:to] || "#{ USER }@localhost.localdomain"
@from = opts["from"] || opts[:from] || "#{ USER }@localhost.localdomain"
@subject = opts["subject"] || opts[:subject] || "[RUBY EMAIL]"
@sent = false
@message =
if messages.empty?
opts["message"] || opts[:message]
else
messages.join
end
@message = b.call(self) if b
email if @message
end
def email
raise Error, "already sent!" if @sent
msg = "To: %s\nFrom: %s\nSubject: %s\n\n%s" % [@to, @from, @subject, @message]
Net::SMTP.start("localhost"){|smtp| smtp.send_message msg, @to, @from }
@sent = true
self
end
alias_method "email!", "email"
end
EMail = Email
def email *a, &b
Email::new *a, &b
end
if $0 == __FILE__
your_address = nil
p(email("test @ #{ Time::now }", "to" => your_address, "from" => your_address))
p(email("to" => your_address, "from" => your_address){ "test @ #{ Time::now }" })
end
regards.
-a
···
On Wed, 22 Feb 2006, John N. Alegre wrote:
I want to send some mail using ruby as the CGI.
I have no trouble creating the parsing code.
What I need is a quick easy way to send the mail.
Does the overhead of using ActionMailer under Rails work here. The mail is
a real simple form with text info, no graphics, nothing fancy. Is there
anything in stock Ruby CGI that will send smtp mail? Can someone point me
to some example code?
Thanks
john
--
judge your success by what you had to give up in order to get it.
- h.h. the 14th dali lama