Simple mail send ... CGI or ActionMailer?


#1

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


#2

On Feb 21, 2006, at 4:43 PM, John N. Alegre wrote:

anything in stock Ruby CGI that will send smtp mail? Can someone
point me
to some example code?

Thanks
john

You don’t have to use the whole Rails stack to make use of ActionMailer.
As far as using something in stock ruby:

http://www.ruby-doc.org/stdlib/libdoc/net/smtp/rdoc/classes/Net/
SMTP.html


#3

On Wed, 22 Feb 2006, John N. Alegre wrote:

Thanks
john

 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


#4

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.

I have a script that checks my library for DVDs, and mails my cell phone
when found. Here’s the mailing code:

require ‘net/smtp’

def sa2( search, url )
send_to_domain = “some-domain.com
send_to = “removed_email_address@domain.invalid”
send_from_domain = “from-domain.com
send_from = “removed_email_address@domain.invalid”

msgstr = "From: Library Checker <#{send_from}>

To: #{send_to} <#{send_to}>
Subject: DVD search notice : #{search} #{Time.new.to_s}
Date: #{Time.new().to_s}
Message-Id: <#{Time.new().to_i}@#{send_from_domain}>

This is a library search alert , sent on #{Time.new().to_s}.\n\n#{msg}"

Net::SMTP.start( “#{send_to_domain}”, 25, send_from_domain ) { |smtp|
smtp.send_message( msgstr, send_from, send_to )
}

end

the end

Sloppy but functional. Beats Blockbuster.


James B.

?Design depends largely on constraints.?
? Charles Eames


#5

David,

Excuse my ignorance.

What is an RAA?

john


#6

DÅ?a Utorok 21 Február 2006 22:43 John N. Alegre napísal:

Thanks
john

You can also use the TMail or RubyMail or whichever else mail packages
there
out there to make you the mail body of arbitrary fanciness if you don’t
feel
like making the mail string yourself. Check the RAA.

David V.


#7

Again a followup question.

Am I right here …

RubyMail and TMail would give me methods to parse my text into a mail
object
but then I would then still use net/smtp to send that mail object out as
email? Are the mail objects produced by RubyMail or TMail compatible
with
sending with net/smtp?

Thanks for the continued attention.
john


#8

DÅ?a Streda 22 Február 2006 16:23 John N. Alegre napísal:

Again a followup question.

Am I right here …

RubyMail and TMail would give me methods to parse my text into a mail
object but then I would then still use net/smtp to send that mail object
out as email? Are the mail objects produced by RubyMail or TMail
compatible with sending with net/smtp?

You don’t need to remember the SMTP format with the headers and stuff,
that’s
all. It’s a slightly less hackish way of going on about things, and as a
bonus, you’ll know how to use it if you ever wish to send multipart
messages
which are arguably more difficult to encode by hand.

You can do all manners of things with the TMail and RubyMail objects,
but you
probably want to call #to_s on those, which gives you the message
prepared
for sending with SMTP.

David V.


#9

David never mind my question on this. I found it.

James and Logan … Thanks for your answers.

Just one brief followup question. Can any one who has used them discuss
the
advantages of TMail or RubyMail vs simple “rolling your own”. Note that
in
my case the parsing code to pull the mail out of the form is done. All
I
am looking for is an easy way to put that into a email form and send it
via
a local SMTP host. Note that the host is not the same machine as the
CGI
but is reachable on the LAN.

James’s sample code looks like it might work, I have not worked with it
yet.
What gains would i get with the TMail or RubyMail packages?

Thanks for the continued help.
john