]> git.openstreetmap.org Git - osqa.git/blob - forum/utils/mail.py
Should fix the problems some users where having with rosetta.
[osqa.git] / forum / utils / mail.py
1 import email
2 import socket
3 import os
4
5 try:
6     from email.mime.multipart import MIMEMultipart
7     from email.mime.text import MIMEText
8     from email.mime.image import MIMEImage
9     from email.header import Header
10 except:
11     from email.MIMEMultipart import MIMEMultipart
12     from email.MIMEText import MIMEText
13     from email.MIMEImage import MIMEImage
14     from email.Header import Header
15
16 from django.core.mail import DNS_NAME
17 from smtplib import SMTP
18 from forum import settings
19 from django.template import loader, Context, Template
20 from forum.utils.html import sanitize_html
21 from forum.context import application_settings
22 from forum.utils.html2text import HTML2Text
23 from threading import Thread
24
25 def send_template_email(recipients, template, context):
26     t = loader.get_template(template)
27     context.update(dict(recipients=recipients, settings=settings))
28     t.render(Context(context))
29
30 def create_and_send_mail_messages(messages):
31     if not settings.EMAIL_HOST:
32         return
33
34     sender = Header(unicode(settings.APP_SHORT_NAME), 'utf-8')
35     sender.append('<%s>' % unicode(settings.DEFAULT_FROM_EMAIL))
36     sender = u'%s <%s>' % (unicode(settings.APP_SHORT_NAME), unicode(settings.DEFAULT_FROM_EMAIL))
37
38     try:
39         connection = SMTP(str(settings.EMAIL_HOST), str(settings.EMAIL_PORT),
40                           local_hostname=DNS_NAME.get_fqdn())
41
42         if (bool(settings.EMAIL_USE_TLS)):
43             connection.ehlo()
44             connection.starttls()
45             connection.ehlo()
46
47         if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD:
48             connection.login(str(settings.EMAIL_HOST_USER), str(settings.EMAIL_HOST_PASSWORD))
49
50         if sender is None:
51             sender = str(settings.DEFAULT_FROM_EMAIL)
52
53         for recipient, subject, html, text, media in messages:
54             msgRoot = MIMEMultipart('related')
55
56             msgRoot['Subject'] = Header(subject, 'utf-8')
57             msgRoot['From'] = sender
58
59             to = Header(recipient.username, 'utf-8')
60             to.append('<%s>' % recipient.email)
61             msgRoot['To'] = to
62
63             msgRoot.preamble = 'This is a multi-part message from %s.' % unicode(settings.APP_SHORT_NAME).encode('utf8')
64
65             msgAlternative = MIMEMultipart('alternative')
66             msgRoot.attach(msgAlternative)
67
68             msgAlternative.attach(MIMEText(text.encode('utf-8'), _charset='utf-8'))
69             msgAlternative.attach(MIMEText(html.encode('utf-8'), 'html', _charset='utf-8'))
70
71             for alias, location in media.items():
72                 fp = open(location, 'rb')
73                 msgImage = MIMEImage(fp.read())
74                 fp.close()
75                 msgImage.add_header('Content-ID', '<'+alias+'>')
76                 msgRoot.attach(msgImage)
77
78             try:
79                 connection.sendmail(sender, [recipient.email], msgRoot.as_string())
80             except Exception, e:
81                 pass
82
83         try:
84             connection.quit()
85         except socket.sslerror:
86             connection.close()
87     except Exception, e:
88         pass