Running into SMTP error when trying to send email in RoR app

10,182

Solution 1

The reason that you are seeing this error is that Google has blocked your IP. You will need to enable authorization for this IP.

Go to http://www.google.com/accounts/DisplayUnlockCaptcha and click continue.

Then again try to send email from the application, it should work. You will need to send email from your app within 10 min of visiting the above link. By visiting above link, Google will grant access to register new apps for 10 min.

Solution 2

I solved by doing the following:

1) Log into the Google Apps Admin portal and enable Less Secure Apps by checking "Allow users to manage their access to less secure apps" under Security preferences.

2) Login into the account that will serve as the mailer and turn "Allow less secure apps" to ON.

Most of the answers available on-line refer to step 2, but you cannot configure at a user level without the Admin turning the feature on.

Share:
10,182
Abhas Arya
Author by

Abhas Arya

Learning Ruby on Rails. Love programming, sophomore CS student at UMD, College Park.

Updated on June 05, 2022

Comments

  • Abhas Arya
    Abhas Arya almost 2 years

    I've been desperately trying to send an email using the Action Mailer class in RoR, and even though the terminal shows that a message is being sent, it keeps returning with this error:

     et::SMTPAuthenticationError: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtOS
    
    from /Users/abhasarya/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/smtp.rb:968:in `check_auth_response'
    from /Users/abhasarya/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/smtp.rb:739:in `auth_plain'
    from /Users/abhasarya/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/smtp.rb:731:in `authenticate'...
    

    Here's what my mailer class looks like within app/mailers/my_mailer.rb:

    class MyMailer < ActionMailer::Base
      default from: "[email protected]"
    
      def welcome_email(user) 
        @user = user
    
            mail(to: user.email,
                 from: '[email protected]',
                 subject: 'Welcome!')
      end
    end
    

    And here's what my config/application.rb file looks like:

    config.action_mailer.delivery_method = :smtp
        config.action_mailer.smtp_settings = {
    
            address:              'smtp.gmail.com',
            port:                 587,
            #domain:               'gmail.com',
            user_name:            '[email protected]',
            password:             'my_password',
            authentication:       'plain',
            enable_starttls_auto: true  
    
            }
    
            config.action_mailer.default_url_options = {
                :host => "localhost",
                :port => 3000
            }
    

    in my config/environments/development.rb file I also have these two lines:

      config.action_mailer.raise_delivery_errors = true
      config.action_mailer.perform_deliveries = true
    

    My problem is that whenever I start the rails console (working in development, have no idea how to run in production), assign a user's email to the user variable then enter the command: MyMailer.welcome_email(user).deliver

    the terminal says:

    Rendered my_mailer/welcome_email.html.erb (11.7ms)
      Rendered my_mailer/welcome_email.text.erb (0.5ms)
    
    Sent mail to [email protected] (923.1ms)
    Date: Fri, 08 Aug 2014 13:54:38 -0400
    From: [email protected]
    To: [email protected]
    Message-ID: <[email protected]>
    Subject: Welcome!
    Mime-Version: 1.0
    Content-Type: multipart/alternative;
     boundary="--==_mimepart_53e50eded7355_2b0b8082dbf85068f";
     charset=UTF-8
    Content-Transfer-Encoding: 7bit
    

    but then renders the error. My views are simple enough that I didn't think I needed to include them here. I've desperately tried everything, including unlocking google captcha but still nothing works. I'd appreciate any help, all I want is for an email to get sent to the proper inbox. I'd love you forever if you can solve this issue!!!

  • Abhas Arya
    Abhas Arya almost 10 years
    Thanks, I tried it but unfortunately it still doesnt work and shows the exact same errors
  • Abhas Arya
    Abhas Arya almost 10 years
    Thank you so much Raj, I followed the link in the error log and temporarily enabled the access to less secure apps and it worked! You rock dude!
  • olucube.com
    olucube.com over 3 years
    I think it necessary to mention that "admin.google.com is used for Google Workspace accounts only. Regular Gmail accounts cannot be used to sign in to admin.google.com"