在数据库中记录ActionMailer发送的电子邮件的最佳方法? [英] Best way to log ActionMailer sent emails in a database?

查看:104
本文介绍了在数据库中记录ActionMailer发送的电子邮件的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要记录通过ActionMailer发送的电子邮件,仅一个简单的文本文件是不够的.我需要将日志存储在ActiveRecord模型中.电子邮件发送性能可能会受到影响,但是在这种情况下,这是最好的选择(因为日志随后会由应用程序定期访问).

I need to log emails sent through ActionMailer and a simple text file isn't enough. I'd need to store the logs in an ActiveRecord model. Email sending performance will likely take a hit, but in this case, it's the best alternative (since the logs are afterwards routinely accessed by the app).

我还需要记录实际发送的内容和相关的错误,而不仅仅是我的控制器传递给ActionMailer的内容(即,最低级别的实现).

I'd also need to log what was actually sent and the errors associated, not just what my controller passes to ActionMailer (ie. the lowest level implementation possible).

config.action_mailer.logger似乎是劫持Rails已经安装的机器的理想方法.但是,据我了解,Log4r和默认的Ruby记录器只能输出到文本文件.

config.action_mailer.logger seems an ideal way to hijack the machinery already in place by Rails to do so. However, from my understanding, Log4r and the default Ruby logger can only output to text files.

有什么建议吗?

谢谢!

推荐答案

有两种方法可以解决您的问题,因为您也想存储邮件的内容.
在此之前,邮件的典型内容是:

There are 2 ways to solve your problem, since you want to store the contents of the mail as well.
Before that, the typical contents of a mail are:

Sent mail to 123@gmail.com

From: abc@xyz.com
To: 123@gmail.com
Subject: Blah
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8

Hi, yourself.

这些概述如下

  1. 根据需要的方式,可以定期(例如,一个小时,一天或一周)将邮件存储在数据库中.这样,您可以作为后台作业运行它,它将查找日志文件并提取所需的内容.

  1. Storing the mails in the database can be done on a periodical basis(say an hour, a day or a week) depending on the way you want it. This way you can run it as a background job, which will look up the log file and extract the contents that you want.

更好的方法是,将<回调>回调函数附加到邮件功能,这样,无论何时发送邮件,即调用传递功能,您的回调函数被单独调用,从尾端到特定点查找日志,然后提取内容,然后将其存储在相同的位置.

A much better way of doing the same could be, that you attach a callback function to the mailing function, so that whenever you send a mail i.e. you call the deliver function, your callback function gets called on its own, looks up the log from the tail end to a certain point and then extracts the content and then stores it in the same.

回调函数可以是一个简单的ruby脚本,它可以打开日志并读取其内容,然后您就可以随意使用它并以所需的方式对其进行存储.

The callback function can be a simple ruby script, that opens the log and reads its contents and then you can play around with it and store it in the way you want.

这篇关于在数据库中记录ActionMailer发送的电子邮件的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆