forward questions to info mail

This commit is contained in:
erichhasl
2017-12-07 17:36:55 +01:00
parent 4e445b1c1b
commit aac7e5bc4e
3 changed files with 27 additions and 13 deletions
+19 -12
View File
@@ -1,7 +1,5 @@
from django.core import mail
from django.core.mail import EmailMessage
from django.conf import settings
from django.core.mail.backends.smtp import EmailBackend
NOT_SENT, SENT, PARTLY_SENT = 0, 1, 2
@@ -14,20 +12,29 @@ def send_from_arbeitsministerium(subject, content, recipients, reply_to=None):
auth_password=settings.EMAIL_ARBEITSMINISTERIUM_PASSWORD)
def send_from_info(subject, content, recipients, reply_to=None):
return send(subject, content, settings.EMAIL_INFO, recipients,
reply_to=reply_to,
auth_user=settings.EMAIL_INFO_USER,
auth_password=settings.EMAIL_INFO_PASSWORD)
def send(subject, content, sender, recipients, reply_to=None,
auth_user=None, auth_password=None):
failed, succeeded = False, False
if type(recipients) != list:
recipients = [recipients]
for recipient in set(recipients):
try:
mail.send_mail(subject, content, sender, recipients,
auth_user=auth_user,
auth_password=auth_password)
except Exception as e:
print("Error when sending mail:", e)
failed = True
else:
succeeded = True
with mail.get_connection(username=auth_user, password=auth_password) as conn:
for recipient in set(recipients):
try:
msg = mail.EmailMessage(subject, content, sender, recipients,
reply_to=reply_to,
connection=conn)
msg.send()
except Exception as e:
print("Error when sending mail:", e)
failed = True
else:
succeeded = True
return NOT_SENT if failed and not succeeded else SENT if not failed\
and succeeded else PARTLY_SENT
+4 -1
View File
@@ -1,8 +1,9 @@
from django.shortcuts import render
from django import forms
from django.conf import settings
from captcha.fields import CaptchaField
from .models import Betrieb, Partei, PresidentCandidate, Question
from .mailutils import send_from_arbeitsministerium
from .mailutils import send_from_arbeitsministerium, send_from_info
class BetriebForm(forms.Form):
@@ -106,6 +107,8 @@ def question_new(request):
ip_address=get_client_ip(request),
answered=False)
question.save()
send_from_info(question.subject, question.content,
settings.EMAIL_INFO, reply_to=[question.email])
return render_confirmation(request)
else:
form = QuestionForm()