send emails

R

Ruby Newbee

Hello,

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

Derek Smith

Ruby said:
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: (e-mail address removed)'
f.puts 'To: (e-mail address removed)'
f.puts 'Subject: test message'
f.puts 'This is a test message.'
end
end

Derek
 
A

andrew mcelroy

Ruby said:
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'
=A0Net::SMTP.start('smtp.example.com', 25) do |smtp|
=A0 =A0smtp.open_message_stream('from_addr', [to_addr]) do |f|
=A0 =A0 =A0f.puts 'From: (e-mail address removed)'
=A0 =A0 =A0f.puts 'To: (e-mail address removed)'
=A0 =A0 =A0f.puts 'Subject: test message'
=A0 =A0 =A0f.puts 'This is a test message.'
=A0 =A0end
=A0end

Derek


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
 
E

Ehsanul Hoque

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

To add to your confusion on what to use=2C I'd suggest you use the Mail gem=
=2C which is TMail's successor: http://github.com/mikel/mail/



I'm betting it will become part of the Ruby standard library=2C but who kn=
ows. I think it should anyways.


- Ehsan


=20
_________________________________________________________________
Your E-mail and More On-the-Go. Get Windows Live Hotmail Free.
http://clk.atdmt.com/GBL/go/171222985/direct/01/=
 
R

Ruby Newbee

Ruby said:
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'
=C2=A0Net::SMTP.start('smtp.example.com', 25) do |smtp|
=C2=A0 =C2=A0smtp.open_message_stream('from_addr', [to_addr]) do |f|
=C2=A0 =C2=A0 =C2=A0f.puts 'From: (e-mail address removed)'
=C2=A0 =C2=A0 =C2=A0f.puts 'To: (e-mail address removed)'
=C2=A0 =C2=A0 =C2=A0f.puts 'Subject: test message'
=C2=A0 =C2=A0 =C2=A0f.puts 'This is a test message.'
=C2=A0 =C2=A0end
=C2=A0end

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

Phillip Gawlowski

Ruby said:
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: (e-mail address removed)'
f.puts 'To: (e-mail address removed)'
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).
 
R

Ruby Newbee

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'
=C2=A0Net::SMTP.start('smtp.example.com', 25) do |smtp|
=C2=A0 =C2=A0smtp.open_message_stream('from_addr', [to_addr]) do |f|
=C2=A0 =C2=A0 =C2=A0f.puts 'From: (e-mail address removed)'
=C2=A0 =C2=A0 =C2=A0f.puts 'To: (e-mail address removed)'
=C2=A0 =C2=A0 =C2=A0f.puts 'Subject: test message'
=C2=A0 =C2=A0 =C2=A0f.puts 'This is a test message.'
=C2=A0 =C2=A0end
=C2=A0end

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 g= ets
exited, the environment gets "reset" (DB connections get closed, files
written to disk, etc).

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=3D 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.
 
P

Phillip Gawlowski

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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,428
Latest member
RosalieQui

Latest Threads

Top