def send_inquiry(self, data): context = aq_inner(self.context) if self.subpath: contact_uid = self.subpath[0] contact_obj = api.content.get(UID=contact_uid) subject = _(u"Inquiry from website visitor") email_subject = api.portal.translate( "Inquiry from website visitor", 'ade25.contacts', api.portal.get_current_language()) mail_tpl = self._compose_message(data) mail_plain = create_plaintext_message(mail_tpl) msg = prepare_email_message(mail_tpl, mail_plain) default_email = api.portal.get_registry_record('plone.email_from_address') recipient_email = getattr(context, 'email', default_email) if contact_obj: recipient_email = getattr(contact_obj, 'email', default_email) recipients = [recipient_email, ] send_mail( msg, recipients, email_subject ) next_url = '{0}/@@inquiry-form-dispatched/{1}'.format( context.absolute_url(), self.traverse_subpath[0] ) return self.request.response.redirect(next_url)
def update(self): translation_service = api.portal.get_tool(name="translation_service") unwanted = ('_authenticator', 'form.button.Submit') required = ('email', 'subject') if 'form.button.Submit' in self.request: authenticator = getMultiAdapter((self.context, self.request), name=u"authenticator") if not authenticator.verify(): raise Unauthorized form = self.request.form form_data = {} form_errors = {} errorIdx = 0 for value in form: if value not in unwanted: form_data[value] = safe_unicode(form[value]) if not form[value] and value in required: error = {} error_msg = _(u"This field is required") error['active'] = True error['msg'] = translation_service.translate( error_msg, 'ade25.contacts', target_language=api.portal.get_default_language() ) form_errors[value] = error errorIdx += 1 else: error = {} error['active'] = False error['msg'] = form[value] form_errors[value] = error if errorIdx > 0: self.errors = form_errors else: self.send_inquiry(form)