delete from hateblo.jp where 1=1;

タイトルに意味はありません。

拡張メールアドレスでプロジェクトを指定して起票する

This content is written in Japanese. Please translate into another langage by translator.

対象

  • redmineを管理/運用している人
  • postfix若しくは、qmailgmail(外部公開サーバーのみ)を使っている人
  • メールで起票する設定をしている人

相変わらずニッチな世界で申し訳ないです。

目的

拡張メールアドレスを利用してメールで直接起票する。
postfixqmailgmail等には拡張メールアドレスという機能がある。
その拡張メールアドレスを利用して、プロジェクトを指定してしまおうという企画。

以下にメールを送ると、project-identifierプロジェクトへ起票されるという仕組み。

redmine+project-identifier@test.spamcom

パッチ

以下のようなパッチを当ててください。(revが大分古いですが、最新でも書き変わっていないので使えると思います)

Index: app/models/mail_handler.rb
===================================================================
--- app/models/mail_handler.rb     (revision 3859)
+++ app/models/mail_handler.rb     (working copy)
@@ -158,7 +158,24 @@
     # TODO: other ways to specify project:
     # * parse the email To field
     # * specific project (eg. Setting.mail_handler_target_project)
-    target = Project.find_by_identifier(get_keyword(:project))
+    target = nil
+    d = email['Delivered-To']
+    if !d.nil? && d = d.to_a.first
+      d = d.to_s.gsub(/@.*$/, '')
+      ['+','-'].each do |splitter|
+        project_identifier = ''
+        d.split(splitter).reverse.each do |props|
+          project_identifier = props + project_identifier
+          target = Project.find_by_identifier(project_identifier)
+          break if !target.nil?
+          project_identifier = splitter + project_identifier
+        end
+        break if !target.nil?
+      end
+    end
+    if target.nil?
+      target = Project.find_by_identifier(get_keyword(:project))
+    end
     raise MissingInformation.new('Unable to determine target project') if target.nil?
     target
   end

気が向いたら本家にパッチをSPAMる予定です。

問題点

タイトルが文字化け

メールで起票すると、タイトルが盛大に化けます。
Redmine.JP Blogさんにメールでチケット登録時に題名が文字化けする問題を修正 | Redmine.JP Blogという対応が載っているので、その対応を行えば、見事解決されます。

alias設定すると拡張アドレスが紛失する


以下のように転送設定をしておくと、あるべきヘッダー情報が無くなってしまいます。(サーバーの設定の問題?)

redmine-support@test.spamcom → redmine+project-identifier@test.spamcom

想定するヘッダー情報:

Return-Path: <sender@test.spamcom>
X-Original-To: redmine+project-identifier@test.spamcom
Delivered-To: redmine+project-identifier@test.spamcom
To: redmine-support@test.spamcom
以下略

現実:

Return-Path: <sender@test.spamcom>
X-Original-To: redmine-support@test.spamcom
Delivered-To: redmine-support@test.spamcom
To: redmine-support@test.spamcom
以下略

Postfix設定パラメータ この設定を入れると改善するのかな?(未検証)
対策方法等ご存じの方はご教授願います。

早速追記:
aliasをlocal配送で行う場合(/etc/aliases)は以下の設定で回避できます

redmine-support:   "|grep -v Delivered-To|/usr/sbin/sendmail -i redmine+project-identifier@test.spamcom"

ただ、この実装は注意しないとループを発生させるので、細心の注意を払うようにしてください。