Skip to content

Example of sending mail from server running node.js through gmail

License

Notifications You must be signed in to change notification settings

alexhokl/node-example-gmail-send

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This runs on Node version 5.x.

To try this example, clone this repository and download client secret from Google API Console (from credentials tab, click the credential of application type Other). Rename the json file to client_secret.json and put it in the project directory. Run npm install to install the required node packages.

Run node index.js to start the server.

When this application first runs, an URL will be presented in console. Copy and paste the URL into a browser and follow the instructions for authorisation. Note that the account signed-in will be used to send the mails. At the end of the screen flow, you will be presented with an authorisation code. Copy and paste the code into the console window to continue.

After authorisation, the refresh and access tokens are stored in ~/.credentials/gmail-nodejs-quickstart.json.

To send a mail, make a POST request to http://localhost:3001/message with the following body.

{
    "to": "alex@gmail.com",
    "from": "alex@gmail.com",
    "subject": "testing using Google APIs",
    "message": "This is a <i>testing</i> message."
}

To send a mail with an attachment, put a copy of PDF file in the project directory and name the file as attachment.pdf. Then, make a POST request to http://localhost:3001/message with the following body.

{
    "to": "alex@gmail.com",
    "from": "alex@gmail.com",
    "subject": "testing using Google APIs",
    "message": "This is a <i>testing</i> message.",
    "attachments": [{
       "localFilename": "attachment.pdf",
       "filename": "form.pdf",
       "contentType": "application/pdf",
       "encoding": "utf8"
    }]
}

About

Example of sending mail from server running node.js through gmail

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published