def testmail(request): try: kwargs = dict(instance=models.Email.objects.order_by('-id')[0]) except IndexError: kwargs = {} form = forms.EmailForm(request.POST, **kwargs) if not form.is_valid(): return JsonResp(request, form=form) email = bsdUsers.objects.get(bsdusr_username='******').bsdusr_email if not email: return JsonResp( request, error=True, message=_( "You must configure the root email (Accounts->Users->root)"), ) sid = transaction.savepoint() form.save() error = False if request.is_ajax(): sw_name = get_sw_name() error, errmsg = send_mail( subject=_('Test message from %s' % (sw_name)), text=_('This is a message test from %s' % (sw_name, ))) if error: errmsg = _("Your test email could not be sent: %s") % errmsg else: errmsg = _('Your test email has been sent!') transaction.savepoint_rollback(sid) return JsonResp(request, error=error, message=errmsg)
def testmail(request): try: kwargs = dict(instance=models.Email.objects.order_by('-id')[0]) except IndexError: kwargs = {} fromwizard = False data = request.POST.copy() for key, value in data.items(): if key.startswith('system-'): fromwizard = True data[key.replace('system-', '')] = value form = forms.EmailForm(data, **kwargs) if not form.is_valid(): return JsonResp(request, form=form) if fromwizard: allfield = 'system-__all__' else: allfield = '__all__' if fromwizard: email = request.POST.get('system-sys_email') errmsg = _('You must provide a Root E-mail') else: email = bsdUsers.objects.get(bsdusr_username='******').bsdusr_email errmsg = _('You must configure the root email (Accounts->Users->root)') if not email: form.errors[allfield] = form.error_class([errmsg]) return JsonResp( request, form=form, ) sid = transaction.savepoint() form.save() error = False if request.is_ajax(): sw_name = get_sw_name() error, errmsg = send_mail(subject=_('Test message from %s') % sw_name, text=_('This is a message test from %s') % sw_name, to=[email]) if error: errmsg = _("Your test email could not be sent: %s") % errmsg else: errmsg = _('Your test email has been sent!') transaction.savepoint_rollback(sid) form.errors[allfield] = form.error_class([errmsg]) return JsonResp( request, form=form, )