Changeset - ad239692ea95
[Not reviewed]
default
0 1 0
Mads Kiilerich - 5 years ago 2020-11-04 00:35:21
mads@kiilerich.com
Grafted from: 4da2f1faee7d
mail: fix duplicate "From" headers

Problem introduced in 9a0c41175e66: When iterating the headers dict and setting
"msg[key] = value", it wasn't replacing the header but performing add_header so
we sometimes ended up with two From headers.

It is also a general problem that while the headers dict only can contain each
key once, it can contain entries that only differ in casing and thus will fold
to the same message header, making it possible to end up adding duplicate
headers.

"msg.replace_header(key, value)" is not a simple solution to the problem: it
will raise KeyError if no such previous key exists.

Now, make the problem more clear by explicitly using add_header.

Avoid the duplication problem by deleting the key (no matter which casing)
before invoking add_header. Delete promises that "No exception is raised if the
named field isn’t present in the headers".
1 file changed with 2 insertions and 1 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/celerylib/tasks.py
Show inline comments
 
@@ -317,7 +317,8 @@ def send_email(recipients, subject, body
 
    msg['Date'] = email.utils.formatdate(time.time())
 

	
 
    for key, value in headers.items():
 
        msg[key] = value
 
        del msg[key]  # Delete key first to make sure add_header will replace header (if any), no matter the casing
 
        msg.add_header(key, value)
 

	
 
    msg.attach(email.mime.text.MIMEText(body, 'plain'))
 
    msg.attach(email.mime.text.MIMEText(html_body, 'html'))
0 comments (0 inline, 0 general)