在开发环境中使用Rails 3发送邮件。

时间:2021-06-15 23:33:27

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的railscast教程,我可以看到正在生成的邮件,但是我没有收到它们。

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            => "xxx@gmail.com",
  :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

我在我的配置/环境中尝试过上面的代码(当然是有效的gmail细节)。rb、配置/环境/发展。rb,目前它在自己的initialiser配置/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

有谁知道其他的gotcha我可能错过了需要为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.

在我的配置/发展。但是开发日志显示的还是和我在终端上看到的一样。

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

非常感谢

8 个解决方案

#1


55  

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

然后我添加配置/电子邮件。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

就像我说的,不知道为什么,但这似乎是成功的秘诀。谢谢大家的指点

#2


27  

I have the following in 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 :)

希望这有助于:)

#3


5  

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            => "xxx@gmail.com",
  :password             => "yyy",
  :authentication       => "plain",
  :enable_starttls_auto => true
}

#4


3  

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: true is the default and unnecessary.

最后,enable_starttls_auto: true是默认值,没有必要。

#5


1  

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

把所有的配置放到:config/environment /development.rb中

I mean

我的意思是

ActionMailer::Base.delivery_method = :smtp

ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => "587",
  :domain               => "gmail.com",
  :user_name            => "xxx@gmail.com",
  :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.

它为我工作。

#6


1  

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

有两个必要的步骤让我克服了这个问题吗

#7


0  

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

#8


0  

My two pennies worth:

我的两个便士的价值:

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

我对Rails 5.1也有同样的症状:什么也没发生,开发中的设置。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.rb are, in this respect, misleading:

然而,这个问题很棘手,因为您没有预料到这种行为。在我看来,开发中的默认注释。在这方面,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*.

#1


55  

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

然后我添加配置/电子邮件。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

就像我说的,不知道为什么,但这似乎是成功的秘诀。谢谢大家的指点

#2


27  

I have the following in 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 :)

希望这有助于:)

#3


5  

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            => "xxx@gmail.com",
  :password             => "yyy",
  :authentication       => "plain",
  :enable_starttls_auto => true
}

#4


3  

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: true is the default and unnecessary.

最后,enable_starttls_auto: true是默认值,没有必要。

#5


1  

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

把所有的配置放到:config/environment /development.rb中

I mean

我的意思是

ActionMailer::Base.delivery_method = :smtp

ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => "587",
  :domain               => "gmail.com",
  :user_name            => "xxx@gmail.com",
  :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.

它为我工作。

#6


1  

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

有两个必要的步骤让我克服了这个问题吗

#7


0  

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

#8


0  

My two pennies worth:

我的两个便士的价值:

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

我对Rails 5.1也有同样的症状:什么也没发生,开发中的设置。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.rb are, in this respect, misleading:

然而,这个问题很棘手,因为您没有预料到这种行为。在我看来,开发中的默认注释。在这方面,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*.