Errno::EBADF Send an Email in Rails using GMail

Hi,

would someone please help me. I have been desperatley trying to fnd
out for the last two days why the following code is not working. Any
ideas.

I’m running Instant Rails 1.4 / Rails version 1.2.5 / Ruby version
1.8.5 (i386-mswin32) / RubyGems version 1.1.1 / Action Mailer version
1.3.5 / Windows XP.

Here is a description of my emailing application:

in app/views/emailer/correspond.rhtml:

Request A Call Back

Email a request to receive a Call Back from XYZ:

<% form_tag :action => ‘sendmail’ do -%>

    <p>First Name:<br /><%= text_field 'email', 'firstname', :size

=> 30 %>


Surname:
<%= text_field ‘email’, ‘surname’, :size =>
30 %>


Email Address:
<%= text_field ‘email’,
‘email_address’, :size => 30 %>


Telephone Number:
<%= text_field ‘email’,
‘telephone’, :size => 30 %>


Message:
<%= text_area ‘email’, ‘message_body’, :rows
=> 20, :cols => 50 %>

<%= submit_tag "Send", :class => "submit" %> or <%= link_to

‘Cancel’, ‘/’ %>

<% end -%>

in app/controllers/emailer_controller.rb:

class EmailerController < ApplicationController

def sendmail
email = params[:email]
recipient = ‘[email protected]
subject = ‘Request A Call Back’
firstname = email[“firstname”]
surname = email[“surname”]
email_address = email[“email_address”]
telephone = email[“telephone”]
message_body = email[“message_body”]
Emailer.deliver_message(recipient, subject, firstname, surname,
email_address, telephone, message_body)
return if request.xhr?
flash[:notice] = “Email Sent Successfully.”
redirect_to ‘/emailer/correspond’
end

end

in app/models/emailer.rb:

class Emailer < ActionMailer::Base

def message(recipient, subject, firstname, surname, email_address,
telephone, message_body)
@subject = subject
@recipients = recipient
@from = ‘[email protected]

@body["firstname"] = firstname
@body["surname"] = surname
@body["email_address"] = email_address
@body["telephone"] = telephone
@body["message_body"] = message_body

end

end

in app/views/emailer/message.rhtml:

Hi XYZ!

You are receiving an email message from:

First Name:
<%= @firstname %>

Surname:
<%= @surname %>

Email address:
<%= @email_address %>

Telephone Number:
<%= @telephone %>

Message:
<%= @message_body %>

requesting a Call Back.

in config/environment.rb:

require ‘smtp_tls’
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.server_settings = {
:address => “smtp.gmail.com”,
:port => 587,
:domain => “def.com”,
:authentication => :plain,
:user_name => “[email protected]”,
:password => “abcdef”
}

in lib/smtp_tls.rb:

require “openssl”
require “net/smtp”

Net::SMTP.class_eval do
private
def do_start(helodomain, user, secret, authtype)
raise IOError, ‘SMTP session already started’ if @started
check_auth_args user, secret, authtype if user or secret

sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
@socket = Net::InternetMessageIO.new(sock)
@socket.read_timeout = 60 #@read_timeout
#@socket.debug_output = STDERR #@debug_output

check_response(critical { recv_response() })
do_helo(helodomain)

if starttls
  raise 'openssl library not installed' unless defined?(OpenSSL)
  ssl = OpenSSL::SSL::SSLSocket.new(sock)
  ssl.sync_close = true
  ssl.connect
  @socket = Net::InternetMessageIO.new(ssl)
  @socket.read_timeout = 60 #@read_timeout
  #@socket.debug_output = STDERR #@debug_output
  do_helo(helodomain)
end

authenticate user, secret, authtype if user
@started = true

ensure
unless @started
# authentication failed, cancel connection.
@socket.close if not @started and @socket and not
@socket.closed?
@socket = nil
end
end

def do_helo(helodomain)
begin
if @esmtp
ehlo helodomain
else
helo helodomain
end
rescue Net::ProtocolError
if @esmtp
@esmtp = false
@error_occured = false
retry
end
raise
end
end

def starttls
getok(‘STARTTLS’) rescue return false
return true
end

def quit
begin
getok(‘QUIT’)
rescue EOFError, OpenSSL::SSL::SSLError
end
end

end

I get the following error message:

Errno::EBADF in EmailerController#sendmail
Bad file descriptor - connect(2)
RAILS_ROOT: ./script/…/config/…

Application Trace | Framework Trace | Full Trace
C:/InstantRails/rails_apps/YOTO/vendor/plugins/action_mailer_tls/lib/
smtp_tls.rb:10:in initialize' C:/InstantRails/rails_apps/YOTO/vendor/plugins/action_mailer_tls/lib/ smtp_tls.rb:10:inopen’
C:/InstantRails/rails_apps/YOTO/vendor/plugins/action_mailer_tls/lib/
smtp_tls.rb:10:in do_start' C:/InstantRails/ruby/lib/ruby/1.8/timeout.rb:56:intimeout’
C:/InstantRails/ruby/lib/ruby/1.8/timeout.rb:76:in timeout' C:/InstantRails/rails_apps/YOTO/vendor/plugins/action_mailer_tls/lib/ smtp_tls.rb:10:indo_start’
C:/InstantRails/ruby/lib/ruby/1.8/net/smtp.rb:378:in start' C:/InstantRails/ruby/lib/ruby/1.8/net/smtp.rb:316:instart’
C:/InstantRails/rails_apps/YOTO/vendor/rails/actionmailer/lib/
action_mailer/base.rb:565:in perform_delivery_smtp' C:/InstantRails/rails_apps/YOTO/vendor/rails/actionmailer/lib/ action_mailer/base.rb:451:insend’
C:/InstantRails/rails_apps/YOTO/vendor/rails/actionmailer/lib/
action_mailer/base.rb:451:in deliver!' C:/InstantRails/rails_apps/YOTO/vendor/rails/actionmailer/lib/ action_mailer/base.rb:333:inmethod_missing’
C:/InstantRails/rails_apps/YOTO/app/controllers/emailer_controller.rb:
12:in `sendmail’

What on earth is happening?

I have a working version of this code running on Mac OS X with Rails
2.0.2 (with the obvious changes: rhtml --> html.erb and
server_settings --> smtp_settings).

Thanks in advance.

Regards

Walter

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