Gathering detailed insights and metrics for versa-mail
Gathering detailed insights and metrics for versa-mail
Gathering detailed insights and metrics for versa-mail
Gathering detailed insights and metrics for versa-mail
npm install versa-mail
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
This emailing package is a Node.js library that simplifies sending emails using either Nodemailer or Azure Communication Services. It provides easy-to-use functions to send single or bulk emails with customizable templates and attachments.
To use the Emailer package in your Node.js project, you can install it via npm:
1npm install versa-mail
The Emailer package allows you to create a mailer instance based on the mailer type you want to use. The mailer factory class provides a convenient method for this purpose.
1const MailerFactory = require("versa-mail"); 2 3// Configuration for Nodemailer mailer 4const nodemailerConfig = { 5 emailService: "your_email_service_provider", 6 emailUsername: "your_email_username", 7 emailPassword: "your_email_password", 8 senderAddress: "your_sender_email@example.com", 9}; 10 11// Configuration for Azure mailer 12const azureConfig = { 13 connectionString: "your_azure_connection_string", 14 senderAddress: "your_sender_email@example.com", 15}; 16 17// Create the mailer instance based on the selected type 18const mailerType = "nodemailer"; // Use "nodemailer" for Nodemailer or "azure" for Azure Email Service 19const mailerFactory = new MailerFactory(); 20const mailer = mailerFactory.createMailer(mailerType, nodemailerConfig); 21// OR 22const mailer = mailerFactory.createMailer("azure", azureConfig);
You can send a single email using the sendEmail
method. The method allows you to use custom or default email templates, replace template variables, and include attachments.
1const emailOptions = { 2 withDefaultTemplate: true, // Set to false if using a custom template 3 templateName: "template_name", // Required if withDefaultTemplate is true 4 template: "<html><body>Hello, #{username}!</body></html>", // Required if withDefaultTemplate is false 5 constants: { 6 username: "John Doe", // Replace #{username} in the template with this value 7 }, 8 subject: "Test Email", 9 email: "recipient@example.com", 10 replyTo: "reply@example.com", // Optional 11 cc: ["cc_recipient1@example.com", "cc_recipient2@example.com"], // Optional 12 bcc: ["bcc_recipient1@example.com", "bcc_recipient2@example.com"], // Optional 13 attachments: [ 14 { 15 name: "attachment.txt", 16 url: "path.to.attachment.", 17 }, 18 ], // Optional 19}; 20 21mailer 22 .sendEmail(emailOptions) 23 .then((response) => { 24 // Email sent successfully 25 console.log("Email sent successfully:", response); 26 }) 27 .catch((error) => { 28 // Error sending email 29 console.error("Error sending email:", error); 30 }); 31 32OR; 33 34try { 35 const sendMail = await mailer.sendEmail(emailOptions); 36 console.log("Email sent successfully:", sendMail); 37} catch (err) { 38 console.error("Error sending email:", err); 39}
If you need to send bulk emails, you can use the sendBulk
method. This method allows you to customize each email for individual recipients and include common attachments for all emails.
1const bulkEmailOptions = { 2 withDefaultTemplate: true, // Set to false if using a custom template 3 templateName: "template_name", // Required if withDefaultTemplate is true 4 template: "<html><body>Hello, #{username}!</body></html>", // Required if withDefaultTemplate is false 5 constants: { 6 // Common constants for all emails 7 company: "Your Company", 8 website: "https://example.com", 9 }, 10 subject: "Test Email", 11 users: [ 12 { 13 //An object of user specific constants to replace placeholders within the html. User specific Attachments can be handled seperately. 14 email: "recipient1@example.com", 15 username: "Recipient 1", 16 attachments: [ 17 { 18 name: "attachment1.txt", 19 url: "path/to/attachment1.txt", 20 }, 21 ], // Optional 22 }, 23 { 24 email: "recipient2@example.com", 25 username: "Recipient 2", 26 // Add more user-specific options here 27 }, 28 ], 29 cc: ["cc_recipient@example.com"], // Optional 30 bcc: ["bcc_recipient@example.com"], // Optional 31 attachments: [ 32 { 33 name: "common_attachment.txt", 34 url: "path/to/common_attachment.txt", //should a valid CDN link 35 }, 36 ], // Optional 37}; 38 39mailer 40 .sendBulk(bulkEmailOptions) 41 .then((response) => { 42 // Bulk emails sent successfully 43 console.log("Bulk emails sent:", response); 44 }) 45 .catch((error) => { 46 // Error sending bulk emails 47 console.error("Error sending bulk emails:", error); 48 });
If you are using the AzureMailer
, you have the option to customize email templates with CSS configurations. The following CSS configurations are available:
To set CSS configurations, provide an object with these properties when creating the AzureMailer
instance.
1const cssConfigurations = { 2 USER_DEFINED_BODY_CSS: "body { background-color: #f0f0f0; }", 3 USER_DEFINED_CONTAINER_CSS: ".container { padding: 20px; }", 4 USER_DEFINED_H1_CSS: "h1 { color: #0080ff; }", 5}; 6 7const mailer = new AzureMailer( 8 connectionString, 9 senderAddress, 10 cssConfigurations 11);
email
and username
fields in the email options are mandatory and must be valid strings.Feel free to reach out to us if you encounter any issues or have questions about using the Emailer package! Happy emailing!
No vulnerabilities found.
No security vulnerabilities found.