Giter Site home page Giter Site logo

email-automation's Introduction

Email Automation using SendGrid

Send Emails using Node, Express and SendGrid

Prerequisite

  • node and node package manager
  • Postman

How to set up in your localhost

  • Save your API Key in your local environment
export API_KEY="Your custom API KEY from Sendgrid"
  • Install all dependencies listed in package.json
npm install
  • To start the endpoint use
npm start

or

node index.js
  • In the console check Port number Example
    server started on PORT: 5000
  • Open Postman and make a get request on http://localhost:5000 If You get a callback as "Hello World", your server has started successfully on localhost

How to Test the API

Endpont to send mail is in the format https://localhost:5000/test

Without attachment

To send an email to a person copy-paste this JSON Tree. This is the simplest payload. Every field is mandatory. Replace emailMessage with your HTML Body and email with an email id you want to send an email.

Sample Input

{
   "userData": [
       {
           "email": "[email protected]",
           "name": "Ram",
           "status": "PASS",
           "cc":[],
           "bcc":[]
       }
   ],
   "emailMessage": "<html><head><title></title></head><body>Hello {{name}} ,<br /><br/>Results are out<br /><br/><br /><br/>You are designated as {{status}}<br /><br/></body></html>"
}

Sample Output

{
   "status": 200,
   "message": "Successfully added mails to queue",
   "data": {
       "success": [
           "[email protected]"
       ],
       "failure": [],
       "note": "Check your Dashboard or set up a Webhook to get live values of failures. Emails are put in a queue, they may take some time to arrive or decide to fail"
   }
}

Check Your MailBox

Check your mailbox to see the email you have sent. Emails are put in a queue by SendGrid, they may take some time to arrive or decide to fail.

More complex Input JSON samples

{
   "userData": [
       {
           "email": "[email protected]",
           "name": "Ram",
           "status": "PASS",
           "cc":["[email protected]"],
           "bcc":[]
       },
       {
           "email": "[email protected]",
           "name": "Ram",
           "status": "PASS",
           "cc":["[email protected]","[email protected]"],
           "bcc":["[email protected]","[email protected]"]
       }
   ],
   "emailMessage": "<html><head><title></title></head><body>Hello {{name}} ,<br /><br/>Results are out<br /><br/><br /><br/>You are designated as {{status}} This is the raw body<br /><br/></body></html>"
}

With attachment [PDF's]

Attachments can se sent in an API using form-data and binary data. In binary data method one attachment can be sent with no json body. Here form-data is used.

The easiest way to test the API with attachment

  • Open Postman
  • Select POST as a request
  • Add URL http://localhost:5000
  • Click on Body Tab
  • Click form-data
  • Add key as binaryFile In the same horizontal line click on dropdown select File and choose the file you want to send
  • Now fill all other options in the sample image
Key Value
binaryFile [CHOOSE FILE]]
emailMessage

Sample HTML Body

userData[0][email] [email protected]
userData[0][name] Ram
userData[0][status] PASS

with attachment image

More complex sample imput

Key Value
binaryFile [CHOOSE FILE]]
emailMessage

Sample HTML Body

userData[0][email] [email protected]
userData[0][name] Ram
userData[0][status] PASS
userData[0][cc] [email protected]
userData[1][email] [email protected]
userData[1][name] Shyam
userData[1][status] FAIL
userData[1][cc] [email protected]
userData[1][cc] [email protected]
userData[1][cc] [email protected]
userData[1][bcc] [email protected]
userData[1][bcc] [email protected]

How to work with TypeScript and Node

To compile your typescript file to javascript you must have typescript installed. To install TypeScript

sudo npm install -g typescript

Compile TypeScript > JavaScript

tsc index.ts

Now Run the index.js using node

node index.ts

email-automation's People

Contributors

anandshivam44 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

kunal-mahatha

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.