|
| 1 | +# Official EmailJS SDK for Node.js |
| 2 | + |
| 3 | +SDK for [EmailJS.com](https://www.emailjs.com) customers. |
| 4 | +\ |
| 5 | +Use you EmailJS account for sending emails. |
| 6 | + |
| 7 | +[](https://codecov.io/gh/emailjs-com/emailjs-nodejs) |
| 8 | +[](https://www.npmjs.com/package/@emailjs/nodejs) |
| 9 | + |
| 10 | +## Disclaimer |
| 11 | + |
| 12 | +This is a NodeJS-only version, otherwise use |
| 13 | +the [REST API](https://www.emailjs.com/docs/rest-api/send/). |
| 14 | + |
| 15 | +## Links |
| 16 | + |
| 17 | +[Official SDK Docs](https://www.emailjs.com/docs) |
| 18 | + |
| 19 | +## Intro |
| 20 | + |
| 21 | +EmailJS helps to send emails directly from your code. |
| 22 | +No large knowledge is required – just connect EmailJS to one of the supported |
| 23 | +email services, create an email template, and use our SDK |
| 24 | +to trigger an email. |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +Install EmailJS SDK using [npm](https://www.npmjs.com/): |
| 29 | + |
| 30 | +```bash |
| 31 | +$ npm install @emailjs/nodejs |
| 32 | +``` |
| 33 | + |
| 34 | +## Examples |
| 35 | + |
| 36 | +### ECMAScript modules |
| 37 | + |
| 38 | +**send email** |
| 39 | + |
| 40 | +```js |
| 41 | +import emailjs from '@emailjs/nodejs'; |
| 42 | + |
| 43 | +const templateParams = { |
| 44 | + name: 'James', |
| 45 | + notes: 'Check this out!', |
| 46 | +}; |
| 47 | + |
| 48 | +emailjs.send('<YOUR_SERVICE_ID>', '<YOUR_TEMPLATE_ID>', templateParams, '<YOUR_PUBLIC_KEY>').then( |
| 49 | + (response) => { |
| 50 | + console.log('SUCCESS!', response.status, response.text); |
| 51 | + }, |
| 52 | + (err) => { |
| 53 | + console.log('FAILED...', err); |
| 54 | + }, |
| 55 | +); |
| 56 | +``` |
| 57 | + |
| 58 | +**init (optional)** |
| 59 | + |
| 60 | +```js |
| 61 | +import emailjs from '@emailjs/nodejs'; |
| 62 | + |
| 63 | +// set Public Key as global settings |
| 64 | +emailjs.init('<YOUR_PUBLIC_KEY>'); |
| 65 | + |
| 66 | +emailjs.send('<YOUR_SERVICE_ID>', '<YOUR_TEMPLATE_ID>').then( |
| 67 | + (response) => { |
| 68 | + console.log('SUCCESS!', response.status, response.text); |
| 69 | + }, |
| 70 | + (err) => { |
| 71 | + console.log('FAILED...', err); |
| 72 | + }, |
| 73 | +); |
| 74 | +``` |
| 75 | + |
| 76 | +**await/async with EmailJS error handler** |
| 77 | + |
| 78 | +```js |
| 79 | +import { send, EmailJSResponseStatus } from '@emailjs/nodejs'; |
| 80 | + |
| 81 | +try { |
| 82 | + await emailjs.send('<YOUR_SERVICE_ID>', '<YOUR_TEMPLATE_ID>', {}, '<YOUR_PUBLIC_KEY>'); |
| 83 | + console.log('SUCCESS!'); |
| 84 | +} catch (err) { |
| 85 | + if (err instanceof EmailJSResponseStatus) { |
| 86 | + console.log('EMAILJS FAILED...', err); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + console.log('ERROR', err); |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +### CommonJS modules |
| 95 | + |
| 96 | +**send email** |
| 97 | + |
| 98 | +```js |
| 99 | +const emailjs = require('@emailjs/nodejs'); |
| 100 | + |
| 101 | +var templateParams = { |
| 102 | + name: 'James', |
| 103 | + notes: 'Check this out!', |
| 104 | +}; |
| 105 | + |
| 106 | +emailjs.send('<YOUR_SERVICE_ID>', '<YOUR_TEMPLATE_ID>', templateParams).then( |
| 107 | + function (response) { |
| 108 | + console.log('SUCCESS!', response.status, response.text); |
| 109 | + }, |
| 110 | + function (err) { |
| 111 | + console.log('FAILED...', err); |
| 112 | + }, |
| 113 | +); |
| 114 | +``` |
0 commit comments