forward email to custom fallback address (#10528)

This commit is contained in:
Serghei Mihai 2016-04-09 15:49:16 +02:00
parent c25ddea70f
commit d7d78c7491
1 changed files with 36 additions and 48 deletions

View File

@ -6,6 +6,7 @@ import email
import tempfile
import uuid
import smtplib
import subprocess
from redmine import Redmine
@ -13,6 +14,7 @@ REDMINE_URL = os.environ.get('REDMINE_URL', 'https://dev.entrouvert.org')
REDMINE_KEY = os.environ.get('REDMINE_KEY')
TRACKER_ID = os.environ.get('REDMINE_TRACKER', '3')
PROJECT_ID = os.environ.get('PROJECT')
FALLBACK_EMAIL = os.environ.get('FALLBACK_EMAIL', 'support@entrouvert.com')
class UnknownUser(Exception):
@ -56,33 +58,32 @@ def send_mail(to, subject, message):
s.quit()
def create_ticket(mail):
try:
mail = email.message_from_string(mail)
r = Redmine(REDMINE_URL, key=REDMINE_KEY)
from_name, from_email = email.utils.parseaddr(mail['From'])
users = r.user.filter(name=from_email)
if not users:
raise UnknownUser
def create_ticket(email):
mail = email.message_from_string(email)
r = Redmine(REDMINE_URL, key=REDMINE_KEY)
from_name, from_email = email.utils.parseaddr(mail['From'])
users = r.user.filter(name=from_email)
if not users:
raise UnknownUser
r = Redmine(REDMINE_URL, key=REDMINE_KEY, impersonate=users[0].login)
r = Redmine(REDMINE_URL, key=REDMINE_KEY, impersonate=users[0].login)
attachments = []
attachments = []
for data in mail.walk():
attachment = parse_attachment(data)
if attachment:
attachments.append(attachment)
elif data.get_content_type() == "text/plain":
body = data.get_payload(decode=True)
body = unicode(body, data.get_content_charset('utf-8')).encode('utf-8')
for data in mail.walk():
attachment = parse_attachment(data)
if attachment:
attachments.append(attachment)
elif data.get_content_type() == "text/plain":
body = data.get_payload(decode=True)
body = unicode(body, data.get_content_charset('utf-8')).encode('utf-8')
issue = r.issue.create(project_id=PROJECT_ID,
subject=parse_header(mail['Subject']),
tracker_id=TRACKER_ID,
description=body,
uploads=attachments)
message = u"""Bonjour,
issue = r.issue.create(project_id=PROJECT_ID,
subject=parse_header(mail['Subject']),
tracker_id=TRACKER_ID,
description=body,
uploads=attachments)
message = u"""Bonjour,
Votre demande a bien été prise en compte et enregistrée dans notre système sous
le numéro %s.
@ -92,36 +93,23 @@ Vous pouvez suivre son évolution à l'adresse : %s .
Cordialement,
L'équipe Entr'ouvert"""
message = message % (issue.id, issue.url)
send_mail(mail['From'], u'Votre demande a été bien prise en compte', message)
# cleanup temporary attachment files
for attachment in attachments:
if os.path.exists(attachment['path']):
os.unlink(attachment['path'])
except Exception, e:
message = u"""Bonjour,
Malheureusement nous ne sommes pas en mesure de prendre en compte votre demande
pour une des raisons suivantes :
- vous ne disposez pas de compte sur notre plateforme;
- vous n'avez pas les droits d'accès au projet;
Veuillez vous assurer que vous remplissez les condition et re-essayez.
Cordialement,
L'équipe Entr'ouvert"""
send_mail(mail['From'], u'Rejet de votre demande', message)
message = message % (issue.id, issue.url)
send_mail(mail['From'], u'Votre demande a été bien prise en compte', message)
# cleanup temporary attachment files
for attachment in attachments:
if os.path.exists(attachment['path']):
os.unlink(attachment['path'])
if __name__ == '__main__':
mail = sys.stdin.read()
email = sys.stdin.read()
mail_dumps_dir = '/var/tmp'
if not os.path.exists(mail_dumps_dir):
os.mkdir(mail_dumps_dir)
filename = os.path.join(mail_dumps_dir, '%s.mail' % uuid.uuid4())
with open(filename, 'w') as mail_dump:
mail_dump.write(mail)
create_ticket(mail)
mail_dump.write(email)
try:
create_ticket(email)
except:
exim = subprocess.Popen(['/usr/sbin/exim', FALLBACK_EMAIL], stdin=file(filename))