Ruby-on-rails 在开发环境中使用 Rails 3 发送邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4313177/
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 23:44:47  来源:igfitidea点击:

Sending mail with Rails 3 in development environment

ruby-on-railsruby-on-rails-3actionmailer

提问by Addsy

I'm sure this has been asked a million times before but I can't find anything that works for me so I'm asking again!

我确定这之前已经被问过一百万次,但我找不到任何对我有用的东西,所以我再问一次!

I just need a way of sending emails using ActionMailer in rails 3. I have followed numerous tutorials including the Railscasts tutorial on the new ActionMailer and I can see the mails being generated but I don't receive them.

我只需要一种在 rails 3 中使用 ActionMailer 发送电子邮件的方法。我已经学习了许多教程,包括关于新 ActionMailer 的 Railscasts 教程,我可以看到正在生成的邮件,但我没有收到它们。

I have tried a bunch of different ways but they generally amount to configuring the following settings

我尝试了很多不同的方法,但它们通常相当于配置以下设置

ActionMailer::Base.delivery_method = :smtp

ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => "587",
  :domain               => "gmail.com",
  :user_name            => "[email protected]",
  :password             => "yyy",
  :authentication       => "plain",
  :enable_starttls_auto => true
}

I have tried the above code (with valid gmail details of course) in my config/environment.rb, config/environments/development.rb and currently have it in its own initialiser config/initialisers/setup_mail.rb

我已经在我的 config/environment.rb、config/environments/development.rb 中尝试了上面的代码(当然还有有效的 gmail 详细信息),并且目前在它自己的初始化程序 config/initialisers/setup_mail.rb 中有它

I have also tried with a few different smtp servers including Gmail and Sendgrid, adjusting the smtp settings accordingly but still nothing. I can see the mail in the terminal and the development log and that's it.

我还尝试了一些不同的 smtp 服务器,包括 Gmail 和 Sendgrid,相应地调整了 smtp 设置,但仍然没有。我可以在终端中看到邮件和开发日志,仅此而已。

Does anyone know of any other gotcha's that I may have missed that need to be setup for ActionMailer to work? Failing that is there a way of getting more information about why the mails aren't being sent? I have

有没有人知道我可能错过的任何其他问题,需要设置 ActionMailer 才能工作?如果失败,有没有办法获得有关为什么没有发送邮件的更多信息?我有

config.action_mailer.raise_delivery_errors = true

set in my config/development.rb but the development log still just shows the same as I see in the terminal.

在我的 config/development.rb 中设置,但开发日志仍然与我在终端中看到的一样。

For what it's worth, I am developing on a Ubuntu 10.04 laptop just in case there's any specific setup needed for that.

就其价值而言,我正在 Ubuntu 10.04 笔记本电脑上进行开发,以防万一需要进行任何特定设置。

Many thanks

非常感谢

回答by Addsy

Well I have resolved the issue, but quite why this works and the other methods did not, I don't know.

嗯,我已经解决了这个问题,但为什么这有效而其他方法没有,我不知道。

The solution was to create an initialiser in config/initialisers/setup_mail.rb containing the following

解决方案是在 config/initialisers/setup_mail.rb 中创建一个包含以下内容的初始化程序

if Rails.env != 'test'
  email_settings = YAML::load(File.open("#{Rails.root.to_s}/config/email.yml"))
  ActionMailer::Base.smtp_settings = email_settings[Rails.env] unless email_settings[Rails.env].nil?
end

I then added config/email.yml containing the details of the dev and production email accounts

然后我添加了 config/email.yml 包含开发和生产电子邮件帐户的详细信息

development:
  :address: smtp.gmail.com
  :port: 587
  :authentication: plain
  :user_name: xxx
  :password: yyy
  :enable_starttls_auto: true
production:
  :address: smtp.gmail.com
  :port: 587
  :authentication: plain
  :user_name: xxx
  :password: yyy
  :enable_starttls_auto: true

Like I say, no idea why, but this seemed to do the trick. Thanks all for the pointers

就像我说的,不知道为什么,但这似乎奏效了。谢谢大家指点

回答by nathanvda

I have the following in config/environments/development.rb

我有以下内容 config/environments/development.rb

config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true

The actual mail-configuration, config.actionmailer.*i have placed in config\application.rb.

实际的邮件配置,config.actionmailer.*我已经放在config\application.rb.

Hope this helps :)

希望这可以帮助 :)

回答by Anubhaw

Try using 'sendmail' instead of 'smtp'.

尝试使用“sendmail”而不是“smtp”。

ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.sendmail_settings = {
  :address              => "smtp.gmail.com",
  :port                 => "587",
  :domain               => "gmail.com",
  :user_name            => "[email protected]",
  :password             => "yyy",
  :authentication       => "plain",
  :enable_starttls_auto => true
}

回答by IAmNaN

Three things.

三件事。

First, the port is an integer and does not need quotes, as in your first example. (But I think a string should still work.)

首先,端口是一个整数,不需要引号,就像你的第一个例子一样。(但我认为字符串应该仍然有效。)

Second, don't forget to restart your server each time you modify this (or any) initializer file. This could explain why you didn't see an error after adding:

其次,每次修改这个(或任何)初始化文件时不要忘记重新启动服务器。这可以解释为什么您在添加后没有看到错误:

config.action_mailer.raise_delivery_errors = true

config.action_mailer.raise_delivery_errors = true

Without having that error message, it's hard to determine why the mail wasn't going but now is. One possiblity is your use of double quotes around the password. If you were using a strong password and had a token in your password that wasn't escaped it could have been reinterpreted. (i.e. "P@ssw\0rd"would become P@ssrd). For just this reason, I always use single quotes in my code unless I specifically need the syntactic sugar.

如果没有该错误消息,就很难确定为什么邮件没有通过,但现在是。一种可能性是您在密码周围使用双引号。如果您使用的是强密码并且密码中有一个未转义的令牌,则可能会被重新解释。(即"P@ssw\0rd"会成为P@ssrd)。正是出于这个原因,我总是在我的代码中使用单引号,除非我特别需要语法糖。

Lastly, enable_starttls_auto: trueis the default and unnecessary.

最后,enable_starttls_auto: true是默认的和不必要的。

回答by Jerome

ActionMailer::Base.delivery_method = :sendmail
and
config.action_mailer.perform_deliveries = true

ActionMailer::Base.delivery_method = :sendmail

config.action_mailer.perform_deliveries = true

were the two necessary steps that got me over this issue

是让我解决这个问题的两个必要步骤

回答by Nickle

Just put all config to: config/environments/development.rb

只需将所有配置放在:config/environments/development.rb

I mean

我的意思是

ActionMailer::Base.delivery_method = :smtp

ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => "587",
  :domain               => "gmail.com",
  :user_name            => "[email protected]",
  :password             => "yyy",
  :authentication       => "plain",
  :enable_starttls_auto => true
}

and

config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true

It worked for me.

它对我有用。

回答by Varus Septimus

My two pennies worth:

我的两便士价值:

I had those exact same symptoms with Rails 5.1: Nothing happened, the settings in my development.rbfile were utterly ignored...

我在 Rails 5.1 中遇到了完全相同的症状:什么也没发生,我的development.rb文件中的设置被完全忽略了......

Then I remembered to restart the machine!(which solved magically the issue)

然后我记得重启机器!(神奇地解决了这个问题)

This had been pointed out by a couple of previous comments.

之前的一些评论已经指出了这一点。

The issue is tricky however because you do not expect this behavior. In my view, the default comments in development.rbare, in this respect, misleading:

然而,这个问题很棘手,因为您不希望这种行为。在我看来,development.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 web server when you make code changes*.

回答by seyyah

In addition to, your gmail username does not alias.

此外,您的 gmail 用户名没有别名。

Ref: https://support.google.com/mail/answer/12096?hl=en

参考:https: //support.google.com/mail/answer/12096?hl=en