如何配置 Rails ActionMailer 的主机名?

发布于 2024-07-17 10:45:53 字数 523 浏览 6 评论 0原文

我正在处理一个相当传统的忘记密码电子邮件 - 我想通过电子邮件向用户发送嵌入在链接中的密码更改令牌,他们可以单击该链接来更改密码。 我通过传统的 ActionMailer 发送电子邮件。

如果我使用普通的 link_to 标签,

<%= link_to "click here", :controller => foo, :action => 'bar', :token => token %>

我会得到一个相对链接 - 从电子邮件中毫无用处。

如果我添加

:only_path =>; false,则错误提示我需要设置 default_url_options[:host]。 ActionController 文档暗示您可以通过重写控制器中的 #default_url_options 方法来实现这一点。 当然有一个配置选项可以告诉 Rails 它的主机名是什么,而无需添加我自己的配置文件、解析它等?

I'm working on a fairly traditional forgot password email - I want to email the user a password change token embedded in a link that they can click on in order to change their password. I'm emailing via the traditional ActionMailer.

If I use a normal link_to tag

<%= link_to "click here", :controller => foo, :action => 'bar', :token => token %>

I get a relative link - rather useless from an email.

If I add in

:only_path => false, then it errors saying I need to set default_url_options[:host]. The ActionController docs imply that you do that by overriding the #default_url_options methods in your controller. Surely there's a configuration option to tell Rails what it's hostname is without adding my own config file, parsing it, etc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

茶底世界 2024-07-24 10:45:53

default_url_options 可从 config.action_mailer 获取,并且应在您的环境的配置文件中进行设置。

例如,在 config/environments/development.rb 中:

config.action_mailer.default_url_options = {
  :host => 'www.yourdomain.com'
}

对于本地测试,修改 config/environments/development.rb

config.action_mailer.default_url_options = {
  :host => '127.0.0.1',
  :port => 3000
}

然后,假设您有一个名为 的命名路由>forgot_password_login,您可以使用类似以下内容在邮件程序中生成登录链接 URL:

forgot_password_login_url(:token => 'a7s8q15sk2...')

default_url_options is available from config.action_mailer and should be set in your environment's configuration file.

For example, in config/environments/production.rb:

config.action_mailer.default_url_options = {
  :host => 'www.yourdomain.com'
}

For local testing, modify config/environments/development.rb:

config.action_mailer.default_url_options = {
  :host => '127.0.0.1',
  :port => 3000
}

Then, assuming you have a named route called forgot_password_login, you can generate the login link URL in your mailer using something like this:

forgot_password_login_url(:token => 'a7s8q15sk2...')
脸赞 2024-07-24 10:45:53

您可能想要设置 :protocol => 顺便说一句,还有“https”。

config.action_mailer.default_url_options = { 
    :host => "portal.example.com", 
    :protocol => 'https' 
}

You probably want to set :protocol => 'https' as well, btw.

config.action_mailer.default_url_options = { 
    :host => "portal.example.com", 
    :protocol => 'https' 
}
风月客 2024-07-24 10:45:53

还有另一种选择,如 http 中所述://pivotallabs.com/how-i-leaned-to-stop-hating-and-love-action-mailer/

该解决方案的优点是不需要任何配置(因此不那么麻烦),只要您从控制器内发送电子邮件,就可以正常工作。

但是,如果您计划在不通过控制器的情况下发送电子邮件(例如从命令行或响应另一封电子邮件),则需要静态配置。

There is another alternative, as described in http://pivotallabs.com/how-i-leaned-to-stop-hating-and-love-action-mailer/

This solution has the advantage that it doesn't require any configuration (so less of a hassle), and works fine as long as you send emails from within controllers.

But if you plan on sending email without going through a controller (e.g. from command line or in response to another email), you need the static configuration.

暮倦 2024-07-24 10:45:53

Rails 3.1 中不推荐直接设置 default_url_options。 请改用 url_for

添加参数 :protocol 覆盖默认值(http),:protocol => 'https://'。 这将创建以“https://...”而不是默认的“http://”开头的网址

Setting default_url_options directly is deprecated in Rails 3.1. Use url_for instead.

Add parameter :protocol to override default value (http), :protocol => 'https://'. This will create url starting with "https://..." instead of default "http://"

相权↑美人 2024-07-24 10:45:53

有趣的是,我遇到了和你一样的问题,但是是在单元测试中(同时遵循迈克尔·哈特尔的railstutorial)。 我的 test.rb 文件中有这一行,但这没有帮助:

config.action_mailer.default_url_options = { host: 'example.com', protocol: 'http' }

我还在 test.rb 中添加了另一行这样的行,令人惊讶的是,这解决了问题

default_url_options = { host: 'example.com', protocol: 'http' }

Interestingly, I had the same issue as you did, but in unit tests (while following Michael Hartl's railstutorial). I had this line in my test.rb file, but that didn't help:

config.action_mailer.default_url_options = { host: 'example.com', protocol: 'http' }

I've also added another line like this to test.rb, and surprisingly this solved the issue

default_url_options = { host: 'example.com', protocol: 'http' }
日裸衫吸 2024-07-24 10:45:53

Rails 3.1 中不推荐直接设置 default_url_options

使用 url_for 帮助器创建它:

<%= link_to "click here", url_for(:controller => foo, :action => 'bar', :token => token, :host => 'www.yourdomain.com') %>

Setting default_url_options directly is deprecated in Rails 3.1

Use the url_for helper to create it:

<%= link_to "click here", url_for(:controller => foo, :action => 'bar', :token => token, :host => 'www.yourdomain.com') %>
帝王念 2024-07-24 10:45:53

你能做吗

<%="click here", :controller => foo, :action => 'bar', :token => token, :host=>request.host -%>

Can you just do

<%="click here", :controller => foo, :action => 'bar', :token => token, :host=>request.host -%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文