如何在特定环境的电子邮件中发送 URL?

发布于 2024-07-15 03:34:40 字数 603 浏览 10 评论 0原文

使用restful_authentication生成的代码,我们通常会在注册和激活邮件中获取“localhost”作为URL。 我正在尝试创建这样的东西:

def signup_notification(user, host)
  setup_email(user)
  @subject    += 'Please activate your new account'

  @body[:url]  = "http://#{host}/activate/#{user.activation_code}"
end

但是,由于传递邮件的实际调用是在观察者中,所以我不能这样做:

UserMailer.deliver_signup_notification(user, request.host_with_port)

由于请求方法在模型中不可用。 这样做的最佳方法是什么?

我考虑过将每个域的 URL 存储在 YAML 文件中,然后在启动时加载它,但端口可能会发生变化,因此它不起作用。

或者,我考虑在加载应用程序时设置的某个地方创建一个静态类变量,但我不知道如何执行此操作。 请求方法可用于初始化程序吗?

Using the code generated by restful_authentication, we usually get 'localhost' as the URL in the sign up and activation mails. I am trying to create something like this instead:

def signup_notification(user, host)
  setup_email(user)
  @subject    += 'Please activate your new account'

  @body[:url]  = "http://#{host}/activate/#{user.activation_code}"
end

However, since the actual call to deliver the mail is in an Observer, I can't do this:

UserMailer.deliver_signup_notification(user, request.host_with_port)

Since the request method is not available in models. What is the best way to go about doing this?

I thought about storing URLs per domain in a YAML file and then loading it on startup but then the ports may change, so it wouldn't work.

Alternatively, I thought about creating a static class variable somewhere that is set upon loading up the application, but I don't know how to go about doing this. Is the request method available for initializers?

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

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

发布评论

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

评论(4

回忆追雨的时光 2024-07-22 03:34:40

尝试 current_request 插件,参见 http://codeshooter.wordpress.com/2009/08/

为我工作

try current_request plugin, described at http://codeshooter.wordpress.com/2009/08/

worked for me

花海 2024-07-22 03:34:40

config/envrionments/development.rb 中设置它

config.action_mailer.default_url_options = { :host => "my.host.com" }

使用 ActiveMailer,我在模型 app/models/event_mailer.rb 中的

def new_event(event)
    recipients  EventMailer::NO_REPLY
    bcc         event.emails('new_event')
    from        EventMailer::FROM_EMAIL
    subject     "#{EventMailer::SUBJECT_HEADER} Event Updated :: #{event.title}"
    content_type    "text/plain"
    body        :event => event
end

,然后在邮件程序视图 app/ views/event_mailer/new_event.rb

You can view the event by going to: <%= url_for(:controller => "events", :action => "show", :id => @event.id, :only_path => false) %>

在邮件中生成:

You can view the event by going to: http://my.host.com/events/11

Using ActiveMailer, i set this in the config/envrionments/production.rb

config.action_mailer.default_url_options = { :host => "my.host.com" }

In the model app/models/event_mailer.rb

def new_event(event)
    recipients  EventMailer::NO_REPLY
    bcc         event.emails('new_event')
    from        EventMailer::FROM_EMAIL
    subject     "#{EventMailer::SUBJECT_HEADER} Event Updated :: #{event.title}"
    content_type    "text/plain"
    body        :event => event
end

And then in the mailer view app/views/event_mailer/new_event.rb

You can view the event by going to: <%= url_for(:controller => "events", :action => "show", :id => @event.id, :only_path => false) %>

Which Generates in the mail:

You can view the event by going to: http://my.host.com/events/11
随波逐流 2024-07-22 03:34:40

我也遇到了同样的问题,这就是我所做的

  1. 在用户模型中我将 :host 添加到 attr_accessible,然后添加此方法

    def 主机=(值) 
        write_attribute : 主机、值 
      结尾 
      
  2. ,在发送电子邮件(如创建和激活)的方法中,我添加了

    @user.host = request.host_with_port

因为我仍然是一个菜鸟,所以我真的希望有人能出现针对此问题提供最佳实践解决方案。

I got a same problem too and here's what I've done

  1. in user model I add :host to attr_accessible, then I add this method

    def host=(value)
      write_attribute :host, value
    end
    
  2. in user controller, in the methods that sends email like create and activate I put

    @user.host = request.host_with_port

since I'm still a noob-on-rails so I really hope someone will come up with a best-practice solution for this problem.

乜一 2024-07-22 03:34:40

您的服务器访问的主机名可以在请求的 Host 标头中找到。 您不应该在模型中访问它(假设 Rails 正常)。 但是,如果您可以访问控制器中的请求对象,则可以在从数据库中提取模型对象时将该值插入到模型字段中。

The hostname your server is accessed by is found in the Host header of the request. You shouldn't have access to it in the models (assuming rails is sane). But if you can access request objects in the controllers you can insert that value to your model field when you pull the model objects from db.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文