Files
postgres-web/pgweb/mailqueue/util.py
Magnus Hagander 097a732031 cc bugreports to the original submitter
This gives the submitter a chance to respond to their own message even
if it's not delivered through the list (for example, because they are
not subscribed, or because it's caught in moderation for other reasons).

Per discussion at the developer meeting.
2017-07-04 16:57:51 +01:00

47 lines
1.8 KiB
Python

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.Utils import make_msgid
from email import encoders
from models import QueuedMail
def send_simple_mail(sender, receiver, subject, msgtxt, attachments=None, usergenerated=False, cc=None):
# attachment format, each is a tuple of (name, mimetype,contents)
# content should be *binary* and not base64 encoded, since we need to
# use the base64 routines from the email library to get a properly
# formatted output message
msg = MIMEMultipart()
msg['Subject'] = subject
msg['To'] = receiver
msg['From'] = sender
if cc:
msg['Cc'] = cc
msg['Date'] = formatdate(localtime=True)
msg['Message-ID'] = make_msgid()
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(), usergenerated=usergenerated).save()
if cc:
# Write a second copy for the cc, wihch will be delivered
# directly to the recipient. (The sender doesn't parse the
# message content to extract cc fields).
QueuedMail(sender=sender, receiver=cc, fullmsg=msg.as_string(), usergenerated=usergenerated).save()
def send_mail(sender, receiver, fullmsg, usergenerated=False):
# Send an email, prepared as the full MIME encoded mail already
QueuedMail(sender=sender, receiver=receiver, fullmsg=fullmsg, usergenerated=False).save()