def serve(self, request): if request.method == 'POST': form_slug = request.POST.get('formSlug') if 'simpleContactForm' == form_slug: form = SimpleContactForm(request.POST) if form.is_valid(): form.clean() send_mail( 'Contacting using Contact Form', self.build_mail_content_contact(form.cleaned_data), THESES_MAILS, # recipient email form.cleaned_data['contact_email']) return JsonResponse({ 'message': 'Thank you for your interest! ' 'We will let get back to you soon!' }) elif 'proposalForm' == form_slug: form = ProposalForm(request.POST) if form.is_valid(): form.clean() send_mail( 'Contacting using Contact Form', self.build_mail_content_propose(form.cleaned_data), THESES_MAILS, # recipient email form.cleaned_data['contact_email']) return JsonResponse({ 'message': 'Thank you for the proposal! ' 'We will let get back to you soon!' }) else: return super(ThesisSimple, self).serve(request)
def serve(self, request, *args, **kwargs): if request.is_ajax() and request.method == "POST": form = GoogleAdGrantApplicationForm(request.POST) if form.is_valid(): form.save() if self.to_address: subject = "{} form submission".format(self.title) content = '\n'.join([ x[1].label + ': ' + str(form.data.get(x[0])) for x in form.fields.items() ]) send_mail( subject, content, [self.to_address], ) return render( request, 'torchbox/includes/ad_grant_application_landing.html', { 'self': self, 'form': form }) else: return render( request, 'torchbox/includes/ad_grant_application_form.html', { 'self': self, 'form': form }) else: return super(GoogleAdGrantsPage, self).serve(request)
def process_form_submission(self, form): super(AbstractEmailForm, self).process_form_submission(form) if self.to_address: addresses = [x.strip() for x in self.to_address.split(',')] content = '\n'.join([x[1].label + ': ' + text_type(form.data.get(x[0])) for x in form.fields.items()]) send_mail(self.subject, content, addresses, self.from_address,)
def feedback_form(request): if request.method == 'POST': form = FeedbackForm(request.POST) if form.is_valid(): form.clean() contact_email = form.cleaned_data['contact_email'] if contact_email is None: contact_email = THESES_MAILS[0] uri = request.build_absolute_uri() mail_content = build_mail_content(uri, form.cleaned_data) send_mail('Feedback on page {}'.format(uri), mail_content, THESES_MAILS, contact_email) return JsonResponse({ 'message': 'Thank you for your interest! ' 'We will let get back to you soon!' }) else: return JsonResponse({ 'message': 'Sorry, submitting your form was not ' 'successful. Please use our contact page.' }) else: return JsonResponse( {'message': 'Different than POST message is not allowed'})
def test_send_default_from_email(self): send_mail("Test subject", "Test content", ["*****@*****.**"]) # Check that the email was sent self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, "Test subject") self.assertEqual(mail.outbox[0].body, "Test content") self.assertEqual(mail.outbox[0].to, ["*****@*****.**"]) self.assertEqual(mail.outbox[0].from_email, "webmaster@localhost")
def process_form_submission(self, form): super(AbstractEmailForm, self).process_form_submission(form) if self.to_address: content = '' for x in form.fields.items(): if not isinstance(x[1], SweetCaptchaField): # exclude SweetCaptchaField from notification content += '\n' + x[1].label + ': ' + text_type(form.data.get(x[0])) send_mail(self.subject, content, [self.to_address], self.from_address,)
def test_send_email(self): send_mail("Test subject", "Test content", ["*****@*****.**"], "*****@*****.**") # Check that the email was sent self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, "Test subject") self.assertEqual(mail.outbox[0].body, "Test content") self.assertEqual(mail.outbox[0].to, ["*****@*****.**"]) self.assertEqual(mail.outbox[0].from_email, "*****@*****.**")
def test_send_fallback_to_default_from_email_setting(self): send_mail("Test subject", "Test content", ["*****@*****.**"]) # Check that the email was sent self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, "Test subject") self.assertEqual(mail.outbox[0].body, "Test content") self.assertEqual(mail.outbox[0].to, ["*****@*****.**"]) self.assertEqual(mail.outbox[0].from_email, "*****@*****.**")
def send_mail(self, form): addresses = [x.strip() for x in self.to_address.split(',')] content = [] for field in form: value = field.value() if isinstance(value, list): value = ', '.join(value) content.append('{}: {}'.format(field.label, value)) content = '\n'.join(content) send_mail(self.subject, content, addresses, self.from_address,)
def process_form_submission(self, form): self.get_submission_class().objects.create( form_data=json.dumps(form.cleaned_data, cls=DjangoJSONEncoder), page=self, user=form.user ) if self.to_address: addresses = [x.strip() for x in self.to_address.split(',')] content = '\n'.join([x[1].label + ': ' + text_type(form.data.get(x[0])) for x in form.fields.items()]) send_mail(self.subject, content, addresses, self.from_address,)
def process_form_submission(self, form): super(AbstractEmailForm, self).process_form_submission(form) if self.to_address: content = '\n'.join([x[1].label + ': ' + text_type(form.data.get(x[0])) for x in form.fields.items()]) if 'your-name' in form.data and 'your-email' in form.data: subject = u'{}: {} <{}>'.format(self.subject, form.data['your-name'], form.data['your-email']) else: subject = self.subject send_mail(subject, content, [self.to_address], self.from_address,)
def send_form_mail(self, form): addresses = [x.strip() for x in self.to_address.split(',')] content = [] for name, field in form.fields.items(): data = form.cleaned_data.get(name) if name == 'recaptcha' or not data: continue content.append(field.label + ': ' + six.text_type(data)) send_mail(self.subject, '\n'.join(content), addresses, self.from_address)
def send_mail(self, form): addresses = [x.strip() for x in self.to_address.split(',')] content = '\n'.join([ x[1].label + ': ' + text_type(form.data.get(x[0])) for x in form.fields.items() ]) send_mail( self.subject, content, addresses, self.from_address, )
def send_mail(self, form): addresses = [x.strip() for x in self.to_address.split(',')] content = [] for field in form: value = field.value() if isinstance(value, list): value = ', '.join(value) content.append('{}: {}'.format(field.label, value)) submitted_date_str = date.today().strftime('%x') content.append('{}: {}'.format('Submitted', submitted_date_str)) content.append('{}: {}'.format('Submitted Via', self.full_url)) content = '\n'.join(content) subject = self.subject + " - " + submitted_date_str send_mail(subject, content, addresses, self.from_address)
def process_form_submission(self, form): super(AbstractEmailForm, self).process_form_submission(form) if self.to_address: content = '\n'.join([ x[1].label + ': ' + text_type(form.data.get(x[0])) for x in form.fields.items() ]) send_mail( self.subject, content, [self.to_address], self.from_address, )
def process_form_submission(self, form): # add a incremental identifier to every form submission next_identifier = CustomFormSubmission.objects.filter( page=self).order_by('-identifier').first() if next_identifier: next_identifier = next_identifier.identifier + 1 else: next_identifier = 1 self.get_submission_class().objects.create(form_data=json.dumps( form.cleaned_data, cls=DjangoJSONEncoder), page=self, identifier=next_identifier) if self.to_address: self.send_mail(form) if self.send_confirmation_email: # quick hack sending a confirmation email to the user confirmation_email_address = None # check for confirmation email address and filter headings filtered_fields = [] for field in form: if isinstance(field.field.widget, EmailInput): confirmation_email_address = field.value() elif isinstance(field.field.widget, HeadingWidget): continue filtered_fields.append(field) if confirmation_email_address: extra_content = [ '', ] for field in filtered_fields: value = field.value() if isinstance(value, list): value = ', '.join(value) extra_content.append('{}: {}'.format(field.label, value)) extra_content = '\n'.join(extra_content) send_mail( self.confirmation_email_subject, self.confirmation_email_text + extra_content, [ confirmation_email_address, ], self.from_address, )
def process_form_submission(self, form): super(AbstractEmailForm, self).process_form_submission(form) if self.to_address: content = '\n'.join([ x[1].label + ': ' + text_type(form.data.get(x[0])) for x in form.fields.items() ]) if 'your-name' in form.data and 'your-email' in form.data: subject = u'{}: {} <{}>'.format(self.subject, form.data['your-name'], form.data['your-email']) else: subject = self.subject send_mail( subject, content, [self.to_address], self.from_address, )
def send_mail(self, form): # Overwrite the send_mail method from AbstractEmailForm # to use inquiry as email subject. addresses = [x.strip() for x in self.to_address.split(',')] content = [] subj = None send_from = self.from_address for field in form: if 'inquiry' in field.name: subj = field.value() elif 'email' in field.name.lower(): send_from = field.value() value = field.value() if isinstance(value, list): value = ', '.join(value) content.append('{}: {}'.format(field.label, value)) content = '\n'.join(content) if subj: send_mail(subj, content, addresses, send_from,) else: send_mail(self.subject, content, addresses, send_from,)
def serve(self, request): if request.method == 'POST': from theses.forms import InterestsForm form = InterestsForm(request.POST) if form.is_valid(): form.clean() mail_content = self.build_mail_content(request.build_absolute_uri(), form.cleaned_data) send_mail('Thesis interest: {}'.format(request.POST['thesis_title']), mail_content, THESES_MAILS, # recipient email form.cleaned_data['contact_email'] ) return JsonResponse({'message': 'Thank you for your interest! ' 'We will let get back to you soon!'}) else: logger.error('The submitted form was invalid.') return JsonResponse({'message': 'Sorry, submitting your form was not ' 'successful. Please use our contact page.'}) else: return super(ThesisPage, self).serve(request)
def serve(self, request, *args, **kwargs): if request.is_ajax() and request.method == "POST": form = GoogleAdGrantApplicationForm(request.POST) if form.is_valid(): form.save() if self.to_address: subject = "{} form submission".format(self.title) content = '\n'.join([x[1].label + ': ' + str(form.data.get(x[0])) for x in form.fields.items()]) send_mail(subject, content, [self.to_address],) return render( request, 'torchbox/includes/ad_grant_application_landing.html', {'self': self, 'form': form} ) else: return render( request, 'torchbox/includes/ad_grant_application_form.html', {'self': self, 'form': form} ) else: return super(GoogleAdGrantsPage, self).serve(request)
def serve(self, request): if request.method == 'POST': from theses.forms import InterestsForm form = InterestsForm(request.POST) if form.is_valid(): form.clean() absolute_uri = request.build_absolute_uri() mail_content = self.build_mail_content(absolute_uri, form.cleaned_data) thesis_title = form.cleaned_data['thesis_title'] send_mail( 'Thesis interest: {}'.format(thesis_title), mail_content, THESES_MAILS, # recipient email form.cleaned_data['contact_email']) return conversion(request, absolute_uri, thesis_title) else: logger.error('The submitted form was invalid.') return super(ThesisPage, self).serve(request) else: return super(ThesisPage, self).serve(request)
def process_form_submission(self, form): super(AbstractEmailForm, self).process_form_submission(form) if self.to_address: content = '\n'.join([x[1].label + ': ' + form.data.get(x[0]) for x in form.fields.items()]) send_mail(self.subject, content, [self.to_address], self.from_address,)
def send_mail(self, form): addresses = [x.strip() for x in self.to_address.split(',')] content = '\n'.join([x[1].label + ': ' + text_type(form.data.get(x[0])) for x in form.fields.items()]) send_mail(self.subject, content, addresses, self.from_address,)