Edgewall Software

Changes between Initial Version and Version 1 of TracDev/PluginDevelopment/ExtensionPoints/trac.notification.api.IEmailDecorator


Ignore:
Timestamp:
Dec 6, 2014, 1:03:02 PM (9 years ago)
Author:
Peter Suter
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • TracDev/PluginDevelopment/ExtensionPoints/trac.notification.api.IEmailDecorator

    v1 v1  
     1== Extension Point : ''IEmailDecorator'' ==
     2
     3||'''Interface'''||''IEmailDecorator''||'''Since'''||[wiki:TracDev/ApiChanges/1.1.3#IEmailDecorator 1.1.3]||
     4||'''Module'''||''trac.notification''||'''Source'''||[source:trunk/trac/notification/api.py#/IEmailDecorator api.py]||
     5
     6An ''IEmailDecorator'' decorates [TracNotification notification] email, usually by adding additional email headers.
     7
     8== Purpose ==
     9
     10Trac provides an extendible and flexible notification system, that historically has sent emails formatted by some fixed logic.
     11
     12Now plugins can implement different [wiki:trac.notification.api.INotificationDistributor transports] and [wiki:trac.notification.api.INotificationFormatter formatters], decoupling the formatting logic from the transport implementation. IEmailDecorator allows also decoupling e.g. email title formatting and other header manipulation both from transport-neutral formatting logic and the email transport implementation.
     13
     14== Usage ==
     15
     16Implementing the interface follows the standard guidelines found in [wiki:TracDev/ComponentArchitecture] and of course [wiki:TracDev/PluginDevelopment].
     17
     18The `decorate_message()` decorates the email message as appropriate. The parameters are:
     19* `event`: A `trac.notification.api.NotificationEvent` instance describing the event about which the recipients should be notified.
     20* `message`: An `email.message.Message` to decorate.
     21* `charset`: A `email.charset.Charset` to use for headers.
     22
     23== Examples ==
     24
     25The following example adds a custom `X-Trac-Notification-Author` header to emails:
     26
     27{{{#!python
     28from trac.core import *
     29from trac.notification.api import IEmailDecorator
     30from trac.notification.mail import set_header
     31
     32class AuthorEmailDecorator(Component):
     33
     34    implements(IEmailDecorator)
     35
     36    # IEmailDecorator methods
     37   
     38    def decorate_message(self, event, message, charset):
     39        set_header(message, 'X-Trac-Notification-Author', event.author, charset)
     40}}}
     41
     42=== !ReplyToTicketDecorator
     43
     44Another example (adapted from [th:comment:4:ticket:10044 a feature request]) changes the reply-to address depending on the ticket id (so recipients can just reply to the emails and e.g. th:EmailtoTracScript can forward such replies to `10044@trac-hacks.org` to [th:ticket:10044 the appropriate ticket]):
     45{{{#!python
     46from trac.core import Component, implements
     47from trac.config import Option
     48from trac.notification.api import IEmailDecorator
     49from trac.notification.mail import set_header
     50
     51class ReplyToTicketDecorator(Component):
     52    """Replaces the 'Reply-To' header for tickets with a dynamic email address.
     53
     54    Uses a new config option 'ticket_smtp_replyto'.
     55    """
     56    implements(IEmailDecorator)
     57
     58    ticket_smtp_replyto = Option('notification', 'ticket_smtp_replyto', '__id__@localhost',
     59                """Reply-To address for ticket notification emails.
     60
     61                   ` __id__` is replaced with the ticket id.""")
     62
     63    def decorate_message(self, event, message, charset):
     64        if event.realm == 'ticket':
     65            replyto = self.ticket_email_replyto.replace('__id__', str(event.target.id))
     66            set_header(message, 'Reply-To', replyto, charset)
     67}}}
     68
     69== Available Implementations ==
     70
     71* [source:trunk/trac/notification/mail.py#/AlwaysEmailDecorator trac.notification.mail.AlwaysEmailDecorator]: Implements `smtp_always_cc` and `smtp_always_bcc` configuration options.
     72
     73== Additional Information and References ==
     74
     75 * [apiref:trac.notification.api.IEmailDecorator-class epydoc]
     76 * [apidoc:api/trac_notification#trac.notification.api.IEmailDecorator API Reference]
     77
     78=== History
     79 * This interface originated in th:AnnouncerPlugin as `IAnnouncementEmailDecorator`.
     80 * [wiki:TracDev/ApiChanges/1.1.3#IEmailDecorator 1.1.3]: Integrated `IEmailDecorator` in Trac as part of [wiki:TracDev/Proposals/AdvancedNotification this proposal] (#3517)
     81   * Removed continuation-passing style (`next_decorator()`).
     82   * Added `charset` parameter.