Clean up the mta directory.
[mailman.git] / src / mailman / mta / personalized.py
blob7cfbfff43a09f427f1e5fc3dc5983109b899e8b1
1 # Copyright (C) 2009-2016 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
5 # GNU Mailman is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
8 # any later version.
10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
18 """Personalized delivery."""
20 from email.header import Header
21 from email.utils import formataddr
22 from mailman import public
23 from mailman.interfaces.mailinglist import Personalization
24 from mailman.interfaces.usermanager import IUserManager
25 from mailman.mta.verp import VERPDelivery
26 from zope.component import getUtility
29 @public
30 class PersonalizedMixin:
31 """Personalize the message's To header.
33 This is a mixin class, providing the basic functionality for header
34 personalization. The methods it provides are intended to be called from a
35 concrete base class.
36 """
38 def personalize_to(self, mlist, msg, msgdata):
39 """Modify the To header to contain the recipient.
41 The To header contents is replaced with the recipient's address, and
42 if the recipient is a user registered with Mailman, the recipient's
43 real name too.
44 """
45 # Personalize the To header if the list requests it.
46 if mlist.personalize != Personalization.full:
47 return
48 recipient = msgdata['recipient']
49 user_manager = getUtility(IUserManager)
50 user = user_manager.get_user(recipient)
51 if user is None:
52 msg.replace_header('To', recipient)
53 else:
54 # Convert the unicode name to an email-safe representation.
55 # Create a Header instance for the name so that it's properly
56 # encoded for email transport.
57 name = Header(user.display_name).encode()
58 msg.replace_header('To', formataddr((name, recipient)))
61 @public
62 class PersonalizedDelivery(PersonalizedMixin, VERPDelivery):
63 """Personalize the message's To header."""
65 def __init__(self):
66 """See `IndividualDelivery`."""
67 super().__init__()
68 self.callbacks.append(self.personalize_to)