simple-gmail-sender
v1.0.0
Published
Simple library to send emails using gmail
Downloads
2
Readme
Simple Gmail Sender
This library allows you to send emails through your gmail account from node.
nodemailer is used behind the scenes as it's simply an abstraction over nodemailer for gmail.
Installation
Simply run the following command:
npm install simple-gmail-sender
This library includes Typescript typing.
Usage
the usage of the library is fairly straightforward:
import { GmailSender } from 'simple-gmail-sender'
const gmail = new GmailSender('[email protected]', 'your-password')
// you can call sendMail function without any params.
// an empty mail will be sent your own address.
gmail.sendMail()
// you can set the destination of the mail.
gmail.sendMail({ to: '[email protected]' })
// you might send a given mail to multiple destinations.
gmail.sendMail({ to: ['[email protected]', '[email protected]'] })
// all params are optionals (only a subset is displayed in this example).
gmail.sendMail({to: '[email protected]', text: 'Some Text', subject: 'Nice Subject', from: '[email protected]'})
in cases where you want to send mails without creating a new GmailSender instance
you might use the function sendThroughGmail
as such:
import { sendThroughGmail } from 'simple-gmail-sender'
// this functions takes the same params as the "GmailSender.sendMail" function,
// except that you have to supply the "user" and "password" params.
sendThroughGmail({user: '[email protected]', password: 'your-password', to: '[email protected]', text: 'Some Text'})
you will get back a promise from calling either sendThroughGmail
or GmailSender.sendMail
(which resolves to the same response):
import { sendThroughGmail, SentMessageInfo } from 'simple-gmail-sender'
sendThroughGmail({ user: '[email protected]', password: 'your-password' })
.then((result: SentMessageInfo) => {
console.log(result.response)
})
.catch((error: Error) => {
console.log(error)
})
Security
It is not advised to use your own password for sending emails.
since gmail will block by default any plain-text password
that you'll send to it.
you have 2 options to resolve this issue:
(Less Recommended) You might allow gmail to accept your plain-text password (called less secure apps): https://myaccount.google.com/lesssecureapps
NOTE: this setting is not available for accounts with 2-Step Verification enabled.(Recommended) You might create a single-use password for your node app (called app passwords) through:
https://myaccount.google.com/apppasswords
after generating a single use password, use it instead of your own real password.
NOTE: app passwords can only be used with accounts that have 2-Step Verification turned on.