/
contact.py
174 lines (149 loc) · 7.1 KB
/
contact.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from Products.CMFCore.utils import getToolByName
from Products.statusmessages.interfaces import IStatusMessage
from email.header import Header
from email.mime.text import MIMEText
from ftw.subsite import _
from ftw.subsite import IS_PLONE_5
from plone.app.layout.navigation.root import getNavigationRoot
from plone.dexterity.utils import safe_utf8
from z3c.form import form, field, button
from z3c.form.validator import SimpleFieldValidator
from z3c.form.validator import WidgetValidatorDiscriminators
from zope import schema
from zope.component import provideAdapter
from zope.i18n import translate
from zope.interface import Interface
from zope.interface import Invalid
class IContactForm(Interface):
"""Interface for z3c.form"""
sender = schema.TextLine(title=_(u"Sender_Name", default=u"Name"),
required=True,
description=_(u"help_Sender", default="Please enter your Name"))
email = schema.TextLine(title=_(u"mail_address", default="E-Mail"),
required=True,
description=_(u"help_mail_address", default="Please enter your e-mail"))
subject = schema.TextLine(title=_(u"label_subject", default="Subject"),
required=True,
description=_(u"help_subject", default="Please enter a Subject"))
message = schema.Text(title=_(u"label_message", default="Message"),
required=True,
description=_(u"help_message", default="Please enter a Message"))
class ContactForm(form.Form):
label = _(u"label_send_feedback", default=u"Send Feedback")
fields = field.Fields(IContactForm)
# don't use context to get widget data
ignoreContext = True
@button.buttonAndHandler(_(u'Send Mail'))
def handleApply(self, action):
data, errors = self.extractData()
if errors:
return
message = data.get('message')
email = data.get('email')
subject = data.get('subject')
sender = data.get('sender')
self.send_feedback(email, subject, message, sender)
msg = _(u'info_email_sent', default=u'The email was sent.')
IStatusMessage(self.request).addStatusMessage(msg, type='info')
return self.redirect()
@button.buttonAndHandler(_(u'button_cancel', default=u'Cancel'))
def handle_cancel(self, action):
return self.redirect()
def redirect(self):
"""Redirect back
"""
url = self.context.absolute_url()
return self.request.RESPONSE.redirect(url)
def send_feedback(self, recipient, subject, message, sender):
"""Send a feedback email to the email address defined in subsite.
"""
portal = getToolByName(self.context, 'portal_url').getPortalObject()
nav_root = None
if not '/'.join(portal.getPhysicalPath()) == getNavigationRoot(self.context):
nav_root = self.context.restrictedTraverse(
getNavigationRoot(self.context))
if nav_root:
site_title = nav_root.Title()
site_url = nav_root.absolute_url()
else:
site_title = portal.Title()
site_url = portal.absolute_url()
message = translate(
u'feedback_mail_text',
domain='ftw.subsite',
default='${sender} sends you a message from your site ${site_title} (${site_url}):\n${msg}',
context=self.request,
mapping={'sender': "%s (%s)" % (sender, recipient),
'msg': message,
'site_title': site_title,
'site_url': site_url})
default_from_name = portal.getProperty('email_from_name', '')
default_from_email = portal.getProperty('email_from_address', '')
if nav_root:
email_from_name = nav_root.from_name or default_from_name
email_from_email = nav_root.from_email or default_from_email
else:
email_from_name = default_from_name
email_from_email = default_from_email
if IS_PLONE_5:
from plone import api
reg = api.portal.get_tool('portal_registry')
from_email_field = reg._records.get('plone.email_from_address')
if not email_from_email and not from_email_field.value:
# especially for testing reasons we need to set a value here
# if none is registered on the page in plone5
from_email_field._set_value('site@nohost.com')
email_from_email = from_email_field.value
email_from_email = (email_from_email or
reg._records.get('plone.email_from_address').value)
email_from_name = (email_from_name or
reg._records.get('plone.email_from_name').value)
# send the message
self.send_mail(email_from_name, email_from_email, email_from_email,
subject, message, recipient, sender)
def send_mail(self, name_from, mail_from, mail_to, mail_subject,
message_text, reply_mail, reply_name):
"""Send mail using plone mailhost (all input strings need to be utf-8).
"""
if not name_from:
name_from = ''
# Put together the mail parts
msg = MIMEText(safe_utf8(message_text), 'plain', 'utf-8')
msg['From'] = '{} <{}>'.format(safe_utf8(name_from),
safe_utf8(mail_from))
msg['To'] = safe_utf8(mail_to)
msg['Subject'] = Header(safe_utf8(mail_subject), 'utf-8')
msg['reply-to'] = '{} <{}>'.format(safe_utf8(reply_name),
safe_utf8(reply_mail))
# Get mailhost and send to mail_to
mailhost = getToolByName(self.context, 'MailHost')
mailhost.send(msg)
class AddressesValidator(SimpleFieldValidator):
"""Validator for validating the e-mail addresses field
"""
MAIL_EXPRESSION = r"^(\w&.%#$&'\*+-/=?^_`{}|~]+!)*[\w&.%#$&'\*+-/=" +\
"?^_`{}|~]+@(([0-9a-z]([0-9a-z-]*[0-9a-z])?" +\
"\.)+[a-z]{2,6}|([0-9]{1,3}\.){3}[0-9]{1,3})$"
def __init__(self, *args, **kwargs):
super(AddressesValidator, self).__init__(*args, **kwargs)
self.email_expression = re.compile(AddressesValidator.MAIL_EXPRESSION,
re.IGNORECASE)
def validate(self, value):
"""Validates the `value`, expects a list of carriage-return-separated
email addresses.
"""
super(AddressesValidator, self).validate(value)
if value:
address = value.strip()
self._validate_addresses(address)
def _validate_addresses(self, address):
"""E-Mail address validation
"""
address = address.strip()
if not self.email_expression.match(address):
msg = _(u'error_invalid_addresses',
default=u'Your E-mail address is not valid.')
raise Invalid(msg)
WidgetValidatorDiscriminators(AddressesValidator,
field=IContactForm['email'])
provideAdapter(AddressesValidator)