Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Integration/NodeMailer Email Integration/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your_email@gmail.com
SMTP_PASS=your_app_password
35 changes: 35 additions & 0 deletions Integration/NodeMailer Email Integration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 📧 NodeMailer Email Integration

This snippet demonstrates how to send emails using **NodeMailer** in a Node.js environment.
It is useful for integrating ServiceNow or custom applications with external email providers via SMTP.

## ✨ Features
- Uses environment variables for secure configuration.
- Sends both **plain text** and **HTML** emails.
- Provides error handling and logging.
- Ready for integration in ServiceNow-related workflows or external utilities.

## 🛠️ Setup Instructions
1. Install dependencies:
```bash
npm install nodemailer


2. 🔧 Configure Environment Variables

```bash
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your_email@gmail.com
SMTP_PASS=your_app_password

3. 📧 Sending Emails with NodeMailer

You can use the provided snippet to quickly send emails:

```javascript
// Import the sendMail function
const { sendMail } = require("./sendMail");

// Send an email
sendMail("receiver@example.com", "<h1>Hello from NodeMailer</h1>");
49 changes: 49 additions & 0 deletions Integration/NodeMailer Email Integration/sendMail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* NodeMailer Email Sending Snippet
*
* This snippet demonstrates how to send emails using NodeMailer.
* It uses environment variables for secure SMTP configuration.
*
* Usage:
* await sendMail("test@example.com", "<h1>Hello from NodeMailer</h1>");
*
* @format
*/

const nodemailer = require("nodemailer");

// Async function to send an email
const sendMail = async (emailId, htmlContent) => {
try {
// Create reusable transporter object using SMTP
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST, // e.g. "smtp.gmail.com"
port: process.env.SMTP_PORT || 587,
secure: false, // true for 465, false for other ports
auth: {
user: process.env.SMTP_USER, // your email address
pass: process.env.SMTP_PASS, // your email password or app password
},
});

// Define the email options
const mailOptions = {
from: `"ServiceNow Integration" <${process.env.SMTP_USER}>`, // sender address
to: emailId, // list of receivers
subject: "ServiceNow Notification", // subject line
text: "This is a plain text body", // plain text body
html: htmlContent, // html body
};

// Send mail with defined options
const info = await transporter.sendMail(mailOptions);

console.log("✅ Email sent successfully:", info.messageId);
return info;
} catch (error) {
console.error("❌ Error sending email:", error.message);
throw error;
}
};

module.exports = { sendMail };
Loading