How to use Google Apps mail configuration with rails application.

I am using google apps for my domain www.bhushangahire.com

For one of my rails application I am using this domain mail account setup for sending mails.
But I am not able to send the mail with the default smtp setings we use for sending mail.

So I search on net and I found the Goggle Apps consider its in different way. Which is TLS ans SSH service, which is not by default comes with the Action mailler.

ActionMailer can’t send emails using GMail out of the box. To add this functionality do the following configuration in your rails application:

  1. Create the file 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
    
    check_response(critical { recv_response() })
    do_helo(helodomain)
    
    raise 'openssl library not installed' unless defined?(OpenSSL)
    starttls
    ssl = OpenSSL::SSL::SSLSocket.new(sock)
    ssl.sync_close = true
    ssl.connect
    @socket = Net::InternetMessageIO.new(ssl)
    @socket.read_timeout = 60 #@read_timeout
    do_helo(helodomain)
    
    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')
    end
    
    def quit
    begin
    getok('QUIT')
    rescue EOFError
    rescue OpenSSL::SSL::SSLError
    end
    end
    end
  2. Add the following lines to config/environment.rb and replace the values with the appropriate username and password:
    require 'smtp_tls'
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.raise_delivery_errors = true
    ActionMailer::Base.server_settings = {
    :address => "smtp.gmail.com",
    :port => 587,
    :domain => "mydomain.com",
    :authentication => :plain,
    :user_name => "username@mydomain.com",
    :password => "password"
    }

    *Note:To work your domain with “mydomain.com” you have to configure your MX records to work Gmail mail sending functionality out of box.

    Else you can use a temporary address @mydomain.com.test-google-a.com, I have not tested with this as I have already configure my MX records.

    You can get more information on changing http://www.google.com/support/a/bin/answer.py?answer=33352.

    Hope this information is useful to you. ;-)

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

good work buddy, keep it up
the more thing please put the hyperlink to ur url

Hey thanks, And about hyperlink, its not live as I am using it with google apps lots of things are not done yet.

Its been live very soon…. ;-)

thanks for info, much appreciated worked just fine for me.

Leave a comment

(required)

(required)