summaryrefslogtreecommitdiff
path: root/pgcommitfest/mailqueue/util.py
blob: 38e114538ad115bfdafb77bfb59c0c6e7828a4a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from django.template import Context
from django.template.loader import get_template

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.nonmultipart import MIMENonMultipart
from email.Utils import formatdate
from email import encoders

from models import QueuedMail

def send_simple_mail(sender, receiver, subject, msgtxt, sending_username, attachments=None):
	# attachment format, each is a tuple of (name, mimetype,contents)
	# content should already be base64 encoded
	msg = MIMEMultipart()
	msg['Subject'] = subject
	msg['To'] = receiver
	msg['From'] = sender
	msg['Date'] = formatdate(localtime=True)
	msg['User-Agent'] = 'pgcommitfest'
	msg['X-cfsender'] = sending_username

	msg.attach(MIMEText(msgtxt, _charset='utf-8'))

	if attachments:
		for filename, contenttype, content in attachments:
			main,sub = contenttype.split('/')
			part = MIMENonMultipart(main,sub)
			part.set_payload(content)
			part.add_header('Content-Disposition', 'attachment; filename="%s"' % filename)
			encoders.encode_base64(part)
			msg.attach(part)


	# Just write it to the queue, so it will be transactionally rolled back
	QueuedMail(sender=sender, receiver=receiver, fullmsg=msg.as_string()).save()

def send_mail(sender, receiver, fullmsg):
	# Send an email, prepared as the full MIME encoded mail already
	QueuedMail(sender=sender, receiver=receiver, fullmsg=fullmsg).save()

def send_template_mail(sender, receiver, subject, templatename, templateattr={}, usergenerated=False):
	send_simple_mail(sender, receiver, subject,
					 get_template(templatename).render(Context(templateattr)),
					 '__internal')