| 1 | == Extension Point : ''IEmailAddressResolver'' == |
| 2 | |
| 3 | ||'''Interface'''||''IEmailAddressResolver''||'''Since'''||[wiki:TracDev/ApiChanges/1.1.3#IEmailAddressResolver 1.1.3]|| |
| 4 | ||'''Module'''||''trac.notification''||'''Source'''||[source:psuter/trac/notification/api.py@advanced-notification-mail-distribution#/IEmailAddressResolver api.py]|| |
| 5 | |
| 6 | The ''IEmailAddressResolver'' is a fallback mechanism for determining which email address should be used to send [TracNotification notifications]. |
| 7 | |
| 8 | == Purpose == |
| 9 | |
| 10 | Trac provides an extendible and flexible notification system, that historically has sent emails to the email address specified in the user preferences or in the CC, reporter or author fields. |
| 11 | |
| 12 | Now plugins can add additional ways to find an email address for a Trac session that has no email address specified in the user preferences. |
| 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 `get_address_for_session()` method returns the email address for the given session or `None`. The parameters are: |
| 19 | * `sid`: The Trac session ID. |
| 20 | * `authenticated`: 1 if the Trac session ID is authenticated, 0 otherwise. |
| 21 | |
| 22 | == Examples == |
| 23 | |
| 24 | The following naively uses LDAP to retrieve the email address. |
| 25 | |
| 26 | {{{#!python |
| 27 | import ldap |
| 28 | |
| 29 | from trac.core import * |
| 30 | from trac.notification.api import IEmailAddressResolver |
| 31 | |
| 32 | class LdapEmailAddressResolver(Component): |
| 33 | |
| 34 | implements(IEmailAddressResolver) |
| 35 | |
| 36 | # IEmailAddressResolver methods |
| 37 | |
| 38 | def get_address_for_session(sid, authenticated): |
| 39 | address = None |
| 40 | ld = ldap.initialize('ldap://localhost:1390') |
| 41 | ld.simple_bind_s() |
| 42 | for dn, entry in ld.search_s('ou=people,dc=example,dc=com', |
| 43 | ldap.SCOPE_SUBTREE, |
| 44 | '(cn={0})' % sid, |
| 45 | ['mail']): |
| 46 | if 'mail' in entry: |
| 47 | address = entry['mail'] |
| 48 | ld.unbind_s() |
| 49 | return address |
| 50 | }}} |
| 51 | |
| 52 | == Available Implementations == |
| 53 | |
| 54 | * [source:trunk/trac/ticket/notification/mail.py trac.ticket.notification.mail.SessionEmailResolver] |
| 55 | |
| 56 | == Additional Information and References == |
| 57 | |
| 58 | * [apiref:trac.notification.api.IEmailAddressResolver-class epydoc] |
| 59 | * [apidoc:api/trac_notification#trac.notification.api.IEmailAddressResolver API Reference] |
| 60 | * Related tickets: |
| 61 | * #2766 introduced the `ignore_domains` option after so user names that look like email addresses are recognized correctly. |
| 62 | * #4372 introduced the `admit_domains` option so email addresses with uncommon domains are recognized correctly. |
| 63 | |
| 64 | === History |
| 65 | * This interface originated in th:AnnouncerPlugin as `IAnnouncementAddressResolver`. |
| 66 | * [wiki:TracDev/ApiChanges/1.1.3#IEmailAddressResolver 1.1.3]: Integrated `IEmailAddressResolver` in Trac as part of [wiki:TracDev/Proposals/AdvancedNotification this proposal] (#3517) |
| 67 | * Renamed `get_address_for_name()` to `get_address_for_session()`. |