# Errno::EBADF Send an Email in Rails using GMail

**URL:** <https://www.ruby-forum.com/t/errno-ebadf-send-an-email-in-rails-using-gmail/226019>\
**Category:** Rails\
**Created:** [July 24, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/errno-ebadf-send-an-email-in-rails-using-gmail/226019 "2008-07-24T21:50:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Walter\_Lockhart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba8739/32.png) [@Walter\_Lockhart](https://www.ruby-forum.com/u/Walter_Lockhart)\
**Post date:** [July 24, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/errno-ebadf-send-an-email-in-rails-using-gmail/226019/1 "2008-07-24T21:50:43Z")

</div>

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 = ‘removed\_email\_address@domain.invalid’  
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 = ‘removed\_email\_address@domain.invalid’

```
@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](http://smtp.gmail.com)”,  
:port =\> 587,  
:domain =\> “[def.com](http://def.com)”,  
:authentication =\> :plain,  
:user\_name =\> “removed\_email\_address@domain.invalid”,  
: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:in `open’  
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:in `timeout’  
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:in `do\_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:in `start’  
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:in `send’  
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:in `method\_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
