| 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 | |
| 6 | An ''IEmailDecorator'' decorates [TracNotification notification] email, usually by adding additional email headers. |
| 7 | |
| 8 | == Purpose == |
| 9 | |
| 10 | Trac provides an extendible and flexible notification system, that historically has sent emails formatted by some fixed logic. |
| 11 | |
| 12 | Now 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 | |
| 16 | Implementing the interface follows the standard guidelines found in [wiki:TracDev/ComponentArchitecture] and of course [wiki:TracDev/PluginDevelopment]. |
| 17 | |
| 18 | The `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 | |
| 25 | The following example adds a custom `X-Trac-Notification-Author` header to emails: |
| 26 | |
| 27 | {{{#!python |
| 28 | from trac.core import * |
| 29 | from trac.notification.api import IEmailDecorator |
| 30 | from trac.notification.mail import set_header |
| 31 | |
| 32 | class 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 | |
| 44 | Another 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 |
| 46 | from trac.core import Component, implements |
| 47 | from trac.config import Option |
| 48 | from trac.notification.api import IEmailDecorator |
| 49 | from trac.notification.mail import set_header |
| 50 | |
| 51 | class 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. |