2012-12-11 5 views
4

스테이징 서버에서 보낸 모든 이메일에 제목 앞에 "[STAGING]"구를 사용합니다. ActionMailer를 사용하여 Rails 3.2에서이 작업을 수행하는 우아한 방법이 있습니까?스테이징 환경에서 ActionMailer 제목을 무시하십시오.

+1

가 실수로 스팸 고객을하지 않는, 그래서 당신은 당신의 자신의 이메일 수신자 데이터베이스를 사용하는지 확인 레일 4.x의 작동합니다. Thats in general ;-) – Roger

답변

16

다음은 an existing answer을 기반으로 한 ActionMailer Interceptor을 사용하여 찾은 멋진 해결책입니다.

# config/initializers/change_staging_email_subject.rb 
if Rails.env.staging? 
    class ChangeStagingEmailSubject 
    def self.delivering_email(mail) 
     mail.subject = "[STAGING] " + mail.subject 
    end 
    end 
    ActionMailer::Base.register_interceptor(ChangeStagingEmailSubject) 
end 
0

실제로 상속은 얻을 정도로 우아합니다.

class OurNewMailer < ActionMailer::Base 

    default :from => '[email protected]', 
      :return_path => '[email protected]' 

    def subjectify subject 
    return "[STAGING] #{subject}" if Rails.env.staging? 
    subject 
    end 
end 

그런 다음 각 우편물을 상속 할 수 있습니다.

# modified from the rails guides 
class Notifier < OurNewMailer 

    def welcome(recipient) 
    @account = recipient 
    mail(:to => recipient.email_address_with_name, :subject => subjectify("Important Message")) 
    end 
end 

나는이 당신을 위해 hopeing 된 것과 같은 깨끗한 생각하지 않는다, 그러나 이것은 조금을 건조합니다.

+1

관심이 있으시면 더 나은 해결책을 찾은 것 같습니다. 제가 제공 한 답을보십시오. –

+0

굉장, 고마워. :) – mwoods79

0

class UserMailer < ActionMailer::Base 
    after_action do 
    mail.subject.prepend('[Staging] ') if Rails.env.staging? 
    end 

    (...) 
end