Search code examples
javascriptnode.jsgoogle-apigoogle-api-nodejs-client

Node google api body of email


I'm using the node googleapi gmail client to send an email with the following code:

var email_lines = ["Content-Type: text/plain; charset=\"UTF-8\"\n",
        "MIME-Version: 1.0\n",
        "Content-Transfer-Encoding: 7bit\n",
        "to: ", "thing@gmail.com", "\n",
        "from: ", "thing2@gmail.io", "\n",
        "subject: ", "the subject", "\n\n",
        "this is the best message"
    ].join('');
    var email = base64.encode(email_lines.trim().replace(/\+/g, '-').replace(/\//g, '_') );

gmail.users.messages.send({
  userId: "thing@gmail.com",
  resource :{
    raw: email
  },
  media:{
    mimeType: "message/rfc822"
  }
},(err,data,body)=>{
  console.log(err);
});

The email gets sent but the body of the message shows up in a file that you have to download. How can I prevent the file attachment and have the text show up in the email itself?


Solution

  • Your line:

    var email = base64.encode(email_lines.trim().replace(/\+/g, '-').replace(/\//g, '_') );
    

    should actually be:

    var email = base64.encode(email_lines.trim()).replace(/\+/g, '-').replace(/\//g, '_');
    

    You need to encode to base64 your mail then do your replacements instead of doing them and then encoding to base64.