简体   繁体   English

Rails Mailer不发送电子邮件。 Smtp设置

[英]Rails Mailer does not send emails. Smtp settings

UPDATE: 更新:

Problem was so silly that it shouldn't even be on StackExchange. 问题是如此愚蠢,甚至不应该出现在StackExchange上。 Adress had to be written Address... 地址必须写成地址...

But now I get it - :sendmail was working fine, since it created its own server. 但是现在我明白了-:sendmail运行正常,因为它创建了自己的服务器。 But, what is strange, that letters sent via :sendmail went to junk box, while now, after I figured out that silly mistake and fixed app to use :smtp directly, all letters comes to normal Inbox even dough I fixed error in word address - it probably means that by using :sendmail, :smtp ignores outgoing server and uses its own, from localhost... 但是,奇怪的是,通过:sendmail发送的信件进入了垃圾箱,而现在,在我发现了这个愚蠢的错误并修复了应用程序直接使用:smtp之后,所有信件都进入了正常的收件箱,甚至是面团,我也修复了字地址中的错误-这可能意味着通过使用:sendmail,:smtp会忽略传出服务器,并使用来自本地主机的服务器...

Problems on delivering email on my very first Rails 5 app (Agile Web Development with Rails 5) 在我的第一个Rails 5应用程序上交付电子邮件时遇到的问题(使用Rails 5进行敏捷Web开发)

Models: 楷模:

Product
LineItem
Cart
Order

Users adds one Product or more to Cart as a LineItem. 用户将一个或多个产品作为LineItem添加到购物车。 Hits checkout, fills his email and confirms order. 点击结帐,填写他的电子邮件并确认订单。 At this point email should be delivered to his email adress, but it is not happening. 此时,应该将电子邮件发送到他的电子邮件地址,但是没有发生。

orders_controller.rb ( create action): orders_controller.rb( create动作):

  def create
    @order = Order.new(order_params)
    @order.add_line_items_from_cart(@cart)

    respond_to do |format|
      if @order.save
        Cart.destroy(session[:cart_id])
        session[:cart_id] = nil
        OrderMailer.received(@order).deliver_later
        format.html { redirect_to store_index_url, notice: 'Thank you for your order.' }
        format.json { render :show, status: :created, location: @order }
      else
        format.html { render :new }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end

order_mailer.rb ( received method - this should send that email): order_mailer.rb( received方法-这应该发送该电子邮件):

  def received(order)
    @order = order

    mail to: order.email, subject: 'Order confirmation'
  end

development.rb: development.rb:

Depot::Application.configure do

  config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
    adress:         "smtp.gmail.com",
    port:           587,
    domain:         "gmail.com",
    authentication: "plain",
    user_name:      "my-gmail-account",
    password:       "my-gmail-password",
    enable_starttls_auto: true
  }

  config.action_mailer.perform_deliveries = true

  config.cache_classes = false

  config.eager_load = false

  config.consider_all_requests_local = true

  if Rails.root.join('tmp/caching-dev.txt').exist?
    config.action_controller.perform_caching = true

    config.cache_store = :memory_store
    config.public_file_server.headers = {
      'Cache-Control' => 'public, max-age=172800'
    }
  else
    config.action_controller.perform_caching = false

    config.cache_store = :null_store
  end

  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.perform_caching = false

  config.active_support.deprecation = :log

  config.active_record.migration_error = :page_load

  config.assets.debug = true

  config.assets.quiet = true

  config.file_watcher = ActiveSupport::EventedFileUpdateChecker
end

From the log I assume it is something wrong with routing..?: 从日志中,我认为路由..?是有问题的:

  Rendering /Users/me/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout
  Rendering /Users/me/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /Users/me/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.1ms)
  Rendered collection of /Users/me/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/routes/_route.html.erb [34 times] (9.1ms)
  Rendered /Users/me/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.1ms)
  Rendering /Users/me/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /Users/me/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.1ms)
  Rendered /Users/me/.rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (93.8ms)

I tried restarting Webrick on my Mac (Ctrl + C) and changed Gmail email settings to allow less secure apps using it. 我尝试在Mac(Ctrl + C)上重新启动Webrick,并更改了Gmail电子邮件设置,以允许安全性较低的应用程序使用它。 Any help is very appreciated. 任何帮助都非常感谢。

将以下内容添加到development.rb

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

Please find below solution. 请找到以下解决方案。

add default url option for action mailer inside you env file. 在您的env文件中为操作邮件程序添加默认的url选项。

config.action_mailer.default_url_options = { host: 'YOUR HOST NAME', port: PORT OF YOUR SERVER }

Please use .deliver_now for quick action of action mailer. 请使用.deliver_now来使操作邮件程序快速动作。

我建议您尝试使用mailcatcher gem,以进行舒适的开发电子邮件调试。

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

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