5 from email.mime.multipart import MIMEMultipart
6 from email.mime.text import MIMEText
7 from email.mime.image import MIMEImage
9 from django.core.mail import DNS_NAME
10 from smtplib import SMTP
12 from forum import settings
13 from django.template import loader, Context, Template
14 from forum.utils.html import sanitize_html
15 from forum.context import application_settings
16 from forum.utils.html2text import HTML2Text
17 from threading import Thread
19 def send_msg_list(msgs, sender=None):
21 connection = SMTP(str(settings.EMAIL_HOST), str(settings.EMAIL_PORT),
22 local_hostname=DNS_NAME.get_fqdn())
25 if (bool(settings.EMAIL_USE_TLS)):
30 if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD:
31 connection.login(str(settings.EMAIL_HOST_USER), str(settings.EMAIL_HOST_PASSWORD))
34 sender = str(settings.DEFAULT_FROM_EMAIL)
36 for email, msg in msgs:
38 connection.sendmail(sender, [email], msg)
43 except socket.sslerror:
48 def html2text(s, ignore_tags=(), indent_width=4, page_width=80):
49 ignore_tags = [t.lower() for t in ignore_tags]
50 parser = HTML2Text(ignore_tags, indent_width, page_width)
57 if isinstance(data, (tuple, list)) and len(data) == 2:
58 return '%s <%s>' % data
62 def create_msg(subject, sender, recipient, html, text, images):
63 msgRoot = MIMEMultipart('related')
64 msgRoot['Subject'] = subject
65 msgRoot['From'] = named(sender)
66 msgRoot['To'] = named(recipient)
67 msgRoot.preamble = 'This is a multi-part message from %s.' % unicode(settings.APP_SHORT_NAME).encode('utf8')
69 msgAlternative = MIMEMultipart('alternative')
70 msgRoot.attach(msgAlternative)
72 msgAlternative.attach(MIMEText(text, _charset='utf-8'))
73 msgAlternative.attach(MIMEText(html, 'html', _charset='utf-8'))
77 fp = open(img[0], 'rb')
78 msgImage = MIMEImage(fp.read())
80 msgImage.add_header('Content-ID', '<'+img[1]+'>')
81 msgRoot.attach(msgImage)
85 return msgRoot.as_string()
87 def send_email(subject, recipients, template, context={}, sender=None, images=[], threaded=True):
89 sender = (unicode(settings.APP_SHORT_NAME), unicode(settings.DEFAULT_FROM_EMAIL))
92 images = [(os.path.join(str(settings.UPFILES_FOLDER), os.path.basename(str(settings.APP_LOGO))), 'logo')]
94 context.update(application_settings(None))
95 html_body = loader.get_template(template).render(Context(context))
96 txt_body = html2text(html_body)
98 if isinstance(recipients, str):
99 recipients = [recipients]
103 for recipient in recipients:
104 if isinstance(recipient, str):
105 recipient_data = ('recipient', recipient)
106 recipient_context = None
107 elif isinstance(recipient, (list, tuple)) and len(recipient) == 2:
108 name, email = recipient
109 recipient_data = (name, email)
110 recipient_context = None
111 elif isinstance(recipient, (list, tuple)) and len(recipient) == 3:
112 name, email, recipient_context = recipient
113 recipient_data = (name, email)
115 raise Exception('bad argument for recipients')
117 if recipient_context is not None:
118 recipient_context = Context(recipient_context)
119 msg_html = Template(html_body).render(recipient_context)
120 msg_txt = Template(txt_body).render(recipient_context)
125 msg = create_msg(subject, sender, recipient_data, msg_html, msg_txt, images)
126 msgs.append((email, msg))
129 thread = Thread(target=send_msg_list, args=[msgs])
130 thread.setDaemon(True)
136 def send_template_email(recipients, template, context):
137 t = loader.get_template(template)
138 context.update(dict(recipients=recipients, settings=settings))
139 t.render(Context(context))
141 def create_and_send_mail_messages(messages):
142 sender = '%s <%s>' % (unicode(settings.APP_SHORT_NAME), unicode(settings.DEFAULT_FROM_EMAIL))
144 connection = SMTP(str(settings.EMAIL_HOST), str(settings.EMAIL_PORT),
145 local_hostname=DNS_NAME.get_fqdn())
148 if (bool(settings.EMAIL_USE_TLS)):
150 connection.starttls()
153 if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD:
154 connection.login(str(settings.EMAIL_HOST_USER), str(settings.EMAIL_HOST_PASSWORD))
157 sender = str(settings.DEFAULT_FROM_EMAIL)
159 for recipient, subject, html, text, media in messages:
160 msgRoot = MIMEMultipart('related')
161 msgRoot.set_charset('utf-8')
162 msgRoot['Subject'] = subject
163 msgRoot['From'] = sender
164 msgRoot['To'] = '%s <%s>' % (recipient.username, recipient.email)
165 msgRoot.preamble = 'This is a multi-part message from %s.' % unicode(settings.APP_SHORT_NAME).encode('utf8')
167 msgAlternative = MIMEMultipart('alternative')
168 msgRoot.attach(msgAlternative)
170 msgAlternative.attach(MIMEText(text))
171 msgAlternative.attach(MIMEText(html, 'html'))
173 for alias, location in media.items():
174 fp = open(location, 'rb')
175 msgImage = MIMEImage(fp.read())
177 msgImage.add_header('Content-ID', '<'+alias+'>')
178 msgRoot.attach(msgImage)
181 connection.sendmail(sender, [recipient.email], msgRoot.as_string())
187 except socket.sslerror: