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

问题描述 投票:0回答:2

我试图通过namecheap域发送重置密码电子邮件。我已经回顾了StackOverflow中提供的每个解决方案,但未能获得可行的解决方案。如果我遗漏下面的任何细节,请告诉我。 My 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: '[email protected]'}

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: '[email protected]'}
  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: '[email protected]'


  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
ruby-on-rails smtp timeout actionmailer namecheap
2个回答
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 protected]',
:password             => 'xxxxxxxxxxxxxxx',
:authentication       => :plain,
:enable_starttls_auto => true,
}

0
投票

经过24小时的反复试验,我不得不删除几行代码才能使其正常工作。

 config.action_mailer.smtp_settings = {
# For Gmail
      # :address              => "smtp.gmail.com",
      # :port                 => "587",
      # :domain               => "gmail.com",
      # :user_name            => "[email protected]",
      # :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      => '[email protected]',
      :password       => 'password!'
    }

I removed the following: 

    enable_starttls_auto: true,
      openss

    l_verify_mode:  'none'
© www.soinside.com 2019 - 2024. All rights reserved.