add effective emailing, including copy to sender (#891)
This commit is contained in:
parent
384c1ca838
commit
fe0af4b0ab
|
@ -62,6 +62,16 @@ def get_possible_subjects(context):
|
|||
terms.append(SimpleVocabulary.createTerm('-', '-', '-'))
|
||||
return SimpleVocabulary(terms)
|
||||
|
||||
def get_email_for_subject(context, topic):
|
||||
for line in context.context.subjects.splitlines():
|
||||
try:
|
||||
topic, email = line.strip().split('|')
|
||||
except ValueError:
|
||||
continue
|
||||
if topic == topic:
|
||||
return email
|
||||
return None
|
||||
|
||||
class IEffectiveContact(interface.Interface):
|
||||
subject = schema.Choice(title=_(u'Subject'), required=True,
|
||||
source=get_possible_subjects)
|
||||
|
@ -100,10 +110,25 @@ class EffectiveContactForm(form.Form):
|
|||
captcha = CaptchaValidator(self.context, self.request, None, IEffectiveContact['captcha'], None)
|
||||
if captcha.validate(data['captcha']):
|
||||
# if captcha validation passes, send the email.
|
||||
plone_utils = getToolByName(self.context.context, 'plone_utils')
|
||||
plone_utils.addPortalMessage(_(u'Your message has been sent successfully.'))
|
||||
portal = getToolByName(self.context.context, 'portal_url').getPortalObject()
|
||||
plone_utils = getToolByName(self.context.context, 'plone_utils')
|
||||
settings = component.getUtility(IRegistry).forInterface(ITabellioSettings, False)
|
||||
topic_email = get_email_for_subject(self.context, data.get('subject'))
|
||||
|
||||
try:
|
||||
sendmail(self.context.context, data, topic_email)
|
||||
except: # TODO Too many things could possibly go wrong. So we catch all.
|
||||
plone_utils = getToolByName(self.context.context, 'plone_utils')
|
||||
exception = plone_utils.exceptionString()
|
||||
message = _(u'Unable to send mail: ${exception}',
|
||||
mapping={u'exception' : exception})
|
||||
plone_utils.addPortalMessage(message, 'error')
|
||||
return self.request.response.redirect('.')
|
||||
|
||||
plone_utils.addPortalMessage(_(u'Your message has been sent successfully.'))
|
||||
return self.request.response.redirect(portal.absolute_url())
|
||||
|
||||
|
||||
except ValidationError:
|
||||
pass
|
||||
return
|
||||
|
@ -191,12 +216,40 @@ class EffectiveDeputyContactForm(form.Form):
|
|||
if not errors and data.has_key('captcha'):
|
||||
# Verify the user input against the captcha
|
||||
try:
|
||||
captcha = CaptchaValidator(self.context, self.request, None, IEffectiveContact['captcha'], None)
|
||||
captcha = CaptchaValidator(self.context, self.request, None,
|
||||
IEffectiveDeputyContact['captcha'], None)
|
||||
if captcha.validate(data['captcha']):
|
||||
# if captcha validation passes, send the email.
|
||||
plone_utils = getToolByName(self.context.context, 'plone_utils')
|
||||
plone_utils.addPortalMessage(_(u'Your message has been sent successfully.'))
|
||||
portal = getToolByName(self.context.context, 'portal_url').getPortalObject()
|
||||
plone_utils = getToolByName(self.context.context, 'plone_utils')
|
||||
settings = component.getUtility(IRegistry).forInterface(ITabellioSettings, False)
|
||||
path = settings.deputiesPath
|
||||
current = portal
|
||||
for part in settings.deputiesPath.split('/'):
|
||||
if not part:
|
||||
continue
|
||||
current = getattr(current, part)
|
||||
|
||||
deputy = getattr(current, data.get('deputy'))
|
||||
deputy_email = None
|
||||
if deputy.work_address:
|
||||
deputy_email = deputy.work_address.email
|
||||
if not deputy_email and deputy.work_address_2:
|
||||
deputy_email = deputy.work_address_2.email
|
||||
if not deputy_email and deputy.private_address:
|
||||
deputy_email = deputy.private_address.email
|
||||
|
||||
try:
|
||||
sendmail(self.context.context, data, deputy_email)
|
||||
except: # TODO Too many things could possibly go wrong. So we catch all.
|
||||
plone_utils = getToolByName(self.context.context, 'plone_utils')
|
||||
exception = plone_utils.exceptionString()
|
||||
message = _(u'Unable to send mail: ${exception}',
|
||||
mapping={u'exception' : exception})
|
||||
plone_utils.addPortalMessage(message, 'error')
|
||||
return self.request.response.redirect('deputy')
|
||||
|
||||
plone_utils.addPortalMessage(_(u'Your message has been sent successfully.'))
|
||||
return self.request.response.redirect(portal.absolute_url())
|
||||
except ValidationError:
|
||||
pass
|
||||
|
@ -205,3 +258,41 @@ class EffectiveDeputyContactForm(form.Form):
|
|||
|
||||
# Register Captcha validator for the captcha field in the IContactForm
|
||||
validator.WidgetValidatorDiscriminators(CaptchaValidator, field=IEffectiveContact['captcha'])
|
||||
|
||||
|
||||
def sendmail(context, data, mto):
|
||||
urltool = getToolByName(context, 'portal_url')
|
||||
portal = urltool.getPortalObject()
|
||||
|
||||
send_to_address = portal.getProperty('email_from_address')
|
||||
envelope_from = portal.getProperty('email_from_address')
|
||||
encoding = portal.getProperty('email_charset')
|
||||
|
||||
message = _(u"""De: %(name)s (phone: %(phone)s, email: %(email)s)
|
||||
Subject: %(subject)s
|
||||
|
||||
Message:
|
||||
|
||||
%(message)s
|
||||
""") % data
|
||||
u_message = message
|
||||
message = message.encode(encoding)
|
||||
|
||||
context.MailHost.send('\n\n'+message, mto, envelope_from,
|
||||
subject=_(u'New message from %s') % portal.title)
|
||||
|
||||
# and now, send a copy to the sender
|
||||
if data.get('email'):
|
||||
message = _(u"""Your message has been sent, here's a copy for reference.
|
||||
|
||||
%(message)s
|
||||
""" % {'message': u_message})
|
||||
|
||||
try:
|
||||
context.MailHost.send('\n\n'+message, data.get('email'), envelope_from,
|
||||
subject=_(u'Your message on %s') % portal.title)
|
||||
except:
|
||||
# ignore everything here, as the most important part has been done
|
||||
# successfully
|
||||
pass
|
||||
|
||||
|
|
Reference in New Issue