CSS Images in Email With Rails 3

后端 未结 3 1921
半阙折子戏
半阙折子戏 2021-01-03 06:51

I\'m trying to send out an email with Rails 3 and Action Mailer. The email goes out fine, but I want it to be HTML formatted with some basic styling which includes backgroun

3条回答
  •  [愿得一人]
    2021-01-03 07:28

    In response to my other answer, @Kevin wrote:

    Thanks for the answer, I did think of doing something like that but I don't think it's possible with the way I have it setup. The mailer call is happening in a after_create call in a model rather than in a controller so I don't think I have access to the request object as you mentioned (or am I mistaken). I do have this in my mailer initializer: ActionMailer::Base.default_url_options[:host] = "localhost:3000" Can I somehow use that hos parameter in my mailer to make it work?

    The answer is yes. All rails url-construction helpers should use these defualt_url_options. In addition to setting the :host, you should also force it to use absolute urls by settings this option:

    ActionMailer::Base.default_url_options[:only_path] = false
    

    Also, set the asset host like this:

    config.action_mailer.asset_host = 'http://localhost:3000'
    

    Then just use the image_path helper instead of hand-writing the url, like this:

    
    

    NOTE: Setting default_url_options directly like that is deprecated. Here's the new way to do it:

    config.action_mailer.default_url_options = {
        :host => 'localhost:3000',
        :only_path => false
    }
    

提交回复
热议问题