Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

reply_to option #13

Merged
merged 3 commits into from

2 participants

@ThiefMaster

Adds an option to set the Reply-To header

@rduplain rduplain merged commit 50be773 into from
@rduplain
Collaborator

Thanks!

@rduplain rduplain referenced this pull request
Closed

Custom header support #11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 16, 2012
  1. @ThiefMaster

    Fix whitespace issues

    ThiefMaster authored
  2. @ThiefMaster

    Add reply_to option

    ThiefMaster authored
  3. @ThiefMaster
This page is out of date. Refresh to see the latest.
Showing with 53 additions and 26 deletions.
  1. +33 −26 flaskext/mail/message.py
  2. +20 −0 tests.py
View
59 flaskext/mail/message.py
@@ -17,11 +17,11 @@ class Attachment(object):
:param content_type: file mimetype
:param data: the raw file data
:param disposition: content-disposition (if any)
-
+
"""
def __init__(self, filename=None, content_type=None, data=None,
- disposition=None):
+ disposition=None):
self.filename = filename
self.content_type = content_type
@@ -30,7 +30,7 @@ def __init__(self, filename=None, content_type=None, data=None,
class Message(object):
-
+
"""
Encapsulates an email message.
@@ -42,16 +42,18 @@ class Message(object):
:param cc: CC list
:param bcc: BCC list
:param attachments: list of Attachment instances
+ :param reply_to: reply-to address
"""
- def __init__(self, subject,
- recipients=None,
- body=None,
- html=None,
+ def __init__(self, subject,
+ recipients=None,
+ body=None,
+ html=None,
sender=None,
cc=None,
bcc=None,
- attachments=None):
+ attachments=None,
+ reply_to=None):
if sender is None:
@@ -68,13 +70,14 @@ def __init__(self, subject,
self.html = html
self.cc = cc
- self.bcc = bcc
+ self.bcc = bcc
+ self.reply_to = reply_to
if recipients is None:
recipients = []
self.recipients = list(recipients)
-
+
if attachments is None:
attachments = []
@@ -89,7 +92,7 @@ def get_response(self):
Creates a Lamson MailResponse instance
"""
- response = MailResponse(Subject=self.subject,
+ response = MailResponse(Subject=self.subject,
To=self.recipients,
From=self.sender,
Body=self.body,
@@ -101,31 +104,35 @@ def get_response(self):
if self.cc:
response.base['Cc'] = self.cc
+ if self.reply_to:
+ response.base['Reply-To'] = self.reply_to
+
for attachment in self.attachments:
- response.attach(attachment.filename,
- attachment.content_type,
- attachment.data,
+ response.attach(attachment.filename,
+ attachment.content_type,
+ attachment.data,
attachment.disposition)
return response
-
+
def is_bad_headers(self):
"""
Checks for bad headers i.e. newlines in subject, sender or recipients.
"""
-
- for val in [self.subject, self.sender] + self.recipients:
+
+ reply_to = self.reply_to or ''
+ for val in [self.subject, self.sender, reply_to] + self.recipients:
for c in '\r\n':
if c in val:
return True
return False
-
+
def send(self, connection):
"""
Verifies and sends the message.
"""
-
+
assert self.recipients, "No recipients have been added"
assert self.body or self.html, "No body or HTML has been set"
assert self.sender, "No sender address has been set"
@@ -138,21 +145,21 @@ def send(self, connection):
def add_recipient(self, recipient):
"""
Adds another recipient to the message.
-
+
:param recipient: email address of recipient.
"""
-
+
self.recipients.append(recipient)
- def attach(self,
- filename=None,
- content_type=None,
+ def attach(self,
+ filename=None,
+ content_type=None,
data=None,
disposition=None):
-
+
"""
Adds an attachment to the message.
-
+
:param filename: filename of attachment
:param content_type: file mimetype
:param data: the raw file data
View
20 tests.py
@@ -72,6 +72,16 @@ def test_sender_as_tuple(self):
msg = Message(subject="testing",
sender=("tester", "tester@example.com"))
+ def test_reply_to(self):
+
+ msg = Message(subject="testing",
+ recipients=["to@example.com"],
+ sender="spammer <spammer@example.com>",
+ reply_to="somebody <somebody@example.com>",
+ body="testing")
+
+ response = msg.get_response()
+ self.assertIn("Reply-To: somebody <somebody@example.com>", str(response))
def test_send_without_sender(self):
@@ -180,6 +190,16 @@ def test_bad_header_sender(self):
self.assertRaises(BadHeaderError, self.mail.send, msg)
+ def test_bad_header_reply_to(self):
+
+ msg = Message(subject="testing",
+ sender="from@example.com",
+ reply_to="evil@example.com\n\r",
+ recipients=["to@example.com"],
+ body="testing")
+
+ self.assertRaises(BadHeaderError, self.mail.send, msg)
+
def test_bad_header_recipient(self):
msg = Message(subject="testing",
Something went wrong with that request. Please try again.