smart-mailer
Version:
Simple Node.js email sender for Gmail, Outlook, or custom SMTP.
93 lines (67 loc) β’ 2.79 kB
Markdown
# smart-mailer



A universal email sending utility for Node.js that auto-detects SMTP settings based on the email domain. Supports Gmail, Outlook, Office365, Zoho, Yahoo, and custom domains via MX record lookup.
> Just provide the email and password in your `.env` file β everything else is mapped automatically!
> β
Built with [nodemailer](https://nodemailer.com)
> π No SMTP credentials hardcoded β everything from `.env`
## π Features
- π§ **Auto-detect SMTP settings** based on senderβs domain (e.g., Gmail, Yahoo, Outlook, Zoho).
- β‘ **No manual setup** required for host, port, or security.
- π Uses `.env` for secure credentials.
- β
Works with most public mail services and custom domains (if MX records are configured).
- π¦ Lightweight and easy to integrate into any backend Node.js app.
## π Installation
```bash
npm install smart-mailer
```
## π¦ Usage
```bash
// index.js
const sendEmail = require('smart-mailer');
require('dotenv').config();
(async () => {
try {
await sendEmail({
to: 'recipient@example.com',
subject: 'Test Email from smart-mailer π©',
text: 'This is a plain text test email.',
html: '<h1>Hello from smart-mailer!</h1><p>This is a test email sent using <strong>nodemailer</strong>.</p>',
});
} catch (err) {
console.error("β Failed to send test email:", err);
}
})();
```
## Setup
Create a .env file in your project root:
```bash
SMTP_EMAIL=your_email@gmail.com
SMTP_PASSWORD=your_email_password_or_app_password
```
π Note:
- Gmail requires App Passwords if 2-Step Verification is enabled.
π [How to generate App Passwords for Gmail](https://support.google.com/accounts/answer/185833)
- Make sure less secure apps are allowed or app password is used if applicable.
- Always keep your .env file private and never commit it to version control.
## π§ How It Works
The package uses your email domain to auto-map the correct SMTP settings by:
Checking a predefined list for common providers (Gmail, Outlook, etc.)
Falling back to MX record lookup for custom domains
## π€ Sending an Email
```bash
const sendEmail = require('smart-mailer');
sendEmail({
to: 'recipient@example.com',
subject: 'Welcome to Quick Mailer',
text: 'Hello from the smart-mailer package!',
})
.then(() => console.log('β
Email sent successfully!'))
.catch((err) => console.error('β Failed to send email:', err));
```
## π§βπ» Author
[Utkarsh Gupta](https://github.com/utkarsh-rgb)