How to send a mail with an attachment in gmail api

2019-07-15 09:11发布

问题:

I wish to send an email with an attachment through gmail.

I'm using google-api-nodejs-client for node.js.

My current message sending code is:

var gmailClass = google.gmail('v1');
gmailClass.users.messages.send({
            auth: OAuth2Client,
            userId: 'me',
            resource: {
              raw: base64EncodedEmail
            },
          }
        ,function(err, results)
        {
        if (!err)
        {
          //do something    
        }
        else
        {
          //do something
        }
});

Is there a way to add an attachment to the send function?

回答1:

Your attachment should be in the "raw" base64EncodedEmail you're already using. The format is simply a full email message (e.g. look at "Show Original" in Gmail or any other mail client to see what an email with an attachment looks like). I imagine there are some MIME email message libraries that will be helpful...