我正在尝试使用JavaScript通过Google API发送电子邮件.
我的问题是,当我尝试发送没有附件的简单邮件时,我收到以下错误:
'raw'RFC822有效负载消息字符串或上传消息via/upload/*URL required`
我的代码
function sendMessage() {
gapi.client.load('gmail', 'v1', function() {
// Web-safe base64
var to = 'someone@someone.nl',
subject = 'Hello World',
content = 'send a Gmail.'
var base64EncodedEmail = btoa(
"Content-Type: text/plain; charset=\"UTF-8\"\n" +
"Content-length: 5000\n" +
"Content-Transfer-Encoding: message/rfc2822\n" +
"to: someone@someone.nl\n" +
"from: \"test\" <test@gmail.com>\n" +
"subject: Hello world\n\n" +
"The actual message text goes here"
).replace(/\+/g, '-').replace(/\//g, '_');
var mail= base64EncodedEmail;
console.log(mail);
var request = gapi.client.gmail.users.messages.send({
'userId': "me",
'message': {
'raw': mail
}
});
request.execute(function(response){ …Run Code Online (Sandbox Code Playgroud) 我已经成功克隆了“原始”格式的消息,方法是使用getwithformat='raw'并重新插入它,就像这样:
params = {'userId':'me", 'id': msg_id, 'format':'raw'}
mesg = service.users().messages().get(**params).execute()
body = {'raw': mesg['raw']}
service.users().messages().insert(userId='me', body=**body).execute()
Run Code Online (Sandbox Code Playgroud)
但是我很想使用 json 格式来做同样的事情,该格式get可以通过format='full'. 像这样的东西:
params = {'userId':'me", 'id': msg_id, 'format':'full'}
mesg = service.users().messages().get(**params).execute()
body = mesg
service.users().messages().insert(userId='me', body=**body).execute()
Run Code Online (Sandbox Code Playgroud)
mesg[1]的格式见下文。做上述给我的错误:
HttpError: <HttpError 400 when requesting
https://www.googleapis.com/gmail/v1/users/me/messages?alt=json
returned "
'raw' RFC822 payload message string or uploading message via /upload/*
URL required
">
Run Code Online (Sandbox Code Playgroud)
所以问题是:
insert通过“完整”json 格式发送消息?格式是“完整”而不是“原始”,所以我应该使用上传网址吗?如何?我们是否raw以某种方式继续在or 主体中使用 json 有效负载?
我们可以将其转换为原始格式然后像以前一样吗?
我应该尝试弄清楚如何使用upload这种格式吗? …