How to send email with gmail and nodejs

Hi all,

in my workflow I want send an email to a specific mail. But I no idea how I can do this.
I create this code

import { axios } from "@pipedream/platform"
export default defineComponent({
  props: {
    gmail: {
      type: "app",
      app: "gmail",
    }
  },
  async run({steps, $}) {
   console.log(this.gmail.$auth);
  
    return await axios($, {
      url: `https://www.googleapis.com/oauth2/v1/userinfo`,
      headers: {
        Authorization: `Bearer ${this.gmail.$auth.oauth_access_token}`,
      },
    })
  },
})

But how I can add recipient and send email?

Hi @pls_lite_development,

You should visit Pricing - Pipedream for the latest pricing, but I’ll do my best to answer.

To send an email using the Gmail API in your Pipedream workflow, you can use the Send Email action provided by Pipedream. Here is an example of how you can modify your existing code to send an email:

import { axios } from "@pipedream/platform";

export default defineComponent({
  props: {
    gmail: {
      type: "app",
      app: "gmail",
    },
    to: {
      type: "string",
      label: "Recipient Email",
    },
    subject: {
      type: "string",
      label: "Email Subject",
    },
    body: {
      type: "string",
      label: "Email Body",
    },
  },
  async run({ steps, $ }) {
    return await axios($, {
      method: "POST",
      url: `https://gmail.googleapis.com/gmail/v1/users/me/messages/send`,
      headers: {
        Authorization: `Bearer ${this.gmail.$auth.oauth_access_token}`,
        "Content-Type": "application/json",
      },
      data: {
        raw: Buffer.from(
          `To: ${this.to}\nSubject: ${this.subject}\n\n${this.body}`
        ).toString("base64"),
      },
    });
  },
});

Explanation

  1. Props: Added to, subject, and body props to accept the recipient email, subject, and body of the email.
  2. API Request: Modified the API request to use the Gmail API’s send endpoint.
  3. Email Format: Constructed the email in the required format and encoded it in base64.

Steps to Use the Component

  1. Search for the Gmail app in the Pipedream UI.
  2. Select the Send Email action.
  3. Connect your Gmail account.
  4. Fill in the recipient email, subject, and body.
  5. Test the action to ensure it works as expected.

This should enable you to send an email to a specific recipient using your Pipedream workflow. If you need further assistance, please visit Support - Pipedream.

I’m a bot powered by Pipedream and GPT-4. I’m still learning, so please double-check my answers and code! Learn how to build your own.

1 Like

Thx, And how can I force the email to be displayed in html format?

Hi @pls_lite_development, have you tried our built-in Gmail - Send Email action? You can select html in the Body Type prop.

Hi, thx, I prefer do this in nodejs.
In found the fix. Just add “Content-Type: text/html”