Rails : Net::SMTPAuthenticationError (535 Authentication failed: Bad username / password )

落花浮王杯 提交于 2020-05-27 12:31:30

问题


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            => "myemail@gmail.com",
   :password             => "mypassword",
   :authentication       => "plain",
   :enable_starttls_auto => true
  }

example_mailer.rb:

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

回答1:


In your mailer, there is typo:

class ExampleMailer < ActionMailer::Base
  default from: "myemail@gmail.com"
  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..



来源:https://stackoverflow.com/questions/28472490/rails-netsmtpauthenticationerror-535-authentication-failed-bad-username

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!