npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@frontboi/mailing

v1.0.13

Published

Utility to make sending mail easier, particularly using Google Gmail and OAuth2.

Downloads

32

Readme

Utility to make sending mail easier, particularly with Google Gmail and OAuth2.

Install

npm install @frontboi/mailing

Support

You can create a PR on this project and I will review it. If you prefer, you can contact me on Linkedin or by email ([email protected]).

Features

This package is meant to facilitate the integration of Google's gmail API and OAuth2 for mailing. As such, it provides functions to generate Google OAuth2 client and access/refresh token to keep so you can use it for mail sending.

  • generateOAuthUrl: returns an authentication URL to redirect your client to so that it authorizes OAuth connection.
  • validateAuthCode: when a user successfuly authorized OAuth, he gets redirect to GOOGLE_REDIRECT_URI with a code in query params. Provide this code to this function in order to exchange it for access and refresh tokens.
  • forgeAccessToken: provide a refresh token to this function, and it will yield you an access token.
  • getUserInfos: returns general informations about the connected user.

Setup

Google console

You will need to setup a Google Console project. Go to this url, and then create your application. You must include these three scopes: /auth/userinfo.email, /auth/userinfo.profile and /auth/gmail.send for this package to work.

Environment variables

To use frontboi's mailing utility, these environment variables must be accessible at runtime:

  • GOOGLE_OAUTH_CLIENT_ID
  • GOOGLE_OAUTH_SECRET
  • GOOGLE_REDIRECT_URI

These values are obtained via after you have created you Google Console project.

How to use

Here is an implementation example in an Express server:

import { generateOAuthUrl, validateAuthCode, sendMail, getUserInfos } from '@frontboi/mailing'

import express from 'express'

const app = express()

// 1. redirect the user to the Google OAuth2 page, in order to get a code that will be used in the next step
//    the code will be sent to the GOOGLE_REDIRECT_URI endpoint - you have configured this endpoint in the Google cloud console
app.get('/authenticate', (req, res) => {
  const authUrl = generateOAuthUrl()
  res.redirect(authUrl)
})

// 2. request this endpoint with the code you obtained earlier
app.post('/get-tokens', (req, res) => {
    const { tokens } = await validateAuthCode(req.body.code)
    const { access_token, refresh_token } = tokens
    // do what you want with your tokens (save the refresh token in database for example)
})

// 3. send a mail using the token you obtained
app.get('/send-mail', (req, res) => {
    const refreshToken // get it the way you like

    // get sender's Google profile informations
    const { email, given_name, family_name } = await getUserInfos(refreshToken)

    await sendMail({
      from: email,
      refreshToken,
      attachments: [],
      subject: 'Hello Henry',
      to: '[email protected]',
      body: `${given_name} ${family_name} managed to send a mail using Gmail !`,
    })
})

app.listen(port, () => console.log(`Server is running on http://localhost:${port}`))