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
11 from email.MIMEMultipart import MIMEMultipart
12 from email.MIMEText import MIMEText
13 from email.MIMEImage import MIMEImage
14 from email.Header import Header
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
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))
30 def create_and_send_mail_messages(messages):
31 if not settings.EMAIL_HOST:
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))
39 connection = SMTP(str(settings.EMAIL_HOST), str(settings.EMAIL_PORT),
40 local_hostname=DNS_NAME.get_fqdn())
42 if (bool(settings.EMAIL_USE_TLS)):
47 if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD:
48 connection.login(str(settings.EMAIL_HOST_USER), str(settings.EMAIL_HOST_PASSWORD))
51 sender = str(settings.DEFAULT_FROM_EMAIL)
53 for recipient, subject, html, text, media in messages:
54 msgRoot = MIMEMultipart('related')
56 msgRoot['Subject'] = Header(subject, 'utf-8')
57 msgRoot['From'] = sender
59 to = Header(recipient.username, 'utf-8')
60 to.append('<%s>' % recipient.email)
63 msgRoot.preamble = 'This is a multi-part message from %s.' % unicode(settings.APP_SHORT_NAME).encode('utf8')
65 msgAlternative = MIMEMultipart('alternative')
66 msgRoot.attach(msgAlternative)
68 msgAlternative.attach(MIMEText(text.encode('utf-8'), _charset='utf-8'))
69 msgAlternative.attach(MIMEText(html.encode('utf-8'), 'html', _charset='utf-8'))
71 for alias, location in media.items():
72 fp = open(location, 'rb')
73 msgImage = MIMEImage(fp.read())
75 msgImage.add_header('Content-ID', '<'+alias+'>')
76 msgRoot.attach(msgImage)
79 connection.sendmail(sender, [recipient.email], msgRoot.as_string())
85 except socket.sslerror: