Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Added support for email reply to with dynamic placeholders #66

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions djangocms_forms/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class FormFieldInline(admin.StackedInline):
def get_fieldsets(self, request, obj=None):
fields = (
('label', 'field_type', 'required'),
'initial', 'placeholder_text', 'help_text',
'choice_values', 'position',
'initial', 'placeholder_text', 'help_text',
'choice_values', 'position',
)

if settings.DJANGOCMS_FORMS_ALLOW_CUSTOM_FIELD_NAME:
Expand Down Expand Up @@ -101,7 +101,7 @@ def get_fieldsets(self, request, obj=None):
'Choose storage options to capture form data. You can enter '
'an email address to have the form submissions emailed to you or '
'log all the form submissions to the database.',
'fields': ('email_to', 'email_from', 'email_subject',
'fields': ('email_to', 'email_from', 'email_reply_to', 'email_subject',
'email_uploaded_files', 'save_data', 'spam_protection', ),
}),
)
Expand Down
15 changes: 14 additions & 1 deletion djangocms_forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ def save_to_db(self, form_data, request, referrer):
def email_submission(self, form_data, request, referrer):
mail_to = re.compile('\s*[,;]+\s*').split(self.form_definition.email_to)
mail_from = self.form_definition.email_from or None
mail_reply_to = self.form_definition.email_reply_to or None
mail_subject = self.form_definition.email_subject or \
'Form Submission - %s' % self.form_definition.name
context = {
Expand All @@ -352,10 +353,22 @@ def email_submission(self, form_data, request, referrer):
'recipients': mail_to,
}

for field in form_data:
if mail_reply_to:
mail_reply_to = mail_reply_to.replace('{{{0}}}'.format(field['label']),
str(field['value']))

mail_subject = mail_subject.replace('{{{0}}}'.format(field['label']),
str(field['value']))

if mail_reply_to:
mail_reply_to = (mail_reply_to,)

message = render_to_string('djangocms_forms/email_template/email.txt', context)
message_html = render_to_string('djangocms_forms/email_template/email.html', context)

email = EmailMultiAlternatives(mail_subject, message, mail_from, mail_to)
email = EmailMultiAlternatives(mail_subject, message, mail_from, mail_to,
reply_to=mail_reply_to)
email.attach_alternative(message_html, 'text/html')

if self.form_definition.email_uploaded_files:
Expand Down
26 changes: 26 additions & 0 deletions djangocms_forms/migrations/0006_formfield_email_reply_to.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.12 on 2018-05-01 09:46
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('djangocms_forms', '0005_formfield_field_name'),
]

operations = [
migrations.AddField(
model_name='formdefinition',
name='email_reply_to',
field=models.CharField(blank=True, help_text='Reply to e-mail header. Use form field values by inserting placeholders of field names, e.g. "{my name field} <{my email field}>"', max_length=255, verbose_name='Reply to'),
),
migrations.AlterField(
model_name='formdefinition',
name='email_subject',
field=models.CharField(blank=True, help_text='Use form field values by inserting placeholders of field names, e.g. "{my subject}"', max_length=255, verbose_name='Email Subject'),
),
]
7 changes: 6 additions & 1 deletion djangocms_forms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ class FormDefinition(CMSPlugin):
_('Send form data to e-mail address'), max_length=255, blank=True,
help_text=_('Separate several addresses with a comma.'))
email_from = models.EmailField(_('Sender Email Address'), max_length=255, blank=True)
email_subject = models.CharField(_('Email Subject'), max_length=255, blank=True)
email_reply_to = models.CharField(_('Reply to'), max_length=255, blank=True,
help_text=_('Reply to e-mail header. Use form field values by inserting placeholders of '
'field names, e.g. "{my name field} <{my email field}>"'))
email_subject = models.CharField(_('Email Subject'), max_length=255, blank=True,
help_text=_('Use form field values by inserting placeholders of '
'field names, e.g. "{my subject}"'))
email_uploaded_files = models.BooleanField(
_('Send uploaded files as email attachments'), default=True)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
$(this).ajaxSubmit(ajaxOptions);
});

if (typeof(grecaptcha) == 'undefined') {
if (typeof(grecaptcha) == 'undefined') || typeof(grecaptcha.render) =='undefined') {
window.reCapctchaOnloadCallback = function() {
this.renderReCaptcha();
}.bind(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ <h1>{{ form.title }}</h1>
{% for item in form_data %}
<p>
<strong>{% if item.label %}{{ item.label }}{% else %}{{ item.name }}{% endif %}:</strong><br/>
{{ item.value|friendly|default_if_none:'—' }}
{{ item.value|friendly|default_if_none:'—'|linebreaksbr }}
</p>
{% endfor %}
{% trans 'Sent via' %} <a href="http://{{ request.get_host }}{{ referrer }}">{{ request.get_host }}{{ referrer }}</a>
Expand Down