+/*
+ * Get the concatenated Received: headers and search from the front
+ * (last Received: header added) and try to extract from them
+ * indications to which email address this message was delivered.
+ *
+ * The Received: header is among special ones in our get_header function
+ * and is always concatenated.
+ *
+ * Return the address that was found, if any, and NULL otherwise.
+ */
+static const char *
+guess_from_in_received_headers (notmuch_message_t *message)
+{
+ const char *received, *addr;
+ char *sanitized;
+
+ notmuch_database_t *notmuch = notmuch_message_get_database (message);
+
+ received = notmuch_message_get_header (message, "received");
+ if (! received)
+ return NULL;
+
+ sanitized = sanitize_string (NULL, received);
+ if (! sanitized)
+ return NULL;
+
+ addr = guess_from_in_received_for (notmuch, sanitized);
+ if (! addr)
+ addr = guess_from_in_received_by (notmuch, sanitized);
+
+ talloc_free (sanitized);
+
+ return addr;
+}
+
+/*
+ * Try to find user's email address in one of the extra To-like
+ * headers: Envelope-To, X-Original-To, and Delivered-To (searched in
+ * that order).
+ *
+ * The Delivered-To: header is among special ones in our get_header
+ * function and is always concatenated.
+ *
+ * Return the address that was found, if any, and NULL otherwise.
+ */
+static const char *
+get_from_in_to_headers (notmuch_message_t *message)
+{
+ size_t i;
+ const char *tohdr, *addr;
+ const char *to_headers[] = {
+ "Envelope-to",
+ "X-Original-To",
+ "Delivered-To",
+ };
+
+ notmuch_database_t *notmuch = notmuch_message_get_database (message);
+
+ for (i = 0; i < ARRAY_SIZE (to_headers); i++) {
+ tohdr = notmuch_message_get_header (message, to_headers[i]);
+
+ /* Note: tohdr potentially contains a list of email addresses. */
+ addr = user_address_in_string (tohdr, notmuch);
+ if (addr)
+ return addr;
+ }
+
+ return NULL;
+}
+