Ruby-on-rails Actionmailer 不发送邮件,使用 rails 3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3392282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:59:18  来源:igfitidea点击:

Actionmailer not delivering mail, with rails 3

ruby-on-railsrubyactionmailer

提问by Amit

I am trying to make an application, that sends an email when user registers.

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

i put in the smtp settings for gmail in the config/application.rb file and the mail function looks like

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

mail(:to => "[email protected]", :subject => "Mail!", :from => "[email protected]", :content_type => "text/html")

now when i see the logs, i see that it says mail has been sent, but i never receive any mail at all...

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

also, when i call the mail deliver function, Emails.signed(@user).deliver, the form page does not redirect, but it works if i comment out the email sending code that is either

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

Emails.signed(@user).deliver

or

或者

mail(:to => "[email protected]", :subject => "Mail!", :from => "[email protected]", :content_type => "text/html")

Thanks :)

谢谢 :)

Edit: development.rb

编辑:development.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

回答by AmitA

Somewhat late, but nevertheless maybe this will save someone a few hours of head banging. This is probably only relevant to sending emails from gmail. First, in order to help debugging the situation, set the following line in development.rb to true (assuming you're in development mode):

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

config.action_mailer.raise_delivery_errors = true

This will make ActionMailer not to silently ignore errors. When I did that, I realized gmail is refusing my username and password. I then went to my configuration file where I put all the Action Mailer config directives (for me it was in development.rb, there is probably a better place to put it), and noticed that :user_name was set to "admin" rather than "[email protected]". Changing it solved the problem. Here is my corrected part of development.rb:

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

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

References:

参考:

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

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

回答by Peter

Another thing not to forget: you have to restart the application after making changes in your environment config files. when using passenger this can quickly be missed :)

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

that's what solved my "problem" when ActionMailer didnt want to send emails without showing any errors..

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

回答by ancajic

The things written here did not help me.

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

I am using Rails 3.2.8 and I spent several hours trying to figure this out and it was very simple in the end. I forgot to call .deliver()on the Mail::Messageobject that is returned by mail(:to => @user.email, :subject => 'Welcome to the Site')method call.

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

Just leave everything like it is specified in official RoR tutorial.

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

That is, in your development/production environment files, make a section like:

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

  # 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
  }

And then you subclass ActionMailer::Base, for example like this:

然后你将 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

After that, you can simply use this InfoMailermethod from your code like a class method:

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

InfoMailer.welcome_user_and_send_password(user, password)

回答by Natasha Jaques

If you're using the test environment, be sure to comment out this line of code in environments/test.rb:

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

config.action_mailer.delivery_method = :test