Skip to content

Commit

Permalink
better email submission example
Browse files Browse the repository at this point in the history
  • Loading branch information
bigmassa committed Jun 16, 2018
1 parent b137216 commit c2b830e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
37 changes: 29 additions & 8 deletions docs/hooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ Create a ``wagtailstreamforms_hooks.py`` in the root of one of your apps and add

.. code-block:: python
from django.core.mail import send_mail
from django.conf import settings
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.core.mail import EmailMessage
from django.template.defaultfilters import pluralize
from wagtailstreamforms.hooks import register
@register('process_form_submission')
Expand All @@ -34,17 +38,34 @@ Create a ``wagtailstreamforms_hooks.py`` in the root of one of your apps and add
addresses = ['to@example.com']
content = ['Please see below submission\n', ]
from_address = 'from@example.com'
subject = 'New Submission'
from_address = settings.DEFAULT_FROM_EMAIL
subject = 'New Form Submission : %s' % instance.title
for field in form:
value = field.value()
if isinstance(value, list):
# build up the email content
for field, value in form.cleaned_data.items():
if isinstance(value, InMemoryUploadedFile):
count = len(form.files.getlist(field))
value = '{} file{}'.format(count, pluralize(count))
elif isinstance(value, list):
value = ', '.join(value)
content.append('{}: {}'.format(field.label, value))
content.append('{}: {}'.format(field, value))
content = '\n'.join(content)
send_mail(subject, content, from_address, addresses, True)
# create the email message
email = EmailMessage(
subject=subject,
body=content,
from_email=from_address,
to=addresses
)
# attach any files submitted
for field in form.files:
for file in form.files.getlist(field):
email.attach(file.name, file.file.getvalue(), file.content_type)
# finally send the email
email.send(fail_silently=True)
A new option will appear in the setup of the forms to run the above hook. The name of the option is taken from
the function name so keep them unique to avoid confusion. The ``instance`` is the form class instance, the
Expand Down
31 changes: 25 additions & 6 deletions example/wagtailstreamforms_hooks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.conf import settings
from django.core.mail import send_mail
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.core.mail import EmailMessage
from django.template.defaultfilters import pluralize

from wagtailstreamforms.hooks import register

Expand All @@ -16,11 +18,28 @@ def email_submission(instance, form):
from_address = settings.DEFAULT_FROM_EMAIL
subject = 'New Form Submission : %s' % instance.title

for field in form:
value = field.value()
if isinstance(value, list):
# build up the email content
for field, value in form.cleaned_data.items():
if isinstance(value, InMemoryUploadedFile):
count = len(form.files.getlist(field))
value = '{} file{}'.format(count, pluralize(count))
elif isinstance(value, list):
value = ', '.join(value)
content.append('{}: {}'.format(field.label, value))
content.append('{}: {}'.format(field, value))
content = '\n'.join(content)

send_mail(subject, content, from_address, addresses, True)
# create the email message
email = EmailMessage(
subject=subject,
body=content,
from_email=from_address,
to=addresses
)

# attach any files submitted
for field in form.files:
for file in form.files.getlist(field):
email.attach(file.name, file.file.getvalue(), file.content_type)

# finally send the email
email.send(fail_silently=True)

0 comments on commit c2b830e

Please sign in to comment.