Sending mail with devise and Gmail smtp server

23,142

If you're still having problems with this try using these settings:

require 'tlsmail'    
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
  :enable_starttls_auto => true,  
  :address            => 'smtp.gmail.com',
  :port               => 587,
  :tls                  => true,
  :domain             => 'gmail.com', #you can also use google.com
  :authentication     => :plain,
  :user_name          => '[email protected]',
  :password           => '_secret_password'
}

Additionally I would recommend putting these settings in your config/environments/development.rb file instead of environment.rb so that you can specify different mailservers for each environment.

Share:
23,142
Jatin Ganhotra
Author by

Jatin Ganhotra

Deep Learning, Knowledge Graphs and NLP

Updated on July 09, 2022

Comments

  • Jatin Ganhotra
    Jatin Ganhotra almost 2 years

    I am using Devise :confirmable and :recoverable module to confirm a user and to let him recover his password if he forgets it. Everything is going fine, the mail gets generated and I can see it in the server log, but then I am facing errors and the mail is not delivered to the mailbox. The SMTP settings for my environment.rb file is :

    require 'tlsmail'
    Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
    ActionMailer::Base.raise_delivery_errors = true
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.delivery_method = :smtp
    
    ActionMailer::Base.smtp_settings = {
      :enable_starttls_auto => true,  #this is the important shit!
      :address => 'smtp.gmail.com', #'localhost', 
      :port => 587,
      :tls => true,
      :domain => 'mail.google.com',  # mail.customdomain.com if you use google apps
      :authentication => :login,
      :user_name => '[email protected]',
      :password => '_secret_password'
    } 
    

    If the :address is 'smtp.gmail.com' , then i get the following error:

    SocketError (getaddrinfo: Name or service not known):
    

    If i set the :address to 'localhost', then i get the following error:

    Errno::ECONNREFUSED Connection refused - connect(2)
    

    I don't know what this :address means, a newbie for all this stuff. On running uname -a, i get

    Linux jatin-ubuntu 2.6.32-24-generic #38-Ubuntu SMP Mon Jul 5 09:22:14 UTC 2010 i686 GNU/Linux
    

    In my /etc/hosts file the entries are :

    127.0.0.1   localhost
    127.0.1.1   jatin-ubuntu
    
    *#74.125.93.109   smtp.gmail.com 
    #The above entry added by me*
    
    # The following lines are desirable for IPv6 capable hosts
    ::1     localhost ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    ff02::3 ip6-allhosts
    

    When I uncomment the 'smtp.gmail.com' address in the /etc/hosts file, the following error is gone :

    SocketError (getaddrinfo: Name or service not known):
    

    and now the error is :

    Errno::ECONNREFUSED Connection refused - connect(2)
    

    I don't know what is going wrong, googled for the errors and tried everything but nothing came to rescue. I do have the 'tlsmail' gem installed and the 'mail' gem as well, and my application is in development mode. Help me fix this error so that i can happily continue my rails journey and if possible guide me a little over this :address issue in the right direction so that i understand the basics of this. Thanks in advance

  • Jatin Ganhotra
    Jatin Ganhotra over 13 years
    @Rio Tera: I am a newbie to all this stuff, so couldn't understand what you are guiding me to. Could you elaborate a bit
  • riotera
    riotera over 13 years
    :domain probably should be your domain 'example.com'
  • Jatin Ganhotra
    Jatin Ganhotra over 13 years
    @Rio tera: I don't have my own domain till yet, a newbie to web development. What should i do then?? Is having your own domain a prerequisite for this?? If yes, i will purchase one asap
  • riotera
    riotera over 13 years
    please try with :domain => "gmail.com", :username => "jatinkumar.nitk", :authentication => :plain
  • Jatin Ganhotra
    Jatin Ganhotra over 13 years
    @Rio tera : tried "gmail.com" and other things as u said...didn't work
  • riotera
    riotera over 13 years
    My last chance to help you: set the :domain to your ip server.
  • Jatin Ganhotra
    Jatin Ganhotra over 13 years
    @Rio tera: Thanks for your help.Tried both machine's local network i/p address and current external (internet) address, didn't work. I am very much confused now, can you guide me to a tutorial for the basics of these things, just to get a clear idea.
  • Jatin Ganhotra
    Jatin Ganhotra over 13 years
    Thanks man...It did really work. Could you also pen down the settings if I have a domain of my own and a user-name by that domain.?
  • Jatin Ganhotra
    Jatin Ganhotra over 13 years
    @RioTera: Thanks for all your help. I really appreciate it.
  • Braden Becker
    Braden Becker over 13 years
    Just change domain to be your domain and user_name to be [email protected]... Everything else should be the same.
  • shibly
    shibly over 12 years
    With this settings, will it send email from [email protected] to any mailing address ?
  • Jared Beck
    Jared Beck over 11 years
    Apparently, :domain is optional, though you wouldn't know that from reading the docs.
  • Noz
    Noz about 11 years
    After hours of debugging the mysterious OpenSSL::SSL::SSLError error, this was the only solution that eventually did it for me!