Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Modify emailer to allow sending emails with attachments

  • Loading branch information
MelissaCole committed Apr 29, 2015
1 parent 1cc3840 commit 6fd9cb472e5bb76eb19783955030cc19522e1332
Showing with 18 additions and 2 deletions.
  1. +18 −2 r2/r2/lib/emailer.py
@@ -20,7 +20,10 @@
# Inc. All Rights Reserved.
###############################################################################

from email import encoders
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.errors import HeaderParseError
import datetime
import traceback, sys, smtplib
@@ -375,13 +378,26 @@ def suspicious_payment(user, link):
return _fraud_email(body, kind)


def send_html_email(to_addr, from_addr, subject, html, subtype="html"):
def send_html_email(to_addr, from_addr, subject, html,
subtype="html", attachments=None):
from r2.lib.filters import _force_utf8
msg = MIMEText(_force_utf8(html), subtype)
if not attachments:
attachments = []

msg = MIMEMultipart()
msg.attach(MIMEText(_force_utf8(html), subtype))
msg["Subject"] = subject
msg["From"] = from_addr
msg["To"] = to_addr

for attachment in attachments:
part = MIMEBase('application', "octet-stream")
part.set_payload(attachment['contents'])
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment',
filename=attachment['name'])
msg.attach(part)

session = smtplib.SMTP(g.smtp_server)
session.sendmail(from_addr, to_addr, msg.as_string())
session.quit()

0 comments on commit 6fd9cb4

Please sign in to comment.
You can’t perform that action at this time.