簡體   English   中英

MIME標頭無法通過Gmail API實現

[英]MIME Headers Not Making it Through Gmail API

我正在嘗試通過Gmail API自動創建草稿,我希望這些草稿能夠回復現有的電子郵件。 為此,我認為我需要設置“threadId”標題(特定於Gmail),“引用”標題和“In-Reply-To”標題。 此外,要讓Gmail將郵件視為回復,“主題”標題​​必須與原始電子郵件匹配。

我將所有這些標頭硬編碼到MIMEText對象中,然后將消息作為字符串進行base-64編碼(urlsafe)並讓Gmail API提供它。 但是,“threadId”,“In-Reply-To”和“References”標題似乎沒有在發送的電子郵件中顯示,因為單擊“顯示原始”時顯示的MIME中不存在它們在Gmail用戶界面中。

new = MIMEText("reply body text")
new["In-Reply-To"] = "[Message-ID of email to reply to]" #looks like <..@mail.gmail.com>
new["References"] = "[Message-ID of email to reply to]" #looks like <..@mail.gmail.com>
new["threadId"] = "[threadId of message to reply to]" #looks like 14ec476abbce3421
new["Subject"] = "Testsend2"
new["To"] = "[Email to send to]"
new["From"] = "[Email to send from]"

messageToDraft = {'raw': base64.urlsafe_b64encode(new.as_string())}
message = {'message': messageToDraft}
draft = service.users().drafts().create(userId="me", body=message).execute()

實際上,它比這簡單得多! 如果您只在標題中提供正確的主題,並在正文中提供正確的threadId,Google將為您計算所有參考。

new = MIMEText("This is the placeholder draft message text.")
new["Subject"] = "Example Mail"
new["To"] = "emtholin@gmail.com"
new["From"] = "emtholin@gmail.com"

raw = base64.urlsafe_b64encode(new.as_string())
message = {'message': {'raw': raw, 'threadId': "14ec598be7f25362"}}
draft = service.users().drafts().create(userId="me", body=message).execute()

這導致草稿,准備好在正確的線程中發送:

在此輸入圖像描述

然后,我發送郵件。 如您所見,參考資料是為您計算的:

MIME-Version: 1.0
Received: by 10.28.130.132 with HTTP; Sat, 25 Jul 2015 07:54:12 -0700 (PDT)
In-Reply-To: <CADsZLRz5jWF5h=6Cs1F45QQOiFuqNGmMeb6St5e-tOj3stCNiA@mail.gmail.com>
References: <CADsZLRwmDZ_L5_zWqE8qOgoKuvRiRTWUopqssn4+XYGM_SKrfg@mail.gmail.com>
    <CADsZLRz5jWF5h=6Cs1F45QQOiFuqNGmMeb6St5e-tOj3stCNiA@mail.gmail.com>
Date: Sat, 25 Jul 2015 16:54:12 +0200
Delivered-To: emtholin@gmail.com
Message-ID: <CADsZLRxuyFhuGNPwjRrfFVQ0_2MxO=_jstjmsBGmAiwMEvfWSg@mail.gmail.com>
Subject: Example Mail
From: Emil Tholin <emtholin@gmail.com>
To: Emil Tholin <emtholin@gmail.com>
Content-Type: text/plain; charset=UTF-8

This is the placeholder draft message text.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM