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

时间:2016-10-28 08:48:19

标签: ruby-on-rails smtp ruby-on-rails-5 mailer

UPDATE:

问题太愚蠢了,甚至不应该在StackExchange上。 地址必须写成地址......

但现在我明白了 - :sendmail工作正常,因为它创建了自己的服务器。但是,奇怪的是,通过发送的信件:sendmail去了垃圾箱,而现在,在我弄清楚那个愚蠢的错误和固定的应用程序使用后:smtp直接,所有字母都变为正常收件箱甚至面团我修复了字地址错误 - 它可能意味着通过使用:sendmail,:smtp忽略传出服务器并使用自己的,来自localhost ......

在我的第一个Rails 5应用程序(使用Rails 5的敏捷Web开发)上发送电子邮件的问题

型号:

Product
LineItem
Cart
Order

用户将一个或多个产品添加到购物车作为LineItem。点击结账,填写他的电子邮件并确认订单。此时,电子邮件应该发送到他的电子邮件地址,但它没有发生。

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方法 - 这应该发送该电子邮件):

  def received(order)
    @order = order

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

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

从日志中我认为路由有问题..?:

  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)

我尝试在Mac上重启Webrick(Ctrl + C)并更改Gmail电子邮件设置,以允许使用它的安全性较低的应用。非常感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

将以下内容添加到development.rb

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

答案 1 :(得分:1)

请找到以下解决方案。

在你的env文件中添加动作邮件的默认网址选项。

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

请使用.deliver_now快速操作邮件邮件。

答案 2 :(得分:0)

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