2
votes

I'm trying to setup email in my rails application and i'm getting the error "Net::SMTPAuthenticationError (535 Authentication failed: Bad username / password )" when i trigger the mail action in production environment.

controller:

class FeedbacksController < InheritedResources::Base

    def create
        @feedback = Feedback.new(feedback_params)
        @user = current_user
         respond_to do |format|
            if @feedback.save
              ExampleMailer.sample_email(@user).deliver

              format.html { redirect_to @feedback, notice: 'feedback was successfully created.' }
              format.json { render :show, status: :created, location: @user }
            else
              format.html { render :new }
            end
        end
    end
end

config/environments/production.rb:

 config.action_mailer.default_url_options = { :host => 'myurl.com:4321' }
  config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
   :address              => "smtp.gmail.com",
   :port                 => 587,
   :domain               => "myurl.com:4321",
   :user_name            => "[email protected]",
   :password             => "mypassword",
   :authentication       => "plain",
   :enable_starttls_auto => true
  }

example_mailer.rb:

class ExampleMailer < ActionMailer::Base
  default from: "[email protected]
  def sample_email(user)
    @user = user
    mail(to: @user.email, subject: 'sample email')
  end
end
1

1 Answers

1
votes

In your mailer, there is typo:

class ExampleMailer < ActionMailer::Base
  default from: "[email protected]"
  def sample_email(user)
    @user = user
    mail(to: @user.email, subject: 'sample email')
  end
end

And in your production.rb, you can write username as myemail. That's fine.

Let me know if its not work..