Send Email In Node.JS In Hindi | NodeMailer Example

Blogs ❯❯ NodeJS

Image could not load

Node.JS Send Email

लगभग हर तरह के application में Email को अपने users के साथ communication करने के लिए use किया जाता है , फिर चाहे account को verify करना हो password reset करना हो या फिर marketing emails send करना हो।

Application में emails send करना बहुत ही basic but important हो गया है , और इस topic में हम Node.js की help से email send करना सीखेंगे।

Node.js में email send करने के लिए Nodemailer package का use किया जाता है।

What is NodeMailer In Hindi ?

Nodemailer एक famous Nodejs module है जिसका use email send करने के लिए किया जाता है। Nodemailer को SMTP (Simple Mail Transfer Protocol) के माध्यम से email send करता है।

NodeMailer module की help से आप आसानी से plain text, HTML, or attachments (image, document, etc.) को email में send कर सकते हैं।

Features of NodeMailer

NodeMailer के कुछ features इस प्रकार है-

  • अन्य modules पर Zero dependencie

  • Secure emails delivery

  • HTML content or embedded attachments

  • Unicode support

  • SMTP के अलावा Various transport options

Node.JS Install NodeMailer

सबसे पहले आप Nodemailer library को अपने Node.js project में install करें , npm का use करके इसे आसानी से install किया जा सकता है।

npm install nodemailer

Node.JS Send Email Example

code में आपको अपनी Gmail email address और password set करना पड़ेगा।

// Nodemailer library ko import karein. const nodemailer = require('nodemailer'); // Transporter (SMTP client) banayein. const transporter = nodemailer.createTransport({ service: 'Gmail', // Email service provider ka naam auth: { user: 'your_email@gmail.com', // Apna email address pass: 'your_password' // Apna email password } }); // Email ki options taiyar karein. const mailOptions = { from: 'your_email@gmail.com', // Sender ka email address. to: 'recipient_email@example.com', // Recipient ka email address. subject: 'Hello from Node.js', // Email ka subject. text: 'This is a test email sent from Node.js.' // Email ka text. }; // Email bhejein transporter.sendMail(mailOptions, function(error, info) { if (error) { console.log('Error: ' + error); } else { console.log('Email sent: ' + info.response); } });

तो कुछ इस तरह से Nodejs में plain text email send किया जाता है।

ध्यान रहे Google Gmail से email send करने के लिए अब two factor authentication mandatory कर दिया है तो , अब password की जगह पर two factor authentication on करने पर जो code मिलता है उसका use करें।

अगर आपको gmail के वजाय किसी और email service provider का SMTP server use करना है तो आप "service" और "auth" objects को us service provider according configure कर सकते हैं।

Node.JS send HTML email

HTML emails ,send करने के लिए mailOptions में आपको HTML , html key के साथ content को associate करना पड़ेगा। जैसे कि नीचे example में दिखाया गया है।

// Email options with both HTML and plain text content. const mailOptions = { from: 'your_email@gmail.com', to: 'recipient_email@example.com', subject: 'HTML and Plain Text Email Example', // HTML content. html: '<h1>Hello from Node.js</h1><p>This is an HTML email sent from Node.js.</p>' };

हालाँकि आपको HTML के साथ - साथ plain text version भी provide करना चाहिए ताकि जो लोग HTML emails नहीं देख सकते हैं उन्हें text version मिल सके।

For example -

// Email options with both HTML and plain text content const mailOptions = { from: 'your_email@gmail.com', to: 'recipient_email@example.com', subject: 'HTML and Plain Text Email Example', // HTML content. html: '<h1>Hello from Node.js</h1><p>This is an HTML email sent from Node.js.</p>', // Plain text content. text: 'Hello from Node.js\nThis is a plain text version of the email.' };

I Hope , आपको अब समझ आ गया होगा कि Nodejs में email को कैसे send किया जाता है।

Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook