简体   繁体   中英

Rails 4 Mailer not working in production Digital Ocean server

I have a simple contact form in my app that uses my Gmail account to send the e-mail to another Gmail account. For some reason it sends mails perfectly in development but no in production. I'm hosting my website in a DigitalOcean one-click Rails app. When I try to send a mail in the live version the website hangs for a bit and displays "We're sorry, but something went wrong."

Gemfile

#Mailer
gem 'mail'

development.rb

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.
  config.cache_classes = false
  config.eager_load = false
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false
  config.action_mailer.raise_delivery_errors = false
  config.active_support.deprecation = :log
  config.active_record.migration_error = :page_load
  config.assets.debug = true
  config.assets.digest = true
  config.assets.raise_runtime_errors = true

  config.action_mailer.default_url_options = { host: 'localhost', port: 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              => "smtp.gmail.com",
    :port                 => 465,                
    :user_name            => 'foo@gmail.com',
    :password             => 'foo',        
    :authentication       => :login,
    :ssl                  => true,
    :tls                  => true,
    :enable_starttls_auto => true
  }
end

production.rb

Rails.application.configure do

  config.cache_classes = true

  config.eager_load = true

  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

  config.assets.js_compressor = :uglifier

  config.assets.compile = false

  config.assets.digest = true

  config.log_level = :debug

  config.i18n.fallbacks = true

  config.active_support.deprecation = :notify

  config.log_formatter = ::Logger::Formatter.new

  config.active_record.dump_schema_after_migration = false

  config.action_mailer.default_url_options = { host: 'foo.com.mx' }
  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              => "smtp.gmail.com",
    :port                 => 465,                
    :user_name            => 'foo@gmail.com',
    :password             => 'foo',        
    :authentication       => :login,
    :ssl                  => true,
    :tls                  => true,
    :enable_starttls_auto => true
  }
end

I got an answer from the support desk and you can remedy this issue by prioritizing IPv4 over IPv6 in the etc/gai.conf file by un-commenting the line

#precedence ::ffff:0:0/96 100

The reason behind this is that Digital Ocean blocks outgoing SMTP requests over IPv6 by default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM