[Ruby] Sending mails in ruby

To Send emails In ruby
I have followed the steps from Ian Purthon
It goes..

skumar@nomad:~$ cat /tmp/email1.rb
require ‘net/smtp’

def send_email(from, from_alias, to, to_alias, subject, message)
msg = <<END_OF_MESSAGE
From: #{from_alias} <#{from}>
To: #{to_alias} <#{to}>
Subject: #{subject}
#{message}
END_OF_MESSAGE
        Net::SMTP.start(‘localhost’) do |smtp|
                smtp.send_message msg, from, to
                end
end

send_email(‘skumar@spikesource.com’,’skumar’,’skumar@mailbucket.org’,’skumar’,’Send email from ruby’, ‘This is a test mail’)
skumar@nomad:~$

If you get some errors like,

skumar@nomad:~$ ruby /tmp/email.rb
/usr/lib/ruby/1.8/net/protocol.rb:206:in `initialize’: Connection refused – connect(2) (Errno::ECONNREFUSED)
        from /usr/lib/ruby/1.8/net/protocol.rb:206:in `new’
        from /usr/lib/ruby/1.8/net/protocol.rb:206:in `old_open’
        from /usr/lib/ruby/1.8/timeout.rb:56:in `timeout’
        from /usr/lib/ruby/1.8/timeout.rb:76:in `timeout’
        from /usr/lib/ruby/1.8/net/protocol.rb:206:in `old_open’
        from /usr/lib/ruby/1.8/net/smtp.rb:393:in `do_start’
        from /usr/lib/ruby/1.8/net/smtp.rb:378:in `start’
        from /usr/lib/ruby/1.8/net/smtp.rb:316:in `start’
        from /tmp/email.rb:11
skumar@nomad:~$

means, We need a smtp server installed in the local machine. I had postfix server installed.

Blogged with Flock

Tags:

email1.rb

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s