问题
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