Znote (recipes)
  Get Znote  

Gmail plugin tutorial

How to use the Gmail Plugin

 

Gmail usage

👉 Insure you follow the Google API initialization

Send mail

//exec: node
const sender = "[YOUR-MAIL]@gmail.com";
const to = "[RECIPIENT]@gmail.com";
const subject = "🤘 Hello you 🤘";
const content = `
Ok this is a message just to say hello.
So... <b>Hello!</b>  🤘❤️😎 and Thanks
`
sendMailWithGmail(sender, to, subject, content);

Read mails

//exec:node
const google = await getGoogleClient();
const gmail = google.gmail({version: 'v1'});

const res = await gmail.users.messages.list({
  userId: 'me',
  labelIds: ['UNREAD', 'CATEGORY_PERSONAL', 'INBOX'],
  maxResults: 10
});

if (res.data.messages) {
  const messages = res.data.messages;
  for (const message of messages) {
    const msg = await gmail.users.messages.get({
      userId: 'me',
      id: message.id
    });
    const mail = readGmailMail(msg.data);
    printJSON(mail)
  }
}

Create Labels

//exec:node
const labelsToCreate = [
  "Newsletter", "Order", "Shipping", "Public Event", "Personal Event", "Personal", "Advertising", "Other"
]
const labels = await createAndGetLabels(labelsToCreate);
print(labels)

Create a Draft

//exec:node
const google = await getGoogleClient();
const gmail = google.gmail({version: 'v1'});

const content = `
Ok this is a message just to say hello.
So... <b>Hello!</b>  🤘❤️😎 and Thanks
`

const encodedMessage = await createGmailMessage("sender", "recipient", "Mon Sujet", content)

const res = await gmail.users.drafts.create({
  userId: 'me',
  resource: {
    message: {
        //threadId: "193b0ff2813cxc0d", // <-- to answer to a conversation
        raw: encodedMessage
    }
  }
});

More info about the API 👉 NodeJs Gmail API

Related recipes