简体   繁体   中英

How can I set the value of 'config.action_mailer.default_url_options' with domain.name?

I have to setup this for my devise usage.
But my app allows users to access through 2 domains.
I wanna set 'config.action_mailer.default_url_options = { :host =>' with the domain that users are accessing to.

How can I do this?

If what you want to achieve is that URLs in the emails you send out are constructed using the current host or domain, the default_url_options approach won't work, because it's only set once at application startup - the request object is not available in that context AFAIK, but only in controller actions.

You could try go generate URLs in your emails in the mailer class without using a default host, something like this (not tested):

class Mailer < ActionMailer::Base

  def welcome(user, host)
    @url = url_for(host: host, controller: ..., action: ...) 
    # construct rest of email here 
  end

end

and then call it from your controller with the appropriate host from the request:

Mailer.welcome(current_user, request.host).deliver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM