2012-07-11 40 views
0

在我的Rails應用程序中,我想暫時停止向特定用戶發送電子郵件(例如,當我由於配額而跳票時),直到用戶確認他能夠再次收到電子郵件。在ActionMailer中暫停發送電子郵件

我有一個所有郵件類的公共超類。在發送郵件之前,我總是調用方法setup_email

最好的地方叫@user.mail_suspended?

下面是一些簡單的示例應用程序,我使用Rails的2.3:

# Common super class for all Mailers 
class ApplicationMailer < ActionMailer::Base 

    protected 
    def setup_mail(user) 
     @recipients = user.email 
     @from = ... 
    end 
end 

# Specific Mailer for User model 
class UserMailer < ApplicationMailer 

    def message(user, message) 
    setup_mail(user) 
    @subject = "You got new message" 
    @body[:message] = message 
    end 
end 

# Use the UserMailer to deliver some message 
def MessagesController < ApplicationController 

    def create 
    @message = Message.new(params[:message]) 
    @message.save 
    UserMailer.deliver_message(@message.user, @message) 
    redirect_to ... 
    end 
end 

回答

1

我解決了這個通過設置ActionMailer::Base.perform_deliveries爲false:普遍

def setup_mail(user) 
    email = user.default_email 
    if email.paused? 
    ActionMailer::Base.perform_deliveries = false 
    logger.info "INFO: suspended mail for #{user.login} to #{email.email})" 
    else 
    ActionMailer::Base.perform_deliveries = true 
    end 
    # other stuff here 
end 
0

我不會設置perform_deliveries,只是每消息,例如

after_filter :do_not_send_if_old_email 

def do_not_send_if_old_email 
    message.perform_deliveries = false if email.paused? 
    true 
end 
相關問題