无法使用javascript通过google api发送邮件

问题描述 投票:10回答:2

我正在尝试通过带有JavaScript的Google API发送电子邮件。

我的问题是,当我尝试发送不带附件的简单邮件时,出现以下错误:

“原始” RFC822有效载荷消息字符串或通过/ upload / * URL上载消息,]]

我的代码

function sendMessage() {
gapi.client.load('gmail', 'v1', function() {
    // Web-safe base64 
    var to = '[email protected]',
        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: [email protected]\n" +
          "from: \"test\" <[email protected]>\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){
     console.log(response);
   });
  });        

}

我正在尝试通过带有JavaScript的Google API发送电子邮件。我的问题是,当我尝试发送不带附件的简单邮件时,出现以下错误:“原始” RFC822有效负载消息...

javascript email google-api google-api-client
2个回答
18
投票

几天后,我自己找到了答案。问题在于,仅当您在电子邮件中发送附件时,才能使用正文中的“消息”。


0
投票

[抱歉,对@paradox的评论不好。请在此处查看建议的Python版本:

© www.soinside.com 2019 - 2024. All rights reserved.