Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from ThiefMaster/reply-to
Browse files Browse the repository at this point in the history
reply_to option
  • Loading branch information
rduplain committed Mar 16, 2012
2 parents ea10234 + 8080f09 commit 50be773
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 26 deletions.
59 changes: 33 additions & 26 deletions flaskext/mail/message.py
Expand Up @@ -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
Expand All @@ -30,7 +30,7 @@ def __init__(self, filename=None, content_type=None, data=None,


class Message(object):

"""
Encapsulates an email message.
Expand All @@ -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:
Expand All @@ -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 = []

Expand All @@ -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,
Expand All @@ -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"
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests.py
Expand Up @@ -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):

Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 50be773

Please sign in to comment.