Why are replies to my SMTP-sent gmail messages not threaded?
Asked Answered
K

1

8

I'm sending lots of similar emails out via SMTP using the following Python snippet:

def send(from_, to, body):
  server = smtplib.SMTP('smtp.gmail.com:587')
  server.ehlo()
  server.starttls()
  server.ehlo()
  server.login('[email protected]', password)
  msg = '''\
From: %s
To: %s
Subject: %s

%s''' % (from_, to.encode('utf-8'), "Hello", body.encode('utf-8'))
  server.sendmail(from_, to, msg)
  server.quit()

These messages are the first messages in a conversation. Strangley, replies to these messages are not being threaded onto the original message's conversation.

A reply comes back as a separate message in my inbox, subject = "Re: Hello", with no tie to the original. (Very occasionally one will be threaded properly, which is even weirder.)

I've verified that these (un-threaded) replies have a References: field that refers to the sent mail's Message-ID field, which was autogenerated by GMail.

Any idea what I'm doing wrong?

Kislev answered 25/5, 2011 at 15:12 Comment(0)
H
6

Look at the References: header. It contains a chain of the previous Message-ID: headers in the thread, and is typically used for threading. It's usually a good idea to specify the Message-ID: yourself, and if you keep track of your previously used ones, you can use them in the References: header to enforce threading.

The Message-ID should be globally unique. They're often constructed as something like this, but it's not a requirement.

Message-ID: unixtimestamp.somerandomval@sending-hostname
Horace answered 25/5, 2011 at 15:14 Comment(4)
Thanks Michael -- I'll try adding a Message-ID header. If they're threaded properly, I'll accept your answer.Kislev
@Michael: thanks. There are no previous messages: the only message sent via SMTP is the one initiating the conversation. The rest of the conversation happens in gmail's web interface.Kislev
Unfortunately, I see that GMail is auto-adding a Message-ID to my message when it is sent over SMTP; whether I explicitly specify one or not doesn't change the outgoing message. So I don't think that's the culprit. Any other ideas?Kislev
Furthermore: the replies that are not being threaded correctly DO have a References: header referring to the message ID of the originally sent message. Might GMail be getting confused by the many similar emails with identical subjects, and giving up on trying to thread properly?Kislev

© 2022 - 2024 — McMap. All rights reserved.