最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

email - Failed sending mail through google api with javascript - Stack Overflow

programmeradmin2浏览0评论

I'm trying to send an email through Google API with JavaScript.

My issue is that when I try to send a simple mail with no attachments, I get the following error:

'raw' RFC822 payload message string or uploading message via /upload/* URL required`

My code

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);
   });
  });        

}

I'm trying to send an email through Google API with JavaScript.

My issue is that when I try to send a simple mail with no attachments, I get the following error:

'raw' RFC822 payload message string or uploading message via /upload/* URL required`

My code

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);
   });
  });        

}
Share Improve this question edited Jun 3, 2015 at 13:02 abraham 47.8k10 gold badges106 silver badges158 bronze badges asked Jun 2, 2015 at 8:28 Jarno ZijnstraJarno Zijnstra 3212 silver badges9 bronze badges 2
  • Related: stackoverflow.com/questions/34546142/… – Sergio Commented Jan 10, 2017 at 16:35
  • Can you please explain why replace(/\+/g, '-').replace(/\//g, '_') ••• Replacing + with - and / with _? Also to simplify use ' (single quote) to avoid escaping ". – Mars Robertson Commented Nov 26, 2017 at 13:29
Add a comment  | 

1 Answer 1

Reset to default 21

After days i had found the answer by my own. The problem was that the 'message' in the body only can be used when you send an attachment in the email.

If you have no attachment the query looks like I wrote down here

var mail= base64EncodedEmail;
console.log(mail);
var request = gapi.client.gmail.users.messages.send({
  'userId': "me",
  'resource': {
      'raw': mail
    }
});
request.execute(function(response){
 console.log(response);
});
发布评论

评论列表(0)

  1. 暂无评论