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, 'home/includes/ad_grant_application_landing.html', { 'self': self, 'form': form }) else: return render(request, 'home/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 serve(self, request): if request.method == "POST": print("posting") from theses.forms import CoachingForm form = CoachingForm(request.POST) if form.is_valid(): print("form is valid") form.clean() mail_content = self.build_mail_content(form.cleaned_data) contact_name = form.cleaned_data["contact_name"] send_mail( "Thesis coaching interest: {}".format(contact_name), mail_content, THESES_MAILS_2, # recipient email form.cleaned_data["contact_email"], ) return coaching_conversion(request) else: print("form is invalid") logger.error("The submitted form was invalid.") return super().serve(request, errored_form=form) else: return super().serve(request)
def process_form_submission(self, form): self.get_submission_class().objects.create(form_data=json.dumps( form.cleaned_data, cls=DjangoJSONEncoder), page=self) 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 for field in form: if isinstance(field.field.widget, EmailInput): confirmation_email_address = field.value() break if confirmation_email_address: extra_content = [ '', ] for field in form: 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 serve(self, request, *args, **kwargs): if request.method == "POST": form = TopicInterestForm(request.POST) if form.is_valid(): mail_content = self.build_mail_content( request.build_absolute_uri(), form.cleaned_data ) send_mail( subject="Topic interest", message=mail_content, recipient_list=THESES_MAILS_2, from_email=form.cleaned_data["contact_email"], ) messages.success(request, "Thank you for your interest!") return HttpResponseRedirect(request.path_info + "#topic-interest-form") else: return super().serve(request, form=form) if "discipline" in request.GET: discipline_name = request.GET["discipline"] if discipline_name in ("biology", "medicine"): return HttpResponseRedirect( request.path_info + "?discipline=life+sciences#disciplines" ) if discipline_name in ("physics", "mathematics statistics"): return HttpResponseRedirect( request.path_info + "?discipline=mathematics+statistics+physics#disciplines" ) return super().serve(request)
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 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 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 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 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 + ': ' + str(data)) send_mail( self.subject, '\n'.join(content), addresses, self.from_address)
def send_share_email(self): """ Emails the user with the review link """ email_address = self.external_user.email review_token = Token(self.external_user.get_reviewer(), self.page.get_latest_revision()) email_body = render_to_string('wagtail_review/email/share.txt', { 'page': self.page, 'review_url': get_review_url(review_token), }) send_mail("A page has been shared with you", email_body, [email_address])
def process_form_submission(self, form): form_submission = 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 + ': ' + str(form.data.get(x[0])) for x in form.fields.items()]) send_mail(self.subject, content, addresses, self.from_address,) # process_form_submission should now return the created form_submission return form_submission
def send_mail(self, form): addresses = [address.strip() for address 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 submit_to_newsletter(request): print(request.is_ajax()) if request.is_ajax(): if request.method == 'POST': form_data = dejsonize(request.body.decode()) contact_name = form_data['contact_name'] contact_email = form_data['contact_email'] send_mail( "Contacting using Contact Form in Footer", build_mail_content_contact(contact_name, contact_email, "Interest in newsletter"), THESES_MAILS, # recipient email contact_email, ) return HttpResponse("Thank you for your interest!") else: return HttpResponse("Something went wrong when submitting to newsletter")
def ask_us_anything(request): print(request.is_ajax()) if request.is_ajax(): if request.method == 'POST': form_data = dejsonize(request.body.decode()) contact_name = form_data['contact_name'] contact_email = form_data['contact_email'] text = form_data['content'] send_mail( "Contacting using Contact Form on About us", build_mail_content_contact(contact_name, contact_email, text), THESES_MAILS, # recipient email contact_email, ) return HttpResponse("Thank you for your question! We'll get back to you as soon as possible.") else: return HttpResponse("Something went wrong when contacting us.")
def send_notification_to_submitter(self): submitter = self.reviewer.review.submitter if submitter.email: context = { 'submitter': submitter, 'reviewer': self.reviewer, 'review': self.reviewer.review, 'page': self.reviewer.review.revision_as_page, 'response': self, } email_subject = render_to_string( 'wagtail_review/email/response_received_subject.txt', context).strip() email_content = render_to_string( 'wagtail_review/email/response_received.txt', context).strip() send_mail(email_subject, email_content, [submitter.email])
def send_request_emails(self): # send request emails to all reviewers except the reviewer record for the user submitting the request for reviewer in self.assignees.all(): email = reviewer.get_email() context = { 'email': email, 'user': reviewer.internal, 'reviewer': reviewer, 'review_request': self, 'page': self.page_revision.as_page_object(), 'submitter': self.submitted_by, 'review_url': self.get_review_url(reviewer), } email_subject = render_to_string('wagtail_review/email/request_review_subject.txt', context).strip() email_content = render_to_string('wagtail_review/email/request_review.txt', context).strip() send_mail(email_subject, email_content, [email])
def send_request_email(self): email_address = self.get_email_address() context = { 'email': email_address, 'user': self.user, 'review': self.review, 'page': self.review.revision_as_page, 'submitter': self.review.submitter, 'respond_url': self.get_respond_url(absolute=True), 'view_url': self.get_view_url(absolute=True), } email_subject = render_to_string( 'wagtail_review/email/request_review_subject.txt', context).strip() email_content = render_to_string( 'wagtail_review/email/request_review.txt', context).strip() send_mail(email_subject, email_content, [email_address])
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!" }) else: return super(ThesisSimple, self).serve(request)
def send_mail(self, form): cleaned_data = self.process_bilingual_form_data(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) subject = "Message from: %(name)s <%(email)s>" % { 'name': cleaned_data['name'], 'email': cleaned_data['email'], } send_mail( subject, content, addresses, self.from_address, )
def send_mail(self, form): addresses = [x.strip() for x in self.to_address.split(',')] emailheader = "New registration via SNEK" 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) content += '\n\nMade with ❤ by Werbeagentur Christian Aichner' #emailfooter = '<style>@keyframes pulse { 10% { color: red; } }</style><p>Made with <span style="width: 20px; height: 1em; color:#dd0000; animation: pulse 1s infinite;">❤</span> by <a style="color: lightgrey" href="https://www.aichner-christian.com" target="_blank">Werbeagentur Christian Aichner</a></p>' #html_message = f"{emailheader}\n\n{content}\n\n{emailfooter}" send_mail(self.subject, f"{emailheader}\n\n{content}", addresses, self.from_address)
def test_send_html_email(self): """Test that the kwarg 'html_message' works as expected on send_mail by creating 'alternatives' on the EmailMessage object""" send_mail("Test HTML subject", "TEXT content", ["*****@*****.**"], html_message="<h2>Test HTML content</h2>") send_mail("Test TEXT subject", "TEXT content", ["*****@*****.**"]) # Check that the emails were sent self.assertEqual(len(mail.outbox), 2) # check that the first email is the HTML email email_message = mail.outbox[0] self.assertEqual(email_message.subject, "Test HTML subject") self.assertEqual(email_message.alternatives, [('<h2>Test HTML content</h2>', 'text/html')]) self.assertEqual(email_message.body, "TEXT content") # note: plain text will alwasy be added to body, even with alternatives self.assertEqual(email_message.to, ["*****@*****.**"]) # confirm that without html_message kwarg we do not get 'alternatives' email_message = mail.outbox[1] self.assertEqual(email_message.subject, "Test TEXT subject") self.assertEqual(email_message.alternatives, []) self.assertEqual(email_message.body, "TEXT content") self.assertEqual(email_message.to, ["*****@*****.**"])
def send_mail(self, form): addresses = [x.strip() for x in self.to_address.split(',')] emailheader = "New registration via Pharmaziegasse Website" 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) content += '\n\nMade with ❤ by Werbeagentur Christian Aichner' #emailfooter = '<style>@keyframes pulse { 10% { color: red; } }</style><p>Made with <span style="width: 20px; height: 1em; color:#dd0000; animation: pulse 1s infinite;">❤</span> by <a style="color: lightgrey" href="https://www.aichner-christian.com" target="_blank">Werbeagentur Christian Aichner</a></p>' #html_message = f"{emailheader}\n\n{content}\n\n{emailfooter}" html_message = render_to_string( 'registration/registration_mail_template.html', { 'first_name': form.cleaned_data['first_name'], 'last_name': form.cleaned_data['last_name'], 'title': form.cleaned_data['title'], 'email': form.cleaned_data['email'], 'telephone': form.cleaned_data['telephone'], 'address': form.cleaned_data['address'], 'city': form.cleaned_data['city'], 'postal_code': form.cleaned_data['postal_code'], 'newsletter': form.cleaned_data['newsletter'], 'verified': form.cleaned_data['verified'] }) send_mail(self.subject, f"{emailheader}\n\n{content}", addresses, self.from_address, html_message=html_message)
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)