Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed sending mail through google api in nodejs

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

I'm using googleapis for Google API access in node.js .

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

I didn't define in my request that there is an attachment and I don't see any error in the email addresses.

Please help.

My code:

    var google = require('googleapis');
    var gmailClass = google.gmail('v1');

    var email_lines = [];

    email_lines.push("From: \"Some Name Here\" <[email protected]>");
    email_lines.push("To: [email protected]");
    email_lines.push('Content-type: text/html;charset=iso-8859-1');
    email_lines.push('MIME-Version: 1.0');
    email_lines.push("Subject: New future subject here");
    email_lines.push("");
    email_lines.push("And the body text goes here");
    email_lines.push("<b>And the bold text goes here</b>");

    var email =email_lines.join("\r\n").trim();

    var base64EncodedEmail = new Buffer(email).toString('base64');

    gmailClass.users.messages.send({
        auth: OAuth2Client,
        userId: "me",
        message: 
        {
             raw: base64EncodedEmail
        }           
      },
    function(err, results){});
like image 873
Hanoch Giner Avatar asked Aug 08 '14 15:08

Hanoch Giner


People also ask

Does Gmail API use SMTP?

Gmail API uses open authentication (Oauth2), which only lets you request the scope of access you need. SMTP provides full access to the account using client login and password SMTP authentication.

Can you send emails with node js?

Whether you're building the next Gmail, cutting-edge email marketing software, or just want to program notifications into your Node. js app, it's easy to send emails using readily-made tools and services. Node. js is one of the most popular (if not the most popular) server-side runtime environment for web applications.


1 Answers

Changes were made to version 1.0.3 of the google api. Try using the following syntax:

gmailClass.users.messages.send({
    auth: OAuth2Client,
    userId: "me",
    resource: 
    {
         raw: base64EncodedEmail
    }           
  }

Make sure that base64EncodedEmail is url safe. You can use the base64EncodedEmail.replace(/\+/g, '-').replace(/\//g, '_') code posted by mscdex. This syntax worked for v. 1.0.11

like image 76
user1445240 Avatar answered Oct 21 '22 04:10

user1445240