5 from email.mime.multipart import MIMEMultipart
6 from email.mime.text import MIMEText
7 from email.mime.image import MIMEImage
8 from email.header import Header
11 from django.core.mail import DNS_NAME
12 from smtplib import SMTP
13 from forum import settings
14 from django.template import loader, Context, Template
15 from forum.utils.html import sanitize_html
16 from forum.context import application_settings
17 from forum.utils.html2text import HTML2Text
18 from threading import Thread
20 def send_msg_list(msgs, sender=None):
22 connection = SMTP(str(settings.EMAIL_HOST), str(settings.EMAIL_PORT),
23 local_hostname=DNS_NAME.get_fqdn())
26 if (bool(settings.EMAIL_USE_TLS)):
31 if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD:
32 connection.login(str(settings.EMAIL_HOST_USER), str(settings.EMAIL_HOST_PASSWORD))
35 sender = str(settings.DEFAULT_FROM_EMAIL)
37 for email, msg in msgs:
39 connection.sendmail(sender, [email], msg)
44 except socket.sslerror:
49 def html2text(s, ignore_tags=(), indent_width=4, page_width=80):
50 ignore_tags = [t.lower() for t in ignore_tags]
51 parser = HTML2Text(ignore_tags, indent_width, page_width)
58 if isinstance(data, (tuple, list)) and len(data) == 2:
59 return '%s <%s>' % data
63 def create_msg(subject, sender, recipient, html, text, images):
64 msgRoot = MIMEMultipart('related')
65 msgRoot['Subject'] = subject
66 msgRoot['From'] = named(sender)
67 msgRoot['To'] = named(recipient)
68 msgRoot.preamble = 'This is a multi-part message from %s.' % unicode(settings.APP_SHORT_NAME).encode('utf8')
70 msgAlternative = MIMEMultipart('alternative')
71 msgRoot.attach(msgAlternative)
73 msgAlternative.attach(MIMEText(text, _charset='utf-8'))
74 msgAlternative.attach(MIMEText(html, 'html', _charset='utf-8'))
78 fp = open(img[0], 'rb')
79 msgImage = MIMEImage(fp.read())
81 msgImage.add_header('Content-ID', '<'+img[1]+'>')
82 msgRoot.attach(msgImage)
86 return msgRoot.as_string()
88 def send_email(subject, recipients, template, context={}, sender=None, images=[], threaded=True):
90 sender = (unicode(settings.APP_SHORT_NAME), unicode(settings.DEFAULT_FROM_EMAIL))
93 images = [(os.path.join(str(settings.UPFILES_FOLDER), os.path.basename(str(settings.APP_LOGO))), 'logo')]
95 context.update(application_settings(None))
96 html_body = loader.get_template(template).render(Context(context))
97 txt_body = html2text(html_body)
99 if isinstance(recipients, str):
100 recipients = [recipients]
104 for recipient in recipients:
105 if isinstance(recipient, str):
106 recipient_data = ('recipient', recipient)
107 recipient_context = None
108 elif isinstance(recipient, (list, tuple)) and len(recipient) == 2:
109 name, email = recipient
110 recipient_data = (name, email)
111 recipient_context = None
112 elif isinstance(recipient, (list, tuple)) and len(recipient) == 3:
113 name, email, recipient_context = recipient
114 recipient_data = (name, email)
116 raise Exception('bad argument for recipients')
118 if recipient_context is not None:
119 recipient_context = Context(recipient_context)
120 msg_html = Template(html_body).render(recipient_context)
121 msg_txt = Template(txt_body).render(recipient_context)
126 msg = create_msg(subject, sender, recipient_data, msg_html, msg_txt, images)
127 msgs.append((email, msg))
130 thread = Thread(target=send_msg_list, args=[msgs])
131 thread.setDaemon(True)
137 def send_template_email(recipients, template, context):
138 t = loader.get_template(template)
139 context.update(dict(recipients=recipients, settings=settings))
140 t.render(Context(context))
142 def create_and_send_mail_messages(messages):
143 if not settings.EMAIL_HOST:
146 sender = Header(unicode(settings.APP_SHORT_NAME), 'utf-8')
147 sender.append('<%s>' % unicode(settings.DEFAULT_FROM_EMAIL))
148 sender = u'%s <%s>' % (unicode(settings.APP_SHORT_NAME), unicode(settings.DEFAULT_FROM_EMAIL))
151 connection = SMTP(str(settings.EMAIL_HOST), str(settings.EMAIL_PORT),
152 local_hostname=DNS_NAME.get_fqdn())
154 if (bool(settings.EMAIL_USE_TLS)):
156 connection.starttls()
159 if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD:
160 connection.login(str(settings.EMAIL_HOST_USER), str(settings.EMAIL_HOST_PASSWORD))
163 sender = str(settings.DEFAULT_FROM_EMAIL)
165 for recipient, subject, html, text, media in messages:
166 msgRoot = MIMEMultipart('related')
168 msgRoot['Subject'] = Header(subject, 'utf-8')
169 msgRoot['From'] = sender
171 to = Header(recipient.username, 'utf-8')
172 to.append('<%s>' % recipient.email)
175 msgRoot.preamble = 'This is a multi-part message from %s.' % unicode(settings.APP_SHORT_NAME).encode('utf8')
177 msgAlternative = MIMEMultipart('alternative')
178 msgRoot.attach(msgAlternative)
180 msgAlternative.attach(MIMEText(text.encode('utf-8'), _charset='utf-8'))
181 msgAlternative.attach(MIMEText(html.encode('utf-8'), 'html', _charset='utf-8'))
183 for alias, location in media.items():
184 fp = open(location, 'rb')
185 msgImage = MIMEImage(fp.read())
187 msgImage.add_header('Content-ID', '<'+alias+'>')
188 msgRoot.attach(msgImage)
191 connection.sendmail(sender, [recipient.email], msgRoot.as_string())
197 except socket.sslerror: