Sending emails with net/smtp

I tried the using the following code to send mail:

require ‘rubygems’
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(‘[email protected]’, ‘ayo’, ‘[email protected]’, ‘a’, ‘a’, ‘a’)

and it worked perfectly,

but when I try this:

require ‘rubygems’
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(‘smtp.gmail.com’, 587, ‘smtp.gmail.com’,
[email protected]’, ‘password’, :plain) do |smtp|
smtp.send_message msg, from, to
end
end

send_email(‘[email protected]’, ‘ayo’, ‘[email protected]’, ‘a’, ‘a’, ‘a’)

I get the following exception:

Exception `Errno::EAGAIN’ at
/home/abejide/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/protocol.rb:135

  • Resource temporarily unavailable - read would block
    Exception `Timeout::Error’ at
    /home/abejide/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/protocol.rb:140
  • Timeout::Error
    Exception `Timeout::Error’ at
    /home/abejide/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:948
  • Timeout::Error
    /home/abejide/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/protocol.rb:140:in
    rescue in rbuf_fill': Timeout::Error (Timeout::Error) from /home/abejide/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/protocol.rb:134:inrbuf_fill’
    from
    /home/abejide/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/protocol.rb:116:in
    readuntil' from /home/abejide/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/protocol.rb:126:inreadline’
    from
    /home/abejide/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:935:in
    recv_response' from /home/abejide/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:558:inblock in do_start’
    from
    /home/abejide/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:945:in
    critical' from /home/abejide/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:558:indo_start’
    from
    /home/abejide/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:525:in
    start' from /home/abejide/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/net/smtp.rb:463:instart’
    from testmail.rb:14:in send_email' from testmail.rb:19:in

I would not want to use gems.

Thanks

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs