接收電子郵件¶
- 目錄
- 接收電子郵件
Redmine 可以設定為允許透過電子郵件建立議題或評論。它還可以識別和合併對論壇訊息的電子郵件回覆。
設定¶
您可以透過以下其中一種方式設定 Redmine 接收電子郵件
- 從電子郵件伺服器轉寄電子郵件
- 優點:適用於遠端郵件伺服器,電子郵件會立即處理,速度快(無需重新載入環境)
- 缺點:需要在郵件傳輸代理程式(例如 Postfix、Sendmail...)上進行一些設定
- 從 IMAP 或 POP3 伺服器擷取電子郵件
- 優點:易於設定,無需設定 MTA,適用於遠端郵件伺服器
- 缺點:電子郵件不會立即處理(需要新增 cron 作業以定期讀取電子郵件)
- 從標準輸入讀取電子郵件
- 優點:適用於測試目的
- 缺點:速度慢(每次讀取電子郵件時都會重新載入環境),需要在 MTA 上進行一些設定
從電子郵件伺服器轉寄電子郵件¶
可以使用獨立腳本從您的郵件伺服器轉寄收到的電子郵件。
此腳本從標準輸入讀取原始電子郵件,並透過 HTTP 請求將其轉寄到 Redmine。
它可以在您的 Redmine 目錄中找到:extra/mail_handler/rdm-mailhandler.rb
。
為了使用它,您必須啟用接收電子郵件的 API
前往「應用程式設定」->「收件匣」,勾選「啟用 WS 以接收電子郵件」並輸入或產生一個密鑰。
將 rdm-mailhandler.rb
複製到您的郵件伺服器,確保其權限允許執行(chmod +x rdm-mailhandler.rb
),並設定您的 MTA(郵件傳輸代理程式)。
用法
Usage: rdm-mailhandler.rb [options] --url=<Redmine URL> --key=<API key> Required arguments: -u, --url URL URL of the Redmine server -k, --key KEY Redmine API key General options: --key-file FILE full path to a file that contains your Redmine API key (use this option instead of --key if you don't want the key to appear in the command line) --no-check-certificate do not check server certificate --certificate-bundle FILE certificate bundle to use -h, --help show this help -v, --verbose show extra information -V, --version show version information and exit User and permissions options: --unknown-user ACTION how to handle emails from an unknown user ACTION can be one of the following values: * ignore: email is ignored (default) * accept: accept as anonymous user * create: create a user account --no-permission-check disable permission checking when receiving the email --default-group GROUP add created user to GROUP (none by default) GROUP can be a comma separated list of groups --no-account-notice don't send account information to the newly created user --no-notification disable email notifications for the created user Issue attributes control options: --project-from-subaddress ADDR select project from subaddress of ADDR found in To, Cc, Bcc headers -p, --project PROJECT identifier of the target project -s, --status STATUS name of the target status -t, --tracker TRACKER name of the target tracker --category CATEGORY name of the target category --priority PRIORITY name of the target priority --assigned-to ASSIGNEE assignee (username or group name) --fixed-version VERSION name of the target version --private create new issues as private -o, --allow-override ATTRS allow email content to set attributes values ATTRS is a comma separated list of attributes or 'all' to allow all attributes to be overridable (see below for details)
有關可用於 --allow-override
選項的值清單,請參閱 議題屬性。
範例
# No project specified. Emails MUST contain the 'Project' keyword: rdm-mailhandler.rb --url http://redmine.domain.foo --key secret # Fixed project and default tracker specified, but emails can override # both tracker and priority attributes: rdm-mailhandler.rb --url https://domain.foo/redmine --key secret \\ --project foo \\ --tracker bug \\ --allow-override tracker,priority
以下是 Postfix 本機別名項目的範例
foo: "|/path/to/rdm-mailhandler.rb --url http://redmine.domain --key secret --project foo"
此行應放在 aliases 檔案中,該檔案通常在 /etc/aliases
中指定。如果位置未知,請使用命令 postconf alias_maps
找出。更新 aliases 檔案後,請務必執行 newaliases
以通知 Postfix 新項目。
如果您的網域設定為虛擬信箱映射(以便您使用 /etc/postfix/virtual_mailbox_maps 以 user@example.com /path/example.com/user
的形式進行映射),則您應該
- 在
/etc/virtual
中建立如下映射:foo@example.org foo
- 修改
/etc/postfix/main.cf
以指定傳輸文件:transport_maps = hash:/etc/postfix/transport
- 在傳輸文件中添加一行,例如:
foo@example.org local:
說明: - 當您為網域定義 virtual_mailbox_maps 時,預設傳輸方式為 virtual,這表示在 /etc/postfix/virtual
中指定本地別名將會失敗(出現「未知用戶」錯誤)。要解決此問題,我們可以透過為相關的電子郵件地址指定本地傳輸來覆寫預設傳輸方式,這表示本地別名將會正確解析,並且您的腳本將會被執行。
從 IMAP 伺服器擷取電子郵件¶
可以使用 rake 工作(redmine:email:receive_imap
)從 IMAP 伺服器擷取收到的電子郵件。當您從 cron 作業執行 rake 命令時,您可以在 rake 命令中加入 -f /path/to/redmine/appdir/Rakefile
參數,因為否則將找不到 rakefile。以下是一個 cron 文件範例,每 30 分鐘擷取一次郵件
*/30 * * * * redmineuser rake -f /path/to/redmine/appdir/Rakefile redmine:email:receive_imap RAILS_ENV="production" host=imap.foo.bar username=redmine@somenet.foo password=xxx
如果您的設定正常運作,但您收到來自 cron 精靈的郵件,您可以透過添加 --silent 參數來抑制 rake 命令的輸出。這應該可以阻止 cron 在每次執行命令時發送郵件。
*/30 * * * * redmineuser rake -f /path/to/redmine/appdir/Rakefile --silent redmine:email:receive_imap RAILS_ENV="production" host=imap.foo.bar username=redmine@somenet.foo password=xxx
該命令必須在您的 cronfile 中佔用一行。另請參閱以下其他範例,這些範例僅顯示 rake 命令,不包含 -f
選項和 cron 部分。
對於以 Windows 作為伺服器的環境,可以使用 pycron 來排程擷取工作。
您可能需要在機器上開啟防火牆,允許連往 IMAP 埠 143 的輸出 TCP 連線。
可用的 IMAP 選項
host=HOST IMAP server host (default: 127.0.0.1) port=PORT IMAP server port (default: 143) ssl=SSL Use SSL? (default: false) starttls=STARTTLS Use STARTTLS? (default: false) username=USERNAME IMAP account password=PASSWORD IMAP password folder=FOLDER IMAP folder to read (default: INBOX) move_on_success=MAILBOX move emails that were successfully received to MAILBOX instead of deleting them move_on_failure=MAILBOX move emails that were ignored to MAILBOX
使用者和權限選項
unknown_user=ACTION how to handle emails from an unknown user ACTION can be one of the following values: ignore: email is ignored (default) accept: accept as anonymous user create: create a user account no_permission_check=1 disable permission checking when receiving the email no_account_notice=1 disable new user account notification default_group=foo,bar adds created user to foo and bar groups
問題屬性控制選項
project=PROJECT identifier of the target project project_from_subaddress=ADDR select project from subaddress of ADDR found in To, Cc, Bcc headers status=STATUS name of the target status tracker=TRACKER name of the target tracker category=CATEGORY name of the target category priority=PRIORITY name of the target priority assigned_to=ASSIGNEE assignee (username or group name) fixed_version=VERSION name of the target version private create new issues as private allow_override=ATTRS allow email content to set attributes values ATTRS is a comma separated list of attributes or 'all' to allow all attributes to be overridable
有關可用於 allow-override
選項的值列表,請參閱問題屬性。
rake 命令的範例
# No project specified. Emails MUST contain the 'Project' keyword: rake redmine:email:receive_imap RAILS_ENV="production" \\ host=imap.foo.bar username=redmine@somenet.foo password=xxx # Fixed project and default tracker specified, but emails can override # both tracker and priority attributes: rake redmine:email:receive_imap RAILS_ENV="production" \\ host=imap.foo.bar username=redmine@somenet.foo password=xxx ssl=1 \\ project=foo \\ tracker=bug \\ allow_override=tracker,priority # Move successful emails to the 'read' mailbox and failed emails to # the 'failed' mailbox rake redmine:email:receive_imap RAILS_ENV="production" \\ host=imap.foo.bar username=redmine@somenet.foo password=xxx \\ move_on_success=read move_on_failure=failed
被忽略的電子郵件會被標記為「已讀取」,但不會從 IMAP 伺服器中刪除 - 這些郵件包括未知使用者、未知專案和來自 Redmine 發送帳戶的電子郵件。
allow_override 選項不僅用於覆寫提供給 rake 的預設值,還用於郵件中的每個屬性。如果您想覆寫郵件中的追蹤器,則必須將 allow_override=tracker 作為參數添加。
從 POP3 伺服器擷取電子郵件¶
可以使用 rake 工作(redmine:email:receive_pop3
)從 POP3 伺服器擷取收到的電子郵件。
可用的 POP3 選項
host=HOST POP3 server host (default: 127.0.0.1) port=PORT POP3 server port (default: 110) username=USERNAME POP3 account password=PASSWORD POP3 password (use '' if it fails, ex: 'my-password') apop=1 use APOP authentication (default: false) delete_unprocessed=1 delete messages that could not be processed successfully from the server (default behaviour is to leave them on the server)
有關問題屬性控制選項,請參閱上方IMAP rake 工作。
從標準輸入讀取電子郵件¶
可以使用 rake 工作(redmine:email:read
)從標準輸入讀取單封原始電子郵件。
有關問題屬性控制選項,請參閱上方IMAP rake 工作。
範例
# No project specified. Emails MUST contain the 'Project' keyword: rake redmine:email:read RAILS_ENV="production" < raw_email # Fixed project and default tracker specified, but emails can override # both tracker and priority attributes: rake redmine:email:read RAILS_ENV="production" \\ project=foo \\ tracker=bug \\ allow_override=tracker,priority < raw_email
allow_override 選項不僅用於覆寫提供給 rake 的預設值,還用於郵件中的每個屬性。如果您想覆寫郵件中的追蹤器,則必須將 allow_override=tracker 作為參數添加。
允許未知使用者透過電子郵件建立問題¶
Redmine 有一個功能,可以接受來自未知使用者的電子郵件。為了使用此功能,必須加入一個額外的參數
unknown_user=ACTION how to handle emails from an unknown user where ACTION can be one of the following values: ignore: the email is ignored (default) accept: the sender is considered as an anonymous user and the email is accepted create: a user account is created for the sender (username/password are sent back to the user) and the email is accepted
權限必須與所選選項一致。例如,如果您選擇「建立」,則「非成員」角色必須具有「新增問題」權限,以便未知使用者可以透過電子郵件建立問題。如果您選擇「接受」,則「匿名使用者」角色必須具有此權限。
如果您透過 rake 工作收到電子郵件,則 unknown-user 選項必須寫成
unknown_user=[ignore|accept|create]
您可以使用「no_permission_check」選項停用權限檢查
no_permission_check=1 disable permission checking when receiving the email
這與「unknown-user」一起使用,可以讓任何人將電子郵件提交到私人專案。例如
rdm-mailhandler.rb --unknown-user accept --no-permission-check --project=foo
將允許任何人將電子郵件提交到私人專案「foo」。
TODO: 這是否正確?這與 no_permission_check
選項有關嗎?
從 Redmine 0.9 版開始,專案不一定要公開,但必須取消勾選「管理」->「設定」->「驗證」分頁中的「需要驗證」選項。
如果您不希望 rdm-mailhandler 向每個新建立的使用者傳送「新帳戶通知電子郵件」,則必須新增 --no-account-notice
選項。此選項已於 2.3.0 版中實作,請參閱問題 #11498。以下是一個範例:
rdm-mailhandler.rb --unknown-user accept --no-permission-check --project=foo --no-account-notice
使用 Rufus Scheduler 排程電子郵件接收¶
您可以使用 Rufus Scheduler 在與 Redmine 相同的程序中執行 Rake 工作,而不必使用 cron 來觸發 Rake 工作以擷取收到的電子郵件。
這比從 cron(或 Windows 上的「工作排程器」)呼叫 Rake 工作更節省資源且速度更快。
為此,請安裝 rufus-scheduler gem。
gem install rufus-scheduler
在 /config/initializers/ 中建立一個 what_you_want.rb 檔案,並將以下內容放入您的檔案中。(這裡以 POP3 帳戶為例。請使用您自己的參數和工作進行變更。)
require 'rubygems' require 'rake' require 'rufus-scheduler' load File.join(Rails.root, 'Rakefile') ENV['host']='pop.toto.com' ENV['port']='110' ENV['ssl']='SSL' ENV['username']='redmine@toto.com' ENV['password']='azerty123456' scheduler = Rufus::Scheduler.new # Check emails every 10 mins scheduler.interval '10m' do task = Rake.application['redmine:email:receive_pop3'] task.reenable task.invoke end
重新啟動您的 Redmine 執行個體,您的收件匣就會按排程時間擷取電子郵件。
您可以查看 Rufus-Scheduler 以瞭解排程語法。
運作方式¶
Redmine 在收到電子郵件時,會使用電子郵件的「寄件者」地址來尋找對應的使用者。從未知或鎖定的使用者收到的電子郵件將會被忽略。
如果電子郵件主旨包含類似「回覆:[xxxxxxx #123]」或「[#123]」的內容,則該電子郵件將會被視為回覆,並將在問題 #123 中新增一則筆記。否則,將會建立一個新的問題。
請注意,若要建立問題,必須提供所有必要的自訂欄位。如果缺少這些欄位,將無法建立問題。或者,您可以確保每個自訂欄位都有一個預設值,以便在建立問題時使用。
目標專案¶
您可以在接收電子郵件時使用 project
選項來指定目標專案。這應該是專案的識別碼,而不是名稱。您可以在網址中輕鬆找到識別碼。
如果您沒有使用這個選項,使用者必須在電子郵件內文中指定要將問題新增到哪個專案。這可以透過在電子郵件內文中插入一行類似「專案:foo」的文字來完成。
範例(電子郵件內文)
This is a new issue that will be added to project foo. Here we have the issue description [...] Project: foo
您可以使用 project
選項設定預設專案,並讓使用者在接收電子郵件時使用 allow-override
選項覆寫這個預設專案。
範例
# Create issues on project foo by default rake redmine:email:receive_imap [...] project=foo allow_override=project
當然,系統會檢查使用者權限,如果傳送此電子郵件的使用者沒有權限將問題新增到專案 foo,則會忽略此電子郵件。
請確保目標專案沒有使用沒有預設值的必要自訂欄位,否則將無法建立問題。
從電子郵件子地址設定目標專案¶
此功能已於 3.2.0 版中推出(SVN 版本 r14687)。建議您為 Redmine 執行個體的所有收件匣使用一個信箱,而不是每個專案都使用一個信箱。當收到傳送到 redmine@somenet.foo 的電子郵件時,傳送到 redmine+foo@somenet.foo 的電子郵件會導致問題被新增到識別碼為 foo 的專案中。
若要啟用此功能,請在指令中新增 project_from_subaddress=redmine@somenet.foo
,例如:
rake redmine:email:receive_imap [...] project_from_subaddress=redmine@somenet.foo
如果指令列中包含專案,則該專案將成為傳送到 redmine@somenet.foo
的電子郵件的預設專案。因此,使用以下指令:
rake redmine:email:receive_imap [...] project=bar project_from_subaddress=redmine@somenet.foo
傳送到 redmine@somenet.foo
或 redmine+bar@somenet.foo
的電子郵件會在專案 bar 中產生議題;傳送到 redmine+foo@somenet.foo
的電子郵件會進入專案 foo。
根據 RFC 3598 和 5233,部分電子郵件供應商支援子地址。 如果不支援,將郵件地址轉發到「中央」地址可能會有效(在我的情況下有效),例如設定一個電子郵件地址,將所有郵件從 redmine+foo@somenet.foo
、redmine+bar@somenet.foo
等轉發到 redmine@somenet.foo
。 這很容易管理,並且不需要從多個帳戶(每個帳戶都有自己的帳戶憑證)擷取電子郵件。
議題屬性¶
根據您在接收電子郵件時使用的選項(請參閱 allow-override
選項),使用者可以在提交議題時覆寫某些屬性。
注意:在 Redmine 3.2.0 之前(#20543),某些屬性「始終」可以覆寫,但這一點沒有記錄在案。 此行為已變更,所有屬性都必須使用 allow_override
選項明確列出才能覆寫。 對於那些希望所有屬性都可以覆寫的人,現在可以使用 allow_override=all
(#20543)。
配置 allow_override
後,您可以透過在電子郵件內文中使用適當的關鍵字來覆寫這些屬性。
範例關鍵字清單
專案
追蹤器
狀態
類別
優先順序
指派給
開始日期
到期日
目標版本
預估時數
完成率
<自訂欄位名稱>
可用的值是上下文的值。 例如,可用「狀態」(針對此追蹤器和此專案)是以當地語系化的語言顯示的標籤,與使用者介面或系統預設語言中顯示的完全相同(即使包含空格,也不需要引號)。
範例(電子郵件內文)
This is a new issue that overrides a few attributes [...] Project: foo Tracker: Bug Priority: Urgent Status: Resolved
關鍵字的格式與 allow_override
選項允許的屬性值不同
專案
追蹤器
狀態
類別
優先順序
assigned_to
start_date
due_date
fixed_version
(亦即目標版本)estimated_hours
done_ratio
<custom_field_name>
可以指定多個屬性值,以便只允許使用某些關鍵字。
範例
# Allow overriding project, tracker, status & priority rake redmine:email:receive_imap [...] allow_override=project,tracker,status,priority
關注者¶
如果傳送電子郵件的使用者具有「新增議題關注者」權限,則電子郵件的「收件者」或「副本」欄位中的使用者會自動新增為已建立議題的關注者。
僅在建立議題時才會新增關注者。 回覆時會忽略「收件者」或「副本」欄位。 請參閱 #7017 和 #8009。
電子郵件格式和附件¶
Redmine 會嘗試使用電子郵件的純文字部分來填寫議題的描述。
如果收到僅限 HTML 的電子郵件,則會從其內文中移除 HTML 標籤。
電子郵件附件會自動附加到議題,除非其大小超過應用程式設定中定義的最大附件大小。
截斷電子郵件¶
管理員的設定可以用來自動截斷電子郵件,例如在論壇回覆中消除引用的訊息。 若要執行此操作,請在「電子郵件通知」設定中,將外寄電子郵件標頭設定為類似 --請回覆至此行以上--
的內容。 然後在「接收電子郵件」設定中,將相同的行輸入「截斷這些行之後的電子郵件」方塊中。(也可以允許截斷 regex)
另請參閱¶
- 子地址處理程式的 Mailhandler Frontend:如何在 Redmine <3.2 中實作子地址
由 Joan Cervan 於 大約 1 年 前更新 · 93 個版本