From: Phillip Gawlowski Date: 2010-01-01T15:16:09+09:00 Subject: Re: send emails On 01.01.2010 07:06, Ruby Newbee wrote: > On Fri, Jan 1, 2010 at 11:43 AM, Derek Smith > wrote: >> Ruby Newbee wrote: >>> Hello, >>> >>> 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: shabbir@g4ef.com' >> f.puts 'To: pradeep@g4ef.com' >> f.puts 'Subject: test message' >> f.puts 'This is a test message.' >> end >> end >> > > 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? 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