我的应用程序有一个UserMailer,其中包含几种邮件方法,其中大多数是通过延迟作业发送的。
我想要的是一个数据库表,它在每次发送邮件时都会创建一条记录。该应用程序使用sendgrid进行发送,但我不信任sendgrid 100%,并希望在该应用程序上保留记录以进行调试。
有没有一种高级方法可以全局捕获邮件的发送时间并将其记录在数据库中,而不会被迫向everer mailer方法添加逻辑?
谢谢
最佳答案
我通过使用带有“ to”和“ guid”字符串字段的电子邮件模型来解决此问题。
def self.deliver(which, to, guid, *args)
transaction do
if self.where(:email => to, :guid => guid).first.nil?
if record = Email.create(:email => to, :guid => guid, :template => which) and !record.new_record?
Mailer.send("deliver_#{which}", to, *args)
end
end
end
end
这是针对Rails 2.3邮件程序的,因此对Rails 3的调用将有所不同,但是核心概念仍然适用。代替直接调用邮件程序,您将调用:
Email.deliver(email_name, receipient, some_guid_you_generate_for_this_email, *args_to_be_sent_to_mailer)
这将检查数据库,以确保您不会将同一封电子邮件重新发送到给定的地址,并将保留所有以后可以检查的已发送电子邮件的记录。
关于ruby-on-rails - Rails,如何在每次发送电子邮件时保持数据库表日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8031156/