I need a module for sending emails.
Could you show me where I can find it and what's the syntax?
Thanks.
Have fun!
require 'net/smtp'
Net::SMTP.start('smtp.example.com', 25) do |smtp|
smtp.open_message_stream('from_addr', [to_addr]) do |f|
f.puts 'From: sha...@g4ef.com'
f.puts 'To: pra...@g4ef.com'
f.puts 'Subject: test message'
f.puts 'This is a test message.'
end
end
Derek
--
Posted via http://www.ruby-forum.com/.
Take a look at tmail also.
It goes without saying that you should follow the law. In the United
States ( I don't know about other countries' laws)
We have the CAN SPAM act.
Make sure you follow it, because people who spam should be
#{insert_your_patent_of_punishment}
Andrew McElroy
You can also use ActionMailer.
see below:
http://am.rubyonrails.org/
---
Ayumu AIZAWA
twitter.com/ayumin
ActionMailer uses tmail :-)
> see below:
> http://am.rubyonrails.org/
Andrew McElroy
> ---
> Ayumu AIZAWA
> twitter.com/ayumin
>
>
To add to your confusion on what to use, I'd suggest you use the Mail gem, which is TMail's successor: http://github.com/mikel/mail/
I'm betting it will become part of the Ruby standard library, but who knows. I think it should anyways.
- Ehsan
_________________________________________________________________
Your E-mail and More On-the-Go. Get Windows Live Hotmail Free.
http://clk.atdmt.com/GBL/go/171222985/direct/01/
Thanks, that works.
But from syntax point to see, what's Net::SMTP.start('smtp.example.com', 25)?
why it can accept a block as argument?
what's the content of "smtp"?
why "smtp" (it seems being taken from God) has the method of
"open_message_stream"?
and what other methods it does also have?
Sorry for my newbie questions.
Regards,
Jenn.
Because it the #start method was written that way. ;)
The reason, I imagine, is to provide some failure resistance: If a code
gets exited, the environment gets "reset" (DB connections get closed,
files written to disk, etc).
> what's the content of "smtp"?
Take a look at it with smtp.inspect in the block.
> why "smtp" (it seems being taken from God) has the method of
> "open_message_stream"?
smtp is a block variable (the | at each side tells Ruby that).
Since smtp is a block variable in a Net::SMTP block, it gains access to
the methods Net::SMTP provides to its blocks.
You could've named the block variable |s| or |richard|, and the methods
would still work.
It makes life easier if the variable's name tells you what it does when
you read the name.
> and what other methods it does also have?
smtp.methods
> Sorry for my newbie questions.
A handy tip I use when I explore something new:
You can #inspect objects or check its #methods in Ruby, and #sort the
output. A simple "puts Class.methods" shows you, in the terminal / on
the command line which methods the class Class has. "puts
Class.methods.sort" sorts the output for you (alphabetically in this case).
--
Phillip Gawlowski
Thanks Phillip.
Can you kindly tell me how to write that a method?
I know something like:
class Myclass
def myfunc a,b,c
do_something_with a,b,c
end
end
x= Myclass.new
x.myfunc(1,2,3)
but I dont know how to write a method which accepts a block (like the
above smtp one).
THanks.
Jenn.
> Thanks Phillip.
> Can you kindly tell me how to write that a method?
Not me directly, no, but I found this:
http://blog.codahale.com/2005/11/24/a-ruby-howto-writing-a-method-that-uses-code-blocks/
> I know something like:
>
> class Myclass
> def myfunc a,b,c
> do_something_with a,b,c
> end
> end
>
> x= Myclass.new
> x.myfunc(1,2,3)
>
> but I dont know how to write a method which accepts a block (like the
> above smtp one).
> THanks.
It's not much different than that.
def block
yield
end
is a simple block method already. Not terribly useful, but it's the
foundation to build from. :)
--
Phillip Gawlowski
Thanks.
I really like that!