| 5 | |
| 6 | == Add or change email header |
| 7 | Some projects like to add or change special email headers to notification emails. |
| 8 | |
| 9 | For example by default Trac sends notifications with email header `Precedence: bulk`, but some projects prefer to use `Precedence: list`: |
| 10 | |
| 11 | 1. Create a [TracDev/PluginDevelopment#Singlefileplugins single file plugin] that implements [wiki:TracDev/PluginDevelopment/ExtensionPoints/trac.notification.api.IEmailDecorator IEmailDecorator]: |
| 12 | {{{#!python |
| 13 | # -*- coding: utf-8 -*- |
| 14 | # |
| 15 | # Copyright (C) 2017 Edgewall Software |
| 16 | # All rights reserved. |
| 17 | # |
| 18 | # This software is licensed as described in the file COPYING, which |
| 19 | # you should have received as part of this distribution. The terms |
| 20 | # are also available at http://trac.edgewall.org/wiki/TracLicense. |
| 21 | # |
| 22 | # This software consists of voluntary contributions made by many |
| 23 | # individuals. For the exact contribution history, see the revision |
| 24 | # history and logs, available at http://trac.edgewall.org/log/. |
| 25 | |
| 26 | from trac.core import Component, implements |
| 27 | from trac.notification.api import IEmailDecorator |
| 28 | from trac.notification.mail import set_header |
| 29 | |
| 30 | class ListPrecedenceEmailDecorator(Component): |
| 31 | |
| 32 | implements(IEmailDecorator) |
| 33 | |
| 34 | # IEmailDecorator methods |
| 35 | |
| 36 | def decorate_message(self, event, message, charset): |
| 37 | set_header(message, 'Precedence', 'list', charset) |
| 38 | }}} |
| 39 | |
| 40 | See also: #7499 |
| 41 | |
| 42 | == Add ticket fields to email headers |
| 43 | Some projects need email headers with the current or changed values of ticket fields: |
| 44 | |
| 45 | 1. Create a [TracDev/PluginDevelopment#Singlefileplugins single file plugin] that implements [wiki:TracDev/PluginDevelopment/ExtensionPoints/trac.notification.api.IEmailDecorator IEmailDecorator]: |
| 46 | {{{#!python |
| 47 | # -*- coding: utf-8 -*- |
| 48 | # |
| 49 | # Copyright (C) 2017 Edgewall Software |
| 50 | # All rights reserved. |
| 51 | # |
| 52 | # This software is licensed as described in the file COPYING, which |
| 53 | # you should have received as part of this distribution. The terms |
| 54 | # are also available at http://trac.edgewall.org/wiki/TracLicense. |
| 55 | # |
| 56 | # This software consists of voluntary contributions made by many |
| 57 | # individuals. For the exact contribution history, see the revision |
| 58 | # history and logs, available at http://trac.edgewall.org/log/. |
| 59 | |
| 60 | from trac.config import ListOption |
| 61 | from trac.core import Component, implements |
| 62 | from trac.notification.api import IEmailDecorator |
| 63 | from trac.notification.mail import set_header |
| 64 | |
| 65 | class TicketFieldEmailDecorator(Component): |
| 66 | |
| 67 | implements(IEmailDecorator) |
| 68 | |
| 69 | smtp_x_trac_ticket_headers = ListOption('notification', |
| 70 | 'smtp_x_trac_ticket_headers', 'CHANGED', |
| 71 | doc="""Possible values: ''ALL'', ''CHANGED'' or comma-separated |
| 72 | list of ticket properties. |
| 73 | |
| 74 | Those properties are put into the e-mail headers when notifying |
| 75 | about ticket updates.""") |
| 76 | |
| 77 | # IEmailDecorator methods |
| 78 | |
| 79 | def decorate_message(self, event, message, charset): |
| 80 | if event.realm != 'ticket' or event.category == 'batchmodify': |
| 81 | return |
| 82 | def header(value, *parts): |
| 83 | name = '-'.join(part.capitalize() for part in parts) |
| 84 | set_header(message, name, value, charset) |
| 85 | fields = list(self.smtp_x_trac_ticket_headers) |
| 86 | prefix = 'X-Trac-Ticket' |
| 87 | if fields == ['CHANGED']: |
| 88 | for item, change in event.changes['fields'].iteritems(): |
| 89 | for type, value in change.iteritems(): |
| 90 | header(value, prefix, item, type) |
| 91 | elif fields == ['ALL']: |
| 92 | for item in event.target.values: |
| 93 | header(event.target.values[item], prefix, item) |
| 94 | else: |
| 95 | for item in fields: |
| 96 | header(event.target.values[item], prefix, item) |
| 97 | }}} |
| 98 | 1. Set the configuration `[notification] smtp_x_trac_ticket_headers` to `ALL` or to a list of ticket field names. |
| 99 | |
| 100 | See also: #6516 |