Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Modify emailer to allow sending emails with attachments
  • Loading branch information
MelissaCole committed May 1, 2015
1 parent 1cc3840 commit 6fd9cb4
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions r2/r2/lib/emailer.py
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 6fd9cb4

Please sign in to comment.