Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

send emails

0 views
Skip to first unread message

Ruby Newbee

unread,
Dec 31, 2009, 9:13:29 PM12/31/09
to
Hello,

I need a module for sending emails.
Could you show me where I can find it and what's the syntax?
Thanks.

Derek Smith

unread,
Dec 31, 2009, 10:43:50 PM12/31/09
to

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/.

andrew mcelroy

unread,
Jan 1, 2010, 12:10:29 AM1/1/10
to


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

Ayumu Aizawa

unread,
Jan 1, 2010, 12:12:14 AM1/1/10
to
Hi

You can also use ActionMailer.

see below:
http://am.rubyonrails.org/
---
Ayumu AIZAWA
twitter.com/ayumin

andrew mcelroy

unread,
Jan 1, 2010, 12:36:48 AM1/1/10
to
On Thu, Dec 31, 2009 at 11:12 PM, Ayumu Aizawa <ayumu....@gmail.com> wrote:
> Hi
>
> You can also use ActionMailer.
>

ActionMailer uses tmail :-)

> see below:
>  http://am.rubyonrails.org/

Andrew McElroy

> ---
> Ayumu AIZAWA
> twitter.com/ayumin
>
>

Ehsanul Hoque

unread,
Jan 1, 2010, 12:41:54 AM1/1/10
to

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/

Ruby Newbee

unread,
Jan 1, 2010, 1:06:04 AM1/1/10
to
On Fri, Jan 1, 2010 at 11:43 AM, Derek Smith
<derekbel...@yahoo.com> 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: sha...@g4ef.com'
>      f.puts 'To: pra...@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?
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.

Phillip Gawlowski

unread,
Jan 1, 2010, 1:16:09 AM1/1/10
to
On 01.01.2010 07:06, Ruby Newbee wrote:
> On Fri, Jan 1, 2010 at 11:43 AM, Derek Smith
> <derekbel...@yahoo.com> 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: sha...@g4ef.com'
>> f.puts 'To: pra...@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

Ruby Newbee

unread,
Jan 1, 2010, 1:26:38 AM1/1/10
to

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.

Phillip Gawlowski

unread,
Jan 1, 2010, 1:33:00 AM1/1/10
to
On 01.01.2010 07:26, Ruby Newbee wrote:

> 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

Ruby Newbee

unread,
Jan 1, 2010, 7:54:35 AM1/1/10
to
On Fri, Jan 1, 2010 at 2:33 PM, Phillip Gawlowski <p...@thimian.com> wrote:
> On 01.01.2010 07:26, Ruby Newbee wrote:
>
>> 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/
>

Thanks.
I really like that!

0 new messages