kootenpv / yagmail

Send email in Python conveniently for gmail using yagmail
MIT License
2.66k stars 265 forks source link

How to make sure messages are threaded in gmail? Reference header? #173

Closed steel3d closed 4 years ago

steel3d commented 4 years ago

I'm sending several emails to myself with the same sender, recipients and subject, but gmail is not threading them. Each shows up as a separate email. According to gmail's docs, these should appear in the same thread. The only exception is if there is a "reference header". I'm not setting a header myself, not sure if yagmail does internally. Ref: https://gsuiteupdates.googleblog.com/2019/03/threading-changes-in-gmail-conversation-view.html

I just noticed that there is a message id once I click "show original" in gmail, and this is probably the problem:

Return-Path: xx@gmail.com Received: from [192.168.0.2] (cpe-172-114-95-4.socal.res.rr.com. [172.114.5.4]) by smtp.gmail.com with ESMTPSA id p1sm982433pjp.10.2020.07.31.13.5.10 for xx@gmail.com (version=TLS1_2 cipher=EDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 31 Jul 2020 13:15:10 -0700 (PDT) Message-ID: 5f247bce.1c69b81.1a324.8b32@mx.google.com Content-Type: multipart/mixed; boundary="===============221320975769819809==" MIME-Version: 1.0 Date: Fri, 31 Jul 2020 20:15:08 -0000 Subject: EOD Update From: "xx@gmail.com" xx@gmail.com To: xx@gmail.com

Is there a way to not set a message id, or set it to a specific id? I don't see any reference to message-id in the codebase.

Thanks!

kootenpv commented 4 years ago

Wow thanks for reporting this, it's quite a sneaky change that I realize is true now.

In your case, how would you like to decide it belongs to the same thread?

Maybe we could add an optional strategy for the following: we attach a message ID that is constructed as hash from the "from emailaddress" and the "subject"?

steel3d commented 4 years ago

It would be optimal if the api would allow me to set an explicit message id. On top of that, you could add heuristic strategies as well, like you suggest. Thanks for taking a look.

kootenpv commented 4 years ago

It's available as per 0.13.234. You can upgrade using:

pip install -U --no-cache yagmail

You will have 2 extra arguments on the send function.

If you pass the message_id yourself, the group_messages has no point.

I chose for calling it "group" and not "thread", as that could be confused with multi-threaded processing.

Please let me know if it works for you :)

steel3d commented 4 years ago

Sounds great! Will check in the next few days.

steel3d commented 4 years ago

There's a bug with message_id. Added a comment to commit. group_messages seems to work well.

kootenpv commented 4 years ago

Thanks for pointing out the bug, it should be fixed now after you upgrade :)

steel3d commented 4 years ago

Doesn't seem to work. I'm using message_id="2" an all messages and they never get aggregated even if everything is identical.

send(to=contact.email, subject=subject, contents=msg, message_id="2")

kootenpv commented 4 years ago

I believe that a message ID is "global in the world", so you should generate a globally unique string as far as I understood.

On Mon, 14 Sep 2020 at 06:29, steel3d notifications@github.com wrote:

Doesn't seem to work. I'm using message_id="2" an all messages and they never get aggregated even if everything is identical.

send(to=contact.email, subject=subject, contents=msg, message_id="2")

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kootenpv/yagmail/issues/173#issuecomment-691803269, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAU6O455DJFM2DAXJOJUIADSFWLZRANCNFSM4PQ445RA .

steel3d commented 4 years ago

There are some requirements on the string that I found out by trial and error. This works: message_id="<2@xyzdummy>" Needs to be enclosed in angle brackets and must contain an @ sign. Would be good to document this. But it's working well. Thanks!

steel3d commented 4 years ago

@kootenpv All newlines are removed from messages now. Is this a known issue?

kootenpv commented 4 years ago

@steel3d Thanks for mentioning it, it was indeed a bug! It should be solved per the latest version again.

steel3d commented 4 years ago

All good now! Thanks!