2011-06-20 62 views
5

我想邀請我的應用程序來自邀請者而不是系統電子郵件地址。我如何覆蓋devise.rb中的config.mailer_sender?在devise_invitable中忽略郵件?

我在我的郵件中有這個,並且已經確認它已經被調用,但是它不覆蓋:from。注意:這是一個私有方法,我嘗試了它作爲一個公共方法,沒有任何效果。

private 

def headers_for(action) 
    if action == :invitation_instructions 
    headers = { 
     :subject  => "#{resource.invited_by.full_name} has invited you to join iTourSmart", 
     :from   => resource.invited_by.email, 
     :to   => resource.email, 
     :template_path => template_paths 
    } 
    else 
    headers = { 
     :from   => mailer_sender(devise_mapping), 
     :to   => resource.email, 
     :template_path => template_paths 
    } 
    end 

    if resource.respond_to?(:headers_for) 
    headers.merge!(resource.headers_for(action)) 
    end 

    unless headers.key?(:reply_to) 
    headers[:reply_to] = headers[:from] 
    end 

    headers 
end 
+0

該動作不應該是':invitation_instructions'嗎?我相信你有一個額外的s,因爲你的代碼說:'invitations_instructions' – swrobel

+0

它應該是,謝謝,但錯字是一致的,所以這不是問題。我將編輯這個問題來澄清。 –

回答

4

查看my answer到類似的問題,這可能會有所幫助。

編輯:所以你似乎需要在你的資源類中定義一個public headers_for方法。

解決方案:在User.rb中放入一些此方法的版本,確保它是公開的。

def headers_for(action) 
    action_string = action.to_s 
    case action_string 
    when "invitation" || "invitation_instructions" 
    {:from => '[email protected]'} 
    else 
    {} 
    end 
end 

因爲Devise::Mailer會嘗試合併哈希值,所以必須返回哈希值。

+0

我確實看過 - 感謝和抱歉,我錯過了早些時候,我仍然失去了一些東西。即使編輯了上面的代碼來添加一個':return_path'如果':invitation_instructions'並且它在接收到的電子郵件中是正確的,但':from'仍然是系統默認的。 –

+0

你是否驗證它是否通過條件'if action ==:invitation_instructions'? – David

+0

我做到了。我可以更改該塊中的任何其他參數,並且發送的電子郵件中存在更改。 –

5

沒有任何黑客更好的解決方案/猴補丁將是: 例如,在你的模型:

def invite_and_notificate_member user_email 
    member = User.invite!({ email: user_email }, self.account_user) do |u| 
    u.skip_invitation = true 
    end 
    notificate_by_invitation!(member) 
end 

def notificate_by_invitation! member 
    UserMailer.invited_user_instructions(member, self.account_user, self.name).deliver 
end 

在郵件:

def invited_user_instructions(user, current_user, sa) 
    @user = user 
    @current_user = current_user 
    @sa = sa 
    mail(to: user.email, subject: "#{current_user.name} (#{current_user.email}) has invited you to the #{sa} account ") 
    end 

所以,你可以把任何主題/數據在郵件的正文中。

祝你好運!

3

看看devise_invitable wiki

class User < ActiveRecord::Base 
    #... regular implementation ... 

    # This method is called interally during the Devise invitation process. We are 
    # using it to allow for a custom email subject. These options get merged into the 
    # internal devise_invitable options. Tread Carefully. 
    # 
def headers_for(action) 
    return {} unless invited_by && action == :invitation_instructions 
    { subject: "#{invited_by.full_name} has given you access to their account" } 
    end 
end