Rails Net::OpenTimeout 邮件程序 Namecheap Heroku 无法发送邮件程序 500 错误

Rails Net::OpenTimeout Mailer Namecheap Heroku Unable to send mailer 500 error

我试图通过 namecheap 域发送重置密码电子邮件。我已经审查了 Whosebug 中提供的每个解决方案,但未能获得可行的解决方案。如果我遗漏了以下任何详细信息,请告诉我。 我的 Rails 申请只有 API。

它通过 gmail connection/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'
  }

NotifierMailer class

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'