Edgewall Software

Changes between Version 4 and Version 5 of CookBook/Notification/Email


Ignore:
Timestamp:
Mar 12, 2017, 2:57:19 PM (7 years ago)
Author:
Peter Suter
Comment:

ListPrecedenceEmailDecorator and TicketFieldEmailDecorator

Legend:

Unmodified
Added
Removed
Modified
  • CookBook/Notification/Email

    v4 v5  
    33
    44TracNotification customizations can often be implemented with a short plugin. Some custom examples are given on this page.
     5
     6== Add or change email header
     7Some projects like to add or change special email headers to notification emails.
     8
     9For example by default Trac sends notifications with email header `Precedence: bulk`, but some projects prefer to use `Precedence: list`:
     10
     111. 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
     26from trac.core import Component, implements
     27from trac.notification.api import IEmailDecorator
     28from trac.notification.mail import set_header
     29
     30class 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
     40See also: #7499
     41
     42== Add ticket fields to email headers
     43Some projects need email headers with the current or changed values of ticket fields:
     44
     451. 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
     60from trac.config import ListOption
     61from trac.core import Component, implements
     62from trac.notification.api import IEmailDecorator
     63from trac.notification.mail import set_header
     64
     65class 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}}}
     981. Set the configuration `[notification] smtp_x_trac_ticket_headers` to `ALL` or to a list of ticket field names.
     99
     100See also: #6516
    5101
    6102== Attachments in Notification Emails