Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 921
Michael Gundlach Avatar asked May 25 '11 15:05

Michael Gundlach


People also ask

Does Gmail still support SMTP?

For non-Gmail clients, Gmail supports the standard IMAP, POP, and SMTP protocols. The Gmail IMAP, POP, and SMTP servers have been extended to support authorization via the industry-standard OAuth 2.0 protocol.


1 Answers

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
like image 128
Michael Berkowski Avatar answered Oct 24 '22 00:10

Michael Berkowski