The SubscriptionWorkflow class doesn't need to include the expiry date in its
[mailman.git] / src / mailman / app / notifications.py
blobfc8a0549e01db41af1daa8ba00d2d8049004392f
1 # Copyright (C) 2007-2015 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 """Sending notifications."""
20 __all__ = [
21 'send_admin_subscription_notice',
22 'send_goodbye_message',
23 'send_welcome_message',
27 import logging
29 from email.utils import formataddr
30 from lazr.config import as_boolean
31 from mailman.config import config
32 from mailman.core.i18n import _
33 from mailman.email.message import OwnerNotification, UserNotification
34 from mailman.interfaces.member import DeliveryMode
35 from mailman.interfaces.templates import ITemplateLoader
36 from mailman.utilities.i18n import make
37 from mailman.utilities.string import expand, wrap
38 from urllib.error import URLError
39 from zope.component import getUtility
42 log = logging.getLogger('mailman.error')
46 def _get_message(uri_template, mlist, language):
47 if not uri_template:
48 return ''
49 try:
50 uri = expand(uri_template, dict(
51 listname=mlist.fqdn_listname,
52 language=language.code,
54 message = getUtility(ITemplateLoader).get(uri)
55 except URLError:
56 log.exception('Message URI not found ({0}): {1}'.format(
57 mlist.fqdn_listname, uri_template))
58 return ''
59 else:
60 return wrap(message)
64 def send_welcome_message(mlist, member, language, text=''):
65 """Send a welcome message to a subscriber.
67 Prepending to the standard welcome message template is the mailing list's
68 welcome message, if there is one.
70 :param mlist: The mailing list.
71 :type mlist: IMailingList
72 :param member: The member to send the welcome message to.
73 :param address: IMember
74 :param language: The language of the response.
75 :type language: ILanguage
76 """
77 welcome_message = _get_message(mlist.welcome_message_uri, mlist, language)
78 options_url = member.options_url
79 # Get the text from the template.
80 display_name = ('' if member.user is None else member.user.display_name)
81 text = expand(welcome_message, dict(
82 fqdn_listname=mlist.fqdn_listname,
83 list_name=mlist.display_name,
84 listinfo_uri=mlist.script_url('listinfo'),
85 list_requests=mlist.request_address,
86 user_name=display_name,
87 user_address=member.address.email,
88 user_options_uri=options_url,
90 digmode = ('' if member.delivery_mode is DeliveryMode.regular
91 else _(' (Digest mode)'))
92 msg = UserNotification(
93 formataddr((display_name, member.address.email)),
94 mlist.request_address,
95 _('Welcome to the "$mlist.display_name" mailing list${digmode}'),
96 text, language)
97 msg['X-No-Archive'] = 'yes'
98 msg.send(mlist, verp=as_boolean(config.mta.verp_personalized_deliveries))
102 def send_goodbye_message(mlist, address, language):
103 """Send a goodbye message to a subscriber.
105 Prepending to the standard goodbye message template is the mailing list's
106 goodbye message, if there is one.
108 :param mlist: the mailing list
109 :type mlist: IMailingList
110 :param address: The address to respond to
111 :type address: string
112 :param language: the language of the response
113 :type language: string
115 goodbye_message = _get_message(mlist.goodbye_message_uri,
116 mlist, language)
117 msg = UserNotification(
118 address, mlist.bounces_address,
119 _('You have been unsubscribed from the $mlist.display_name '
120 'mailing list'),
121 goodbye_message, language)
122 msg.send(mlist, verp=as_boolean(config.mta.verp_personalized_deliveries))
126 def send_admin_subscription_notice(mlist, address, display_name, language):
127 """Send the list administrators a subscription notice.
129 :param mlist: The mailing list.
130 :type mlist: IMailingList
131 :param address: The address being subscribed.
132 :type address: string
133 :param display_name: The name of the subscriber.
134 :type display_name: string
135 :param language: The language of the address's display name.
136 :type language: string
138 with _.using(mlist.preferred_language.code):
139 subject = _('$mlist.display_name subscription notification')
140 text = make('adminsubscribeack.txt',
141 mailing_list=mlist,
142 listname=mlist.display_name,
143 member=formataddr((display_name, address)),
145 msg = OwnerNotification(mlist, subject, text, roster=mlist.administrators)
146 msg.send(mlist)