CSS Images in Email With Rails 3

后端 未结 3 1904
半阙折子戏
半阙折子戏 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:15

    Pass your request host as a parameter to the mailer method, and then pass it from the method to the view. So, for example, your mailer method might look like this (example lifted from rails docs and modified here):

    class UserMailer < ActionMailer::Base
      default :from => "notifications@example.com"
    
      def registration_confirmation(user, host)
        @user = user
        @host = host
        mail(:to => user.email, :subject => "Welcome to My Awesome Site")
      end
    end
    

    You would call it like this:

    def some_action
        UserMailer.registration_confirmation(@user, request.host).deliver
    end
    

    Then in your view, you would just use the @host:

    
    

    This is all assuming the image server is the same as the server running the request. If the image server is hosted elsewhere, you have to output a constant here. You could put something like this in lib/settings.rb:

    module Settings
      IMAGE_HOST = 'superawesome.images.com'
    end
    

    Then in your view, you'd just output the constant there, like this:

    
    

提交回复
热议问题