5 from email.MIMEMultipart import MIMEMultipart
\r
6 from email.MIMEText import MIMEText
\r
7 from email.MIMEImage import MIMEImage
\r
9 from django.core.mail import DNS_NAME
\r
10 from smtplib import SMTP
\r
11 import email.Charset
\r
12 from django.conf import settings
\r
13 from django.template import loader, Context, Template
\r
14 from forum.utils.html import sanitize_html
\r
15 from forum.context import application_settings
\r
16 from forum.utils.html2text import HTML2Text
\r
17 from threading import Thread
\r
19 def send_msg_list(msgs, sender=None):
\r
21 connection = SMTP(str(settings.EMAIL_HOST), str(settings.EMAIL_PORT),
\r
22 local_hostname=DNS_NAME.get_fqdn())
\r
25 if (bool(settings.EMAIL_USE_TLS)):
\r
27 connection.starttls()
\r
30 if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD:
\r
31 connection.login(str(settings.EMAIL_HOST_USER), str(settings.EMAIL_HOST_PASSWORD))
\r
34 sender = str(settings.DEFAULT_FROM_EMAIL)
\r
36 for email, msg in msgs:
\r
38 connection.sendmail(sender, [email], msg)
\r
39 except Exception, e:
\r
43 except socket.sslerror:
\r
45 except Exception, e:
\r
48 def html2text(s, ignore_tags=(), indent_width=4, page_width=80):
\r
49 ignore_tags = [t.lower() for t in ignore_tags]
\r
50 parser = HTML2Text(ignore_tags, indent_width, page_width)
\r
54 return parser.result
\r
57 if isinstance(data, (tuple, list)) and len(data) == 2:
\r
58 return '%s <%s>' % data
\r
62 def create_msg(subject, sender, recipient, html, text, images):
\r
63 msgRoot = MIMEMultipart('related')
\r
64 msgRoot['Subject'] = subject
\r
65 msgRoot['From'] = named(sender)
\r
66 msgRoot['To'] = named(recipient)
\r
67 msgRoot.preamble = 'This is a multi-part message from %s.' % str(settings.APP_SHORT_NAME)
\r
69 msgAlternative = MIMEMultipart('alternative')
\r
70 msgRoot.attach(msgAlternative)
\r
72 msgAlternative.attach(MIMEText(text, _charset='utf-8'))
\r
73 msgAlternative.attach(MIMEText(html, 'html', _charset='utf-8'))
\r
77 fp = open(img[0], 'rb')
\r
78 msgImage = MIMEImage(fp.read())
\r
80 msgImage.add_header('Content-ID', '<'+img[1]+'>')
\r
81 msgRoot.attach(msgImage)
\r
85 return msgRoot.as_string()
\r
87 def send_email(subject, recipients, template, context={}, sender=None, images=[], threaded=True):
\r
89 sender = (str(settings.APP_SHORT_NAME), str(settings.DEFAULT_FROM_EMAIL))
\r
91 images.append((os.path.join(str(settings.UPFILES_FOLDER), os.path.basename(str(settings.APP_LOGO))), 'logo'))
\r
93 context.update(application_settings(None))
\r
94 html_body = loader.get_template(template).render(Context(context))
\r
95 txt_body = html2text(html_body)
\r
97 if isinstance(recipients, str):
\r
98 recipients = [recipients]
\r
102 for recipient in recipients:
\r
103 if isinstance(recipient, str):
\r
104 recipient_data = ('recipient', recipient)
\r
105 recipient_context = None
\r
106 elif isinstance(recipient, (list, tuple)) and len(recipient) == 2:
\r
107 name, email = recipient
\r
108 recipient_data = (name, email)
\r
109 recipient_context = None
\r
110 elif isinstance(recipient, (list, tuple)) and len(recipient) == 3:
\r
111 name, email, recipient_context = recipient
\r
112 recipient_data = (name, email)
\r
114 raise Exception('bad argument for recipients')
\r
116 if recipient_context is not None:
\r
117 recipient_context = Context(recipient_context)
\r
118 msg_html = Template(html_body).render(recipient_context)
\r
119 msg_txt = Template(txt_body).render(recipient_context)
\r
121 msg_html = html_body
\r
124 msg = create_msg(subject, sender, recipient_data, msg_html, msg_txt, images)
\r
125 msgs.append((email, msg))
\r
128 thread = Thread(target=send_msg_list, args=[msgs])
\r
129 thread.setDaemon(True)
\r