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

时间:2019-04-10 19:01:58

标签: ruby-on-rails smtp timeout actionmailer namecheap

我试图通过namecheap域发送重设密码电子邮件。我已经审查了StackOverflow中提供的所有解决方案,但还没有找到可行的解决方案。如果我遗漏了以下任何详细信息,请告诉我。 我的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'
  }

NotifierMailer类

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

2 个答案:

答案 0 :(得分:0)

这些设置对我有用。原来,我还离开了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,
}

答案 1 :(得分:0)

经过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'