15

我正在尝试制作一个应用程序,在用户注册时发送一封电子邮件。

我在 config/application.rb 文件中输入了 gmail 的 smtp 设置,邮件功能看起来像

mail(:to => "me@me.com", :subject => "Mail!", :from => "another@me.com", :content_type => "text/html")

现在当我看到日志时,我看到它说邮件已发送,但我根本没有收到任何邮件......

另外,当我调用邮件传递功能时Emails.signed(@user).deliver,表单页面不会重定向,但如果我注释掉电子邮件发送代码,它可以工作

Emails.signed(@user).deliver

或者

mail(:to => "me@me.com", :subject => "Mail!", :from => "another@me.com", :content_type => "text/html")

谢谢 :)

编辑:开发.rb

App::Application.configure do
  # Settings specified here will take precedence over those in config/environment.rb

  # In the development environment your application's code is reloaded on
  # every request.  This slows down response time but is perfect for development
  # since you don't have to restart the webserver when you make code changes.
  config.cache_classes = false

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true

  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_view.debug_rjs             = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_deliveries = true

  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log

end
4

4 回答 4

35

有点晚了,但尽管如此,这可能会为某人节省几个小时的头撞。这可能只与从 gmail 发送电子邮件有关。首先,为了帮助调试情况,将 development.rb 中的以下行设置为 true(假设您处于开发模式):

config.action_mailer.raise_delivery_errors = true

这将使 ActionMailer 不会默默地忽略错误。当我这样做时,我意识到 gmail 拒绝了我的用户名和密码。然后我转到我的配置文件,在其中放置了所有 Action Mailer 配置指令(对我来说,它位于 development.rb 中,可能有更好的放置位置),并注意到 :user_name 设置为“admin”而不是“admin@thedomain.com”。改变它解决了这个问题。这是我在 development.rb 中更正的部分:

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :domain               => 'thedomain.com',
  :user_name            => 'admin@thedomain.com',
  :password             => '<password>',
  :authentication       => 'plain',
  :enable_starttls_auto => true  }

参考:

http://forums.pragprog.com/forums/43/topics/541 http://edgeguides.rubyonrails.org/action_mailer_basics.html

于 2010-11-23T16:29:02.667 回答
7

另一件不要忘记的事情:您必须在更改环境配置文件后重新启动应用程序。使用乘客时,这很快就会被错过:)

当 ActionMailer 不想在不显示任何错误的情况下发送电子邮件时,这就是解决我的“问题”的原因。

于 2011-02-09T15:38:19.840 回答
6

这里写的东西对我没有帮助。

我正在使用 Rails 3.2.8,我花了几个小时试图弄清楚这一点,最后它非常简单。我忘了调用方法调用返回.deliver()Mail::Message对象mail(:to => @user.email, :subject => 'Welcome to the Site')

只需保留官方 RoR 教程中指定的所有内容即可。

也就是说,在您的开发/生产环境文件中,创建如下部分:

  # mailer
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
      address:              'smtp.gmail.com',
      port:                 587,
      domain:               'gmail.com',
      user_name:            '<username>@gmail.com',
      password:             '<password>',
      authentication:       'plain',
      enable_starttls_auto: true
  }

然后将 ActionMailer::Base 子类化,例如:

class InfoMailer < ActionMailer::Base
  default from: "<username>@gmail.com"

  def welcome_user_and_send_password(user, password)
    default_url_options = self.default_url_options()


    @user = user
    @password = password
    @url = default_url_options[:host]
    mail(:to => @user.email, :subject => 'Welcome to the Site').deliver()
  end

end

之后,您可以InfoMailer像类方法一样简单地从代码中使用此方法:

InfoMailer.welcome_user_and_send_password(user, password)
于 2014-05-09T23:34:02.030 回答
0

如果你使用的是测试环境,一定要在environments/test.rb中注释掉这行代码:

config.action_mailer.delivery_method = :test
于 2015-03-15T00:53:33.673 回答