Workout With Friends
Stay fit with a little motivation
 All Classes Namespaces Files Functions Variables Properties
mailer.py
Go to the documentation of this file.
1 ##
2 #
3 # Email specific helper functions.
4 #
5 
6 from pyramid.renderers import render
7 from pyramid.settings import aslist
8 from pyramid.threadlocal import get_current_request
9 from pyramid_mailer import get_mailer
10 from pyramid_mailer.message import Message
11 
12 
13 ##
14 #
15 # Send an email to the given recipients.
16 #
17 # @param template Path to email template
18 # @param variables Dictionary of variables to pass to the template
19 # @param attachments Files to attach to the email
20 # @param sender Email address of sender, if not the default sender
21 #
22 def send_mail(subject, recipients, template, variables=None, extra_headers=None, attachments=None, sender=None):
23  recipients = aslist(recipients)
24  if variables is None:
25  variables = {}
26  if attachments is not None and not isinstance(attachments, list):
27  attachments = [attachments]
28  request = get_current_request()
29  mailer = get_mailer(request)
30  html = render(template, variables, request)
31  message = Message(
32  subject=subject, bcc=recipients, html=html, extra_headers=extra_headers,
33  attachments=attachments, sender=sender)
34  mailer.send(message)
35