예제 #1
0
    def send(self, recipient_email, request, ts, instance=None, multipart=False):
        current_site = Site.objects.get_current()
        if instance:
            order = ContactPlus.objects.get(id=instance.id).extrafield_set.order_by('inline_ordering_position')
            excluded_field_types = ['MathCaptcha', 'ReCaptcha']
            order = [field for field in order if field.fieldType not in excluded_field_types]
            ordered_dic_list = []
            for field in order:
                key = slugify(field.label)
                value = self.cleaned_data.get(key, '(no input)')
                # redefine value for files... 
                if field.fieldType in ["FileField", "ImageField"]:
                    val = ts + '-' + str(value)
                    if settings.MEDIA_URL.startswith("http"):
                        value = "%s%s" % (settings.MEDIA_URL, val)
                    else:
                        value = "http://%s%s%s" % (current_site, settings.MEDIA_URL, val)
                ordered_dic_list.append({field.label: value})

        # Automatically match reply-to email address in form
        tmp_headers = {}
        cc_list = []
        try:
            reply_email_label = getattr(settings, 'CONTACT_PLUS_REPLY_EMAIL_LABEL', None)
            if reply_email_label is not None:
                tmp_headers.update({'Reply-To': self.cleaned_data[reply_email_label]})
        except:
            pass

        try:
            cc_address_label = getattr(settings, 'CONTACT_PLUS_REPLY_EMAIL_LABEL', None)
            cc_address = self.cleaned_data.get(cc_address_label, None)
            send_copy = getattr(settings, 'CONTACT_PLUS_SEND_COPY_TO_REPLY_EMAIL', False)
            if cc_address and send_copy:
                cc_list.append(cc_address)
        except:
            pass

        email_message = EmailMessage(
            subject=instance.email_subject,
            body=render_to_string("cmsplugin_contact_plus/email.txt", {'data': self.cleaned_data,
                                                                      'ordered_data': ordered_dic_list,
                                                                      'instance': instance,
                                                                      }),
            cc=cc_list,
            from_email=getattr(settings, 'CONTACT_PLUS_FROM_EMAIL', settings.DEFAULT_FROM_EMAIL),
            to=[recipient_email, ],
            headers=tmp_headers,
        )
        email_message.send(fail_silently=True)

        if instance.collect_records:# and not multipart:
            record = ContactRecord(contact_form=instance, data=ordered_dic_list)#self.cleaned_data)
            record.save()

        contact_message_sent.send(sender=self, data=self.cleaned_data)
    def send(self, recipient_email, request, ts, instance=None, multipart=False):
        current_site = Site.objects.get_current()
        if instance:
            order = ContactPlus.objects.get(id=instance.id).extrafield_set.order_by('inline_ordering_position')
            excluded_field_types = ['MathCaptcha', 'ReCaptcha']
            order = [field for field in order if field.fieldType not in excluded_field_types]
            ordered_dic_list = []
            for field in order:
                key = slugify(field.label)
                value = self.cleaned_data.get(key, '(no input)')
                # redefine value for files... 
                if field.fieldType in ["FileField", "ImageField"]:
                    val = ts + '-' + str(value)
                    if settings.MEDIA_URL.startswith("http"):
                        value = "%s%s" % (settings.MEDIA_URL, val)
                    else:
                        value = "http://%s%s%s" % (current_site, settings.MEDIA_URL, val)
                ordered_dic_list.append({field.label: value})

        # Automatically match reply-to email adress in form
        tmp_headers = {}
        try:
            reply_email_label = getattr(settings, 'CONTACT_PLUS_REPLY_EMAIL_LABEL', None)
            if reply_email_label is not None:
                tmp_headers.update({'Reply-To': self.cleaned_data[reply_email_label]})
        except:
            pass

        email_message = EmailMessage(
            subject=instance.email_subject,
            body=render_to_string("cmsplugin_contact_plus/email.txt", {'data': self.cleaned_data,
                                                                      'ordered_data': ordered_dic_list,
                                                                      'instance': instance,
                                                                      }),
            from_email=getattr(settings, 'DEFAULT_FROM_EMAIL', None),
            to=[recipient_email, ],
            headers=tmp_headers,
        )
        email_message.send(fail_silently=True)

        if instance.collect_records:# and not multipart:
            record = ContactRecord(contact_form=instance, data=ordered_dic_list)#self.cleaned_data)
            record.save()

        contact_message_sent.send(sender=self, data=self.cleaned_data)
예제 #3
0
    def send(self, recipient_email, request, instance=None, multipart=False):
        current_site = Site.objects.get_current()
        if instance:
            order = ContactPlus.objects.get(id=instance.id).extrafield_set.order_by('inline_ordering_position')
            ordered_dic_list = []

            for field in order:
                key = slugify(field.label)
                value = self.cleaned_data.get(key, '(no input)')
                ordered_dic_list.append({field.label: value})

        # Automatically match reply-to email adress in form
        tmp_headers = {}
        try:
            reply_email_label = getattr(settings, 'CONTACT_PLUS_REPLY_EMAIL_LABEL', None)
            if reply_email_label is not None:
                tmp_headers.update({'Reply-To': self.cleaned_data[reply_email_label]})
        except:
            pass

        email_message = EmailMessage(
            "[" + current_site.domain.upper() + "]",
                render_to_string("cmsplugin_contact_plus/email.txt", {'data': self.cleaned_data,
                                                                      'ordered_data': ordered_dic_list,
                                                                      'instance': instance,
                                                                      }),
                    from_email=getattr(settings, 'DEFAULT_FROM_EMAIL', None),
                    to=[recipient_email, ],
                    headers=tmp_headers,)
        email_message.send(fail_silently=True)

        if instance.collect_records and not multipart:
            record = ContactRecord(contact_form=instance, data=self.cleaned_data)
            record.save()
    
        contact_message_sent.send(sender=self, data=self.cleaned_data)
예제 #4
0
    def send(self, recipient_email, request, ts, instance=None, multipart=False):
        current_site = Site.objects.get_current()
        if instance:
            order = ContactPlus.objects.get(id=instance.id).extrafield_set.order_by('inline_ordering_position')
            excluded_field_types = ['MathCaptcha', 'ReCaptcha']
            order = [field for field in order if field.fieldType not in excluded_field_types]
            ordered_dic_list = []
            for field in order:
                key = slugify(field.label)
                value = self.cleaned_data.get(key, '(no input)')
                # redefine value for files... 
                if field.fieldType in ["FileField", "ImageField"]:
                    val = ts + '-' + get_valid_filename(value)
                    if settings.MEDIA_URL.startswith("http"):
                        value = "%s%s" % (settings.MEDIA_URL, val)
                    else:
                        value = "http://%s%s%s" % (current_site, settings.MEDIA_URL, urlquote(val))
                ordered_dic_list.append({field.label: value})

        # Automatically match reply-to email address in form
        tmp_headers = {}
        cc_list = []
        try:
            reply_email_label = getattr(settings, 'CONTACT_PLUS_REPLY_EMAIL_LABEL', None)
            if reply_email_label is not None:
                tmp_headers.update({'Reply-To': self.cleaned_data[reply_email_label]})
        except:
            pass

        try:
            cc_address_label = getattr(settings, 'CONTACT_PLUS_REPLY_EMAIL_LABEL', None)
            cc_address = self.cleaned_data.get(cc_address_label, None)
            send_copy = getattr(settings, 'CONTACT_PLUS_SEND_COPY_TO_REPLY_EMAIL', False)
            if cc_address and send_copy:
                cc_list.append(cc_address)
        except:
            pass
        
        # Site specific from_email via GlobalProperty       
        try:
            from userproperty.models import GlobalProperty
            host = request.get_host()
            site = host.split('.')[1] 
            from_email_property = GlobalProperty.objects.filter(Q(name__startswith='DEFAULT_FROM_EMAIL') &
                                                                Q(name__contains=site)).last()
            from_email = from_email_property.value            
        except:
            from_email = settings.DEFAULT_FROM_EMAIL
            
        email_message = EmailMultiAlternatives(
            subject=instance.email_subject,
            body=render_to_string("cmsplugin_contact_plus/email.txt", {'data': self.cleaned_data,
                                                                      'ordered_data': ordered_dic_list,
                                                                      'instance': instance,
                                                                      }),
            cc=cc_list,
            from_email=getattr(settings, 'CONTACT_PLUS_FROM_EMAIL', from_email),
            to=[recipient_email, ],
            headers=tmp_headers,
        )
        if getattr(settings, 'CONTACT_PLUS_SEND_HTML_EMAIL', False):
            html_content = render_to_string("cmsplugin_contact_plus/email.html", {'data': self.cleaned_data,
                                                                      'ordered_data': ordered_dic_list,
                                                                      'instance': instance,
                                                                      })
            email_message.attach_alternative(html_content, "text/html")
        email_message.send(fail_silently=True)

        if instance.collect_records:# and not multipart:
            record = ContactRecord(contact_form=instance, data=ordered_dic_list)#self.cleaned_data)
            record.save()

        contact_message_sent.send(sender=self, data=self.cleaned_data)
예제 #5
0
    def send(self, recipient_email, request, ts, instance=None, multipart=False):
        current_site = Site.objects.get_current(request=request)
        ordered_dic_list = []

        if instance:
            order = ContactPlus.objects.get(id=instance.id).extrafield_set.order_by('inline_ordering_position')
            excluded_field_types = ['MathCaptcha', 'ReCaptcha']
            order = [field for field in order if field.fieldType not in excluded_field_types]
            for field in order:
                key = slugify(field.label)
                value = self.cleaned_data.get(key, '(no input)')
                # redefine value for files... 
                if field.fieldType in ["FileField", "ImageField"]:
                    val = ts + '-' + str(value)
                    if settings.MEDIA_URL.startswith("http"):
                        value = "%s%s" % (settings.MEDIA_URL, val)
                    else:
                        value = "http://%s%s%s" % (current_site, settings.MEDIA_URL, urlquote(val))
                ordered_dic_list.append({field.label: value})

        # Automatically match reply-to email address in form
        email_headers = {}
        cc_list = []

        reply_email_tag = getattr(settings, 'CONTACT_PLUS_REPLY_EMAIL_TAG', None)
        reply_email = None

        if reply_email_tag:
            for key, field in self.fields.items():
                if reply_email_tag in field.tags:
                    reply_email = self.cleaned_data[key]

                    break
        else:
            reply_email_label = getattr(settings, 'CONTACT_PLUS_REPLY_EMAIL_LABEL', None)

            if reply_email_label and reply_email_label in self.cleaned_data:
                reply_email = self.cleaned_data[reply_email_label]

        if reply_email:
            email_headers.update({'Reply-To': reply_email})

            if getattr(settings, 'CONTACT_PLUS_SEND_COPY_TO_REPLY_EMAIL', False):
                cc_list.append(reply_email)

        template_context = {
            'data': self.cleaned_data,
            'ordered_data': ordered_dic_list,
            'instance': instance,
        }

        template_name = instance.email_template if instance else "cmsplugin_contact_plus/email.txt"

        email_message = EmailMultiAlternatives(
            subject=instance.email_subject,
            body=render_to_string(template_name, template_context),
            cc=cc_list,
            from_email=getattr(settings, 'CONTACT_PLUS_FROM_EMAIL', settings.DEFAULT_FROM_EMAIL),
            to=[recipient_email],
            headers=email_headers,
        )

        if instance:
            template = local_settings.CONTACT_PLUS_EMAIL_TEMPLATES[instance.email_template]

            if 'html' in template:
                try:
                    email_message.attach_alternative(
                        content=render_to_string(template['html'], template_context),
                        mimetype='text/html')
                except TemplateDoesNotExist:
                    pass

        email_message.send(fail_silently=True)

        if instance.collect_records:  # and not multipart:
            record = ContactRecord(contact_form=instance, data=ordered_dic_list)
            record.save()

        contact_message_sent.send(
            sender=self,
            data=self.cleaned_data,
            instance=instance,
            request=request)