>我试图通过namecheap域名发送重置密码的电子邮件。我已经审查了StackOverflow中提供的所有解决方案,但无法获得可行的解决方案。如果我在下面缺少任何详细信息,请告诉我。My Rails 应用程序只是一个 API。
它通过gmail连接/smtp工作,当我将其切换到namecheap/privateemail smtp时,它工作了一次。在本地工作后,我将代码上传到 heroku,这就是它开始失败的时候。
# config/environments/development.rb
config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: 'noreply@domainemail.com'}
config.action_mailer.default_url_options = { :host => '587'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'mail.privatemail.com',
port: 587,
domain: 'domainname.com',
user_name: ENV['EMAIL'],
password: ENV['EMAIL_PW'],
authentication: :plain,
enable_starttls_auto: true,
openssl_verify_mode: 'none',
ssl: true
}
生产:
config.cache_classes = true
config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: 'noreply@domainname.com'}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'mail.privatemail.com',
port: 587,
domain: 'domainname.com',
user_name: ENV['EMAIL'],
password: ENV['EMAIL_PW'],
authentication: :plain,
enable_starttls_auto: true,
openssl_verify_mode: 'none'
}
通告程序邮件程序类
class NotifierMailer < ApplicationMailer
default_url_options[:host] = ENV['BACKEND_URL']
default from: 'noreply@domainemail.com'
def create
admin = Admin.find_by(email: params[:email])
admin.generate_password_reset_token!
Notifier.password_reset(admin).deliver
end
def password_reset(admin)
@admin = admin
@url = "#{ENV['BACKEND_URL']}/password/reset?token=#{@admin.reset_password_token}&email=#{@admin.email}"
mail(to: "#{@admin.first_name} #{@admin.last_name} <#{@admin.email}>",
subject: "Ion Portal - Password Reset")
end
end
密码控制器
class PasswordController < ApplicationController
protect_from_forgery with: :null_session
# include ActionController::RequestForgeryProtection
# protect_from_forgery with: :exception, unless: -> { request.format.json? }
def forgot
puts params
if params[:email].blank? # check if email is present
render json: {
error: "Email not present"
}
end
admin = Admin.find_by(email: params[:email]) # if present find admin by email
if admin.present?
admin.generate_password_token! #generate pass token
NotifierMailer.password_reset(admin).deliver_now
render json: { status: 'ok' }
else
render json: { error: ["Email address not found. Please check and try again."]}, status: :not_found
end
end
def reset
token = params[:token].to_s
if params[:email].blank?
return render json: {error: "Token not present"}
end
admin = Admin.find_by(reset_password_token: token)
if admin.present? && admin.password_token_valid?
if admin.reset_password!(params[:password])
redirect_to "#{ENV['ION_URL']}"
else
render json: {error: admin.errors.full_messages}, status: :unprocessable_entity
end
else
render json: { error: ["Link not valid or expired. Try generating a new link."]}, status: :not_found
end
end
def update
if !params[:password].present?
render json: {error: 'Password not present'}, status: :unprocessable_entity
return
end
if current_user.reset_password(params[:password])
render json: {status: 'ok'}, status: :ok
else
render json: {errors: current_user.errors.full_messages}, status: :unprocessable_entity
end
end
def successful_reset
render success_path
end
end
这些设置对我有用。 事实证明,我也离开了MailCatcher的设置,这是我最初的问题。 还要仔细检查域设置和服务器地址是否匹配,这在开发中意味着将域设置为"localhost:3000"。 祝你好运!
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'mail.privateemail.com',
:port => 587,
:domain => 'localhost:3000',
:user_name => 'email@email.com',
:password => 'xxxxxxxxxxxxxxx',
:authentication => :plain,
:enable_starttls_auto => true,
}
经过 24 小时的反复试验,我不得不删除几行代码才能使其工作。
config.action_mailer.smtp_settings = {
# For Gmail
# :address => "smtp.gmail.com",
# :port => "587",
# :domain => "gmail.com",
# :user_name => "noreply@gmail.com",
# :password => "pasword!",
# :authentication => "plain",
# :enable_starttls_auto => true
# For Namecheap
:enable_starttls_auto => true, #this is the important stuff!
:address => 'smtp.privateemail.com',
:port => 587,
:domain => 'privateemail.com',
:authentication => :plain,
:user_name => 'noreply@domainname.com',
:password => 'password!'
}
I removed the following:
enable_starttls_auto: true,
openss
l_verify_mode: 'none'