Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support extra_headers argument to Message to include custom email hea…

…ders

Anything found in extra_headers is passed as-is to the underlying lamson message.
It will also override anything previously set by flask-mail.
  • Loading branch information...
commit 035f4d3fc4d46d7603f67051e1b378bfc0646a3e 1 parent 7c58019
@rslinckx authored
Showing with 15 additions and 1 deletion.
  1. +7 −1 flask_mail.py
  2. +8 −0 tests.py
View
8 flask_mail.py
@@ -170,7 +170,8 @@ def __init__(self, subject,
attachments=None,
reply_to=None,
date=None,
- charset=None):
+ charset=None,
+ extra_headers=None):
if sender is None:
sender = current_app.config.get("DEFAULT_MAIL_SENDER")
@@ -186,6 +187,7 @@ def __init__(self, subject,
self.date = date
self.msgId = make_msgid()
self.charset = charset
+ self.extra_headers = extra_headers
self.cc = cc
self.bcc = bcc
@@ -248,6 +250,10 @@ def as_string(self):
if self.reply_to:
msg['Reply-To'] = self.reply_to
+ if self.extra_headers:
+ for k, v in self.extra_headers.iteritems():
+ msg[k] = v
+
for attachment in self.attachments:
f = MIMEBase(*attachment.content_type.split('/'))
f.set_payload(attachment.data)
View
8 tests.py
@@ -276,6 +276,14 @@ def test_unicode_sender(self):
self.assertIn('From: =?utf-8?b?w4TDnMOWIOKGkiDinJMgPGZyb21AZXhhbXBsZS5jb20+Pg==?=', msg.as_string())
+ def test_extra_headers(self):
+ msg = Message(subject="subject",
+ recipients=["to@example.com"],
+ body="hello",
+ extra_headers={'X-Extra-Header': 'Yes'})
+
+ self.assertIn('X-Extra-Header: Yes', msg.as_string())
+
def test_message_charset(self):
msg = Message(subject="subject",
recipients=["foo@bar.com"],
Please sign in to comment.
Something went wrong with that request. Please try again.