简体   繁体   English

Rails Mailer 不在网站上的生产环境中工作,但在控制台中工作

[英]Rails Mailer not working in production on website, but works in console

I'm banging my head against a wall.我正在用头撞墙。

So here is my problem.所以这是我的问题。 Let's start really simple.让我们从非常简单的开始。 I have a contact form in my app, it sends emails after clicking submit.我的应用程序中有一个联系表,它会在点击提交后发送电子邮件。 This form works perfectly in development mode.这种形式在开发模式下工作得很好。 But for some unknown reason it does not work in production.但由于某些未知原因,它在生产中不起作用。

Well, what would you do now?那么,你现在会做什么? Check production.rb in config/environments?检查配置/环境中的 production.rb? Already done.已经完成了。 Actually more than that - I have tried sending a mail with the rails console in production mode on my VPS.实际上不止于此——我曾尝试在我的 VPS 上以生产模式使用 Rails 控制台发送邮件。 And it works.它有效。

UserMailer.send_data(Deliverer.new(full_name: "John Smith", email: "john.smith@yahoo.com", message: "Hello how are you")).deliver_now

I have a model called Deliverer, purely for antispam purposes.我有一个名为 Deliverer 的模型,纯粹用于反垃圾邮件目的。 After entering the rails console in production on my server and pasting this command, a mail gets sent to my mailbox, just as it is supposed to work from the website.在我的服务器上进入生产环境中的 Rails 控制台并粘贴此命令后,一封邮件将发送到我的邮箱,就像它应该在网站上工作一样。

This is my production.rb file这是我的 production.rb 文件

require "active_support/core_ext/integer/time"

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.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present?
  config.assets.compile = true
  config.active_storage.service = :local

  config.force_ssl = true

  config.action_mailer.default_url_options = { host: ENV['DOMAIN'] }

  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    port: '465',
    address: 'smtp.seznam.cz',
    domain: ENV['DOMAIN'],
    user_name: ENV['SMTP_USERNAME'],
    password: ENV['SMTP_PASSWORD'],
    authentication: :plain,
    enable_starttls_auto: true,
    ssl: true,
    tls: true
  }

  config.log_level = :info

  config.log_tags = [ :request_id ]


  config.action_mailer.perform_caching = false

  config.i18n.fallbacks = true
  config.active_support.report_deprecations = false
  config.log_formatter = ::Logger::Formatter.new

  if ENV["RAILS_LOG_TO_STDOUT"].present?
    logger           = ActiveSupport::Logger.new(STDOUT)
    logger.formatter = config.log_formatter
    config.logger    = ActiveSupport::TaggedLogging.new(logger)
  end

  # Do not dump schema after migrations.
  config.active_record.dump_schema_after_migration = false
end

This is the SMTP error that I found in /log/production.log这是我在 /log/production.log 中发现的 SMTP 错误

Net::SMTPFatalError (550 5.7.1 Not authenticated, please use AUTH first. Net::SMTPFatalError (550 5.7.1 未认证,请先使用AUTH。

As I said, this error shows up only in production when trying to send the mail from the website.正如我所说,此错误仅在尝试从网站发送邮件时出现在生产环境中。 Sending an email from the console on production works, the form from the website does not.从生产作品的控制台发送电子邮件,网站上的表格没有。 And as I said, everything works fine in development, both the console and the form itself.正如我所说,在开发中一切正常,包括控制台和表单本身。

And I have no idea what to do.我不知道该怎么办。

TL:DR The mailer works in development mode (both from the website and with the rails console). TL:DR邮件程序在开发模式下工作(来自网站和 Rails 控制台)。 In production, the mailer works only in rails console, but not from the website.在生产中,邮件程序只能在 Rails 控制台中工作,而不是在网站上工作。

Mailer in production after submitting the form from the website does not work, but works fine when using the rails console.从网站提交表单后生产中的邮件程序不起作用,但在使用 Rails 控制台时工作正常。 No problems at all in development.开发完全没有问题。

Okay, I've managed to solve this problem.好的,我已经设法解决了这个问题。

For some unknown reason, Rails was failing to recognize ENV[] variables.由于某些未知原因,Rails 无法识别 ENV[] 变量。 I really don't know why.我真的不知道为什么。

But once I replaced ENV['SMTP_USERNAME'] with "username@domain.com" and ENV['SMTP_PASSWORD'] with "examplepassword" it all started working and mailer sends emails to my inbox.但是,一旦我将 ENV['SMTP_USERNAME'] 替换为“username@domain.com”并将 ENV['SMTP_PASSWORD'] 替换为“examplepassword”,它就开始工作并且邮件程序将电子邮件发送到我的收件箱。

I got no idea, because entering rails console or irb and using我不知道,因为进入 rails console 或 irb 并使用

puts ENV['SMTP_USERNAME']放 ENV['SMTP_USERNAME']

actually printed my smtp username, the exact same thing happened with password and other environmental variables.实际上打印了我的 smtp 用户名,密码和其他环境变量也发生了完全相同的事情。 I got no idea what was wrong.我不知道出了什么问题。

EDIT Okay I've done a little bit of troubleshooting.编辑好吧,我已经做了一些故障排除。 It seems like only config/environments/production.rb has a problem with accessing ENV variables.似乎只有 config/environments/production.rb 在访问 ENV 变量时有问题。 I'm not sure why...我不确定为什么...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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