techxnix-mail
v1.0.1
Published
Techxnix Mail Library for NestJS applications - A powerful and easy-to-use mail module using SendGrid
Downloads
9
Maintainers
Readme
Techxnix Mail
A powerful and easy-to-use NestJS mail module for sending emails using SendGrid.
Features
- 📧 Send plain text and HTML emails
- 📝 Support for SendGrid templates
- 📎 File attachments support
- 🔄 Dynamic template data
- 🎨 Custom sender information
- 📝 Comprehensive logging
- 🌐 Global module configuration
- 🔒 Type-safe interfaces
Installation
npm install techxnix-mailQuick Start
- Import
TechxnixMailModulein yourapp.module.ts:
import { TechxnixMailModule } from 'techxnix-mail';
@Module({
imports: [
TechxnixMailModule.forRoot({
sendGridApiKey: process.env.SENDGRID_API_KEY,
defaultFromEmail: process.env.DEFAULT_FROM_EMAIL, // optional
defaultFromName: process.env.DEFAULT_FROM_NAME, // optional
}),
],
})
export class AppModule {}- Inject and use
MailServicein your services:
import { MailService } from 'techxnix-mail';
@Injectable()
export class YourService {
constructor(private readonly mailService: MailService) {}
// Send a simple email
async sendEmail() {
await this.mailService.send({
to: '[email protected]',
subject: 'Welcome!',
text: 'Welcome to our platform!',
html: '<h1>Welcome to our platform!</h1>',
});
}
// Send using a SendGrid template
async sendTemplateEmail() {
await this.mailService.sendTemplate(
'[email protected]',
'd-template-id',
{ name: 'John' },
{ subject: 'Welcome!' }
);
}
}Configuration
Environment Variables
SENDGRID_API_KEY=your_sendgrid_api_key
[email protected]
DEFAULT_FROM_NAME=Your App NameConfiguration Options
| Option | Type | Required | Description | |--------|------|----------|-------------| | sendGridApiKey | string | Yes | Your SendGrid API key | | defaultFromEmail | string | No | Default sender email address | | defaultFromName | string | No | Default sender name |
API Reference
MailService
send(options: IMailOptions): Promise
Send an email with the following options:
interface IMailOptions {
to: string | string[];
subject: string;
text?: string;
html?: string;
from?: {
email?: string;
name?: string;
};
templateId?: string;
dynamicTemplateData?: Record<string, any>;
attachments?: Array<{
content: string;
filename: string;
type?: string;
disposition?: string;
contentId?: string;
}>;
}sendTemplate(to: string | string[], templateId: string, dynamicTemplateData: Record<string, any>, options?: Partial): Promise
Send an email using a SendGrid template.
Logging
The module includes comprehensive logging:
- Initialization logs
- Pre-send attempt logs
- Success/failure status
- Detailed error information
- Template usage logs
Example log output:
[MailService] SendGrid API Key configured
[MailService] Attempting to send email to: [email protected]
[MailService] Email sent successfully to [email protected]. Status: 202Error Handling
The module provides detailed error information in logs:
- SendGrid API errors
- Configuration issues
- Template rendering errors
- Network problems
Contributing
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
License
MIT
Support
For support, email [email protected] or create an issue in our GitHub repository.
