constnodemailer=require('nodemailer');// Setup Nodemailer transporterconsttransporter=nodemailer.createTransport({service:'gmail',auth:{user:'your-email@gmail.com',pass:'your-email-password'}});// ... rest of the code
constmailOptionsWithAttachment={// ... other optionsattachments:[{filename:'file.txt',path:'/path/to/file.txt'}]};
1.5 HTML Emails with Dynamic Content
Install email-templates and pug:
npm install email-templates pug --save
Update emailSender.js as per your template.
1.6 Now, create your email templates.
Let's focus on a common scenario: sending a welcome email for new user registration.
Create subject.pug:
= `Hi #{firstName} #{lastName}, welcome to My App!`
Create html.pug:
h1 Hello #{firstName} #{lastName}
p.
Welcome to My App! Now your test emails will be safe.
We just need to make sure your account is real.
a(href='https://example.com/confirmation') Confirm!
Now, let's modify our Node.js application to use these templates:
// ... (previous code)// Step 5: Create an email-templates instanceconstEmail=require('email-templates');constemail=newEmail({message:{from:'hi@example.com',},send:true,transport:{host:'smtp.mailtrap.io',port:2525,ssl:false,tls:true,auth:{user:'your-mailtrap-username',pass:'your-mailtrap-password',},},});// Step 6: Define user dataconstpeople=[{firstName:'Diana',lastName:'One',email:'diana@example.com'},{firstName:'Alex',lastName:'Another',email:'alex@example.com'},];// Step 7: Send emails for each userpeople.forEach((person)=>{email.send({template:'welcome',message:{to:person.email,},locals:person,}).then(console.log).catch(console.error);});
Replace placeholder values in the code with your actual credentials.
Method 2: Sending Emails with Gmail SMTP
2.1 Gmail SMTP Configuration
npm install nodemailer --save
Create gmailSender.js:
constnodemailer=require('nodemailer');consttransporter=nodemailer.createTransport({service:'gmail',auth:{user:'your-email@gmail.com',pass:'your-email-password'}});// ... rest of the code
// oAuth2 authenticationconstoauth2Client=newOAuth2('YOUR_CLIENT_ID','YOUR_CLIENT_SECRET','YOUR_REDIRECT_URL');// ... rest of the code
Method 3: Sending Emails using a Transactional Email API (e.g., Postmark)
Step 1: Sign Up for Postmark and Create API Token
Sign up for Postmark.
Create an API token in your Postmark account.
Step 2: Install the Postmark JavaScript Client
npm install--save postmark
Step 3: Create postmark.js
Create a file named postmark.js:
constpostmark=require('postmark');constclient=newpostmark.ServerClient('YOUR_POSTMARK_API_TOKEN');constmessage={From:'your-email@example.com',To:'recipient@example.com',Subject:'Subject of the Email',TextBody:'Text content of the email',HtmlBody:'<strong>HTML content of the email</strong>',};client.sendEmail(message).then((response)=>{console.log('Email sent\n',response);}).catch((error)=>{console.error(error);});
Step 4: Update postmark.js
Replace 'YOUR_POSTMARK_API_TOKEN' with the API token you obtained from your Postmark account.
Method 4: Using SuprSend - Third-Party Multichannel Notification Infrastructure with Node SDK
Step I: Log in to the SuprSend Account
Visit the SuprSend login page and enter your Email Address and Password. Try SuprSend for free.
Step II: Integrate with Email(s)
Install SuprSend using npm or yarn.
Step III: Create & Load Template
Create templates using the drag & drop editor.
Step IV: Create Smart Flow (Routing)
Define rules and logic for notification flows.
Step V: Trigger a Notification Event via API
Integrate the SuprSend API into your Node application.
npm install @suprsend/node-sdk
# to upgrade to latest SDK version
npm install @suprsend/node-sdk@latest
Following example shows a sample request for triggering a workflow.
It triggers a pre-created workflow purchase-made to a recipient with id: distinct_id,
email: user@example.com & androidpush(fcm-token): __android_push_fcm_token__
fromsuprsendimportWorkflowTriggerRequest# Prepare Workflow bodywf=WorkflowTriggerRequest(
body={
"workflow": "purchase-made""recipients": [
{
"distinct_id": "0f988f74-6982-41c5-8752-facb6911fb08",
# if $channels is present, communication will be tried on mentioned
It is a unified API to trigger workflow and doesn't require user creation before hand. If you are using our frontend SDK's to configure notifications and passing events and user properties from third-party data platforms like Segment, then event-based trigger would be a better choice.
After installing, Import the component in your code and use it as given below. Replace the variables with actual values.
importSuprSendInboxfrom'@suprsend/react-inbox'import'react-toastify/dist/ReactToastify.css'// needed for toast notifications, can be ignored if hideToast=true// add to your react component;<SuprSendInboxworkspaceKey='<workspace_key>'subscriberId='<subscriber_id>'distinctId='<distinct_id>'/>