Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Gmail API编码-如何摆脱3D和&;放大器_Python_Base64_Gmail Api - Fatal编程技术网

Python Gmail API编码-如何摆脱3D和&;放大器

Python Gmail API编码-如何摆脱3D和&;放大器,python,base64,gmail-api,Python,Base64,Gmail Api,我试图通过GMAIL API提取GMAIL电子邮件的正文,很好地使用Python 我可以使用下面的命令提取消息。然而,电子邮件文本的编码似乎存在问题(原始电子邮件中包含html)-出于某种原因,每次出现引用3D之前 此外,在a href=“my_url”中,我会出现随机等号=,并且在链接的末尾有&字符,该字符不在电子邮件的原始HTML中 你知道怎么解决这个问题吗 我用于提取电子邮件的代码: from __future__ import print_function from googlea

我试图通过GMAIL API提取GMAIL电子邮件的正文,很好地使用Python

我可以使用下面的命令提取消息。然而,电子邮件文本的编码似乎存在问题(原始电子邮件中包含html)-出于某种原因,每次出现引用3D之前

此外,在a href=“my_url”中,我会出现随机等号=,并且在链接的末尾有&字符,该字符不在电子邮件的原始HTML中

你知道怎么解决这个问题吗

我用于提取电子邮件的代码:

from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

from apiclient import errors
import base64
msgs = service.users().messages().list(userId='me', q="no-reply@hello.com",maxResults=1).execute()
for msg in msgs['messages']:message = service.users().messages().get(userId='me', id=m_id, format='raw').execute()
“raw”:以base64url编码字符串的形式返回原始字段中包含正文内容的完整电子邮件数据;未使用有效载荷字段。” td style=3D“填充:20px;颜色:#45555f;字体家族:塔荷马,他= lvetica;字体大小:12px;线高:18px;"

JPk79hd=
JFQZEhc6%2Paiqkf8m85sfbilbnd6ig8%2Fawwe3vtr2jpzba4bhf%2FEnjMxq66fr228I7OS=很遗憾,我无法找到正确的方式来解码消息

最后,我使用了以下解决方法:

1) 将消息拆分为一个列表,每一行作为一个列表项

2) 计算其中一个字符串的列表位置和结束字符串的位置

3) 从#2中生成一个新列表,然后重新生成相同的列表,删除最后一个字符(等号)

4) 从新列表中生成一个字符串

5) 搜索我想要的URL

    x= mime_msg.splitlines() #convert to list
    a = ([i for i, s in enumerate(x) if 'My unique start string' in s])[0] #get list# of beginning
    b = ([i for i, s in enumerate(x) if 'my end id' in s])[0] #end
    y = x[a:b]   #generate list w info we want
    new_list=[]
    for item in y:new_list.append(item[:-1]) #get rid of last character, which bs base64 encoding is "="
    url = ("".join(new_list)) #convert to string
    url = url.replace("3D","").replace("&amp","") #cleaner for some reason - encoding gives us random 3Ds + &amps
    csv_url = re.search('Whatever message comes before the URL (.*)',url).group(1)
上述用途

import re 
from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

from apiclient import errors
import base64
import email

您应该检查
内容传输编码
标题,查看它是否指定了
引用的可打印
,因为它看起来像编码文本

Per:

引用的可打印编码旨在表示主要由与US-ASCII字符集中的可打印字符相对应的八位字节组成的数据。它对数据进行编码,使得生成的八位字节不太可能被邮件传输修改。如果编码的数据主要是US-ASCII文本,则数据在很大程度上仍然是人类可以识别的。完全是US-ASCII的主体也可以用可打印的引号进行编码,以确保消息通过字符转换和/或换行网关时数据的完整性


Python的模块可以用来解码这种编码的电子邮件。

我已经从asp.net中的Web服务向gmail发送了一封邮件 内容为真正的html格式
它显示为想要的,尽管=3D

Dim Bericht As MailMessage
Bericht = New MailMessage
我的样式文本的内容是

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-=1">
<meta content="text/html; charset=us-ascii">
<style>h1{color:blue;}
.EditText{
background:#ff0000;/*rood*/
height:100;
font-size:10px;
color:#0000ff;/*blauw*/
}
</head>

h1{颜色:蓝色;}
.编辑文本{
背景:#ff0000;/*rood*/
身高:100;
字体大小:10px;
颜色:#0000ff;/*blauw*/
}
我身体的内容是

<div class='EditText'>this is just some text</div>
这只是一些文本
最后我把它组合在一起

Bericht.Body = "<html>" & styleText & "<body>" & content& "</body></html>"
Bericht.Body=“&styleText&&&content&”
如果我查看收到的消息的来源,仍然有这个3D 它表明


h1{颜色:蓝色;}
.编辑文本{
背景:#ff0000;/*rood*/
身高:100;
字体大小:10px;
颜色:#0000ff;/*blauw*/
}
MailadresAfzender

结果显示了一个带红色背景的蓝色文本。很好

在将整个文档放在我面前之后-看起来python用等号标记每行的结尾,因为它似乎试图将每行保留为#####个字符。你认为是什么导致了这一点?如果我至少能在ea末尾获得等号的话ch string,我可以用find-replace_和_black来完成剩下的部分。谢谢你。像等号这样的书与base64的编码有关-
Bericht.Body = "<html>" & styleText & "<body>" & content& "</body></html>"
<html><head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
=3D1">
<meta content=3D"text/html; charset=3Dus-ascii">
<style>h1{color:blue;}
.EditText{
background:#ff0000;/*rood*/
height:100;
font-size:10px;
color:#0000ff;/*blauw*/
}
</style>
</head><body><div class=3D'EditText'>MailadresAfzender</div></body></html>