Search code examples
ruby-on-railsgmailmailer

Mailer error Gmail Net::SMTPAuthenticationError in EnglishGradesController#create


I'm trying to send an email when a new English-Grade is created in my rails app. In this app it may be relevant that Student is an inherited (STI) type of devise User and I'm trying to send them an email when a teacher (another type of inherited devise User) creates a grade.

At the moment just trying to get it to work locally using gmail. I used

rails g mailer UserMailer

to setup.

This is the error I get when I create an English-grade.

Net::SMTPAuthenticationError in EnglishGradesController#create

530-5.5.1 Authentication Required. Learn more at

@student = Student.find_by_id(@english_grade.student_id)
  if @english_grade.save
    **UserMailer.new_grade(@student).deliver**
    redirect_to(student_path(@student), :notice => "Post was successfully created.")
  else // etc

(** is the highlighted error)

The above is also where I'm calling the mailer so I won't repeat it.

My development.rb file:

config.action_mailer.raise_delivery_errors = true

config.action_mailer.delivery_method = :smtp

config.action_mailer.default_url_options = { :host => "my ip address" }

config.action_mailer.smtp_settings = {
 :address              => "smtp.gmail.com",
 :domain => 'my ip address:3000', //where 3000 is the port I'm running on locally
 :port                 => 587,
 :user_name            => ENV['[email protected]'],
 :password             => ENV['password'],
 :authentication       => "plain",
:enable_starttls_auto => true
}

  config.action_mailer.perform_caching = false

My mailer:

class UserMailer < ApplicationMailer
default from: "[email protected]"

  def new_grade(user)
    @user = user
    mail to: @user.email, subject: "New grade created"
  end

end

From reading some of the other Qs:

In the development.rb I have tried without the 'domain:' and without having the host IP address stuff

I have tried: http://www.google.com/accounts/DisplayUnlockCaptcha

I have also allowed access for less secure apps https://www.google.com/settings/security/lesssecureapps

I have changed my password to something complicated

I also tried changing port to 465 but got an end of file error.

Also if it's of any relevance I created this gmail account today specifically to send emails for the app

Any help would be greatly appreciated, thanks!


Solution

  • The problem was actually that

     :authentication       => "plain",
    

    needed to be:

     :authentication       => "login",