在Rails3 / ActionMailer中设置Message-ID邮件头 [英] Set the Message-ID mail header in Rails3 / ActionMailer

查看:109
本文介绍了在Rails3 / ActionMailer中设置Message-ID邮件头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改 Message-ID 标头,该标头是使用ActionMailer从Ruby on Rails v3应用程序发送的电子邮件的标头部分。

I would like to alter the Message-ID header that is in the header portion of an email sent from a Ruby on Rails v3 application using ActionMailer.

我在本地主机上使用Sendmail进行邮件传递。

I am using Sendmail on localhost for mail delivery.

我是否在Sendmail或ActionMailer中配置它?

Do I configure this in Sendmail or ActionMailer?

在哪里进行配置(如果是ActionMailer): config / 文件夹中的文件还是app / mailers /文件夹中的文件?

Where do I configure this (if it is ActionMailer): a file in config/ folder or a file in app/mailers/ folder?

推荐答案

我知道了。最简单的方法是在邮件程序类文件顶部使用 default 方法。

I figured this out. The easiest way to do is to use the default method at the top of the mailer class file.

示例:

require 'digest/sha2'
class UserMailer < ActionMailer::Base
  default "Message-ID"=>"#{Digest::SHA2.hexdigest(Time.now.to_i.to_s)}@yourdomain.com"

  # ... the rest of your mailer class
end

但是,我发现这很难测试,所以我写了一个私有方法,并使用 sent_at 时间而不是 Time.now

However, I found this difficult to test, so I wrote a private method and used the sent_at time instead of Time.now:

def message_id_in_header(sent_at=Time.now)
  headers["Message-ID"] = "#{Digest::SHA2.hexdigest(sent_at.to_i.to_s)}@yourdomain.com"
end

在调用 mail 方法之前先调用该方法。这样可以轻松地从测试中传递 sent_at 参数,并验证 email.encoded 中的匹配项。

And I simply called that method before calling the mail method. This made it easy to pass a sent_at parameter from my test and verify a match in email.encoded.

这篇关于在Rails3 / ActionMailer中设置Message-ID邮件头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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