TypeScript पर ईमेल भेजना: लाइब्रेरी और कोड उदाहरण

शुरुआती लोगों के लिए ऑनलाइन Python प्रशिक्षक

Python आसानी से सीखें बिना सिद्धांत के बोझ के। स्वचालित जांच के साथ व्यावहारिक कार्य हल करें और ब्राउज़र में सीधे कोड लिखें।

पाठ्यक्रम शुरू करें

TypeScript पर ईमेल भेजना: लाइब्रेरी और उपकरणों का अवलोकन

\n\n

इलेक्ट्रॉनिक पत्र भेजना वेब एप्लिकेशन के प्रमुख कार्यों में से एक है। TypeScript (और इसके रनटाइम Node.js) पर कई परिपक्व समाधान मौजूद हैं। इस लेख में, हम सबसे लोकप्रिय लाइब्रेरी, उनकी सेटिंग और पूर्ण टाइपिंग के साथ उपयोग के उदाहरणों पर चर्चा करेंगे।

\n\n

1. Nodemailer — सार्वभौमिक SMTP-क्लाइंट

\n\n

Nodemailer Node.js में ईमेल भेजने के लिए सबसे लोकप्रिय लाइब्रेरी है। यह SMTP, अंतर्निहित ट्रांसपोर्ट (Sendmail, SES) का समर्थन करता है और अंतर्निहित प्रकारों के कारण TypeScript के साथ पूरी तरह से संगत है।

\n\n

स्थापना

\n
npm install nodemailer\nnpm install --save-dev @types/nodemailer
\n\n

Gmail SMTP के माध्यम से भेजने का उदाहरण

\n
import nodemailer from 'nodemailer';\n\ninterface EmailOptions {\n  to: string;\n  subject: string;\n  text: string;\n  html?: string;\n}\n\nasync function sendEmail(options: EmailOptions): Promise<void> {\n  const transporter = nodemailer.createTransport({\n    service: 'gmail',\n    auth: {\n      user: process.env.EMAIL_USER,\n      pass: process.env.EMAIL_PASS,\n    },\n  });\n\n  await transporter.sendMail({\n    from: `"MyApp" <${process.env.EMAIL_USER}>`,\n    to: options.to,\n    subject: options.subject,\n    text: options.text,\n    html: options.html || options.text,\n  });\n}\n\n// उपयोग\nsendEmail({\n  to: 'user@example.com',\n  subject: 'TypeScript से नमस्ते!',\n  text: 'यह एक परीक्षण पत्र है।',\n}).catch(console.error);
\n\n

अटैचमेंट के साथ काम करना

\n
await transporter.sendMail({\n  to: 'user@example.com',\n  subject: 'अटैचमेंट में फ़ाइल',\n  text: 'संलग्न दस्तावेज़ देखें।',\n  attachments: [\n    {\n      filename: 'report.pdf',\n      path: './files/report.pdf',\n    },\n  ],\n});
\n\n

2. SendGrid — क्लाउड ईमेल सेवा

\n\n

SendGrid (अब Twilio का हिस्सा) लेन-देन और मार्केटिंग ईमेल भेजने के लिए एक शक्तिशाली API प्रदान करता है। TypeScript के लिए तैयार प्रकारों के साथ एक आधिकारिक पैकेज है।

\n\n

स्थापना

\n
npm install @sendgrid/mail
\n\n

टाइपिंग के साथ भेजने का उदाहरण

\n
import sgMail from '@sendgrid/mail';\n\nsgMail.setApiKey(process.env.SENDGRID_API_KEY!);\n\ninterface SendGridMessage {\n  to: string;\n  from: string;\n  subject: string;\n  text: string;\n  html?: string;\n}\n\nasync function sendViaSendGrid(msg: SendGridMessage): Promise<void> {\n  try {\n    await sgMail.send({\n      to: msg.to,\n      from: msg.from,\n      subject: msg.subject,\n      text: msg.text,\n      html: msg.html,\n    });\n    console.log('SendGrid के माध्यम से ईमेल भेजा गया');\n  } catch (error) {\n    console.error('SendGrid त्रुटि:', error);\n  }\n}\n\n// उपयोग\nsendViaSendGrid({\n  to: 'client@example.com',\n  from: 'noreply@myapp.com',\n  subject: 'पंजीकरण की पुष्टि',\n  text: 'स्वागत है!',\n  html: '<h1>स्वागत है!</h1><p>पंजीकरण के लिए धन्यवाद</p>',\n});
\n\n

3. AWS SES — उच्च-लोड सिस्टम के लिए

\n\n

AWS Simple Email Service (SES) बड़ी मात्रा में ईमेल भेजने के लिए एक एंटरप्राइज़ समाधान है। इसका उपयोग SDK @aws-sdk/client-ses के माध्यम से किया जाता है।

\n\n

स्थापना

\n
npm install @aws-sdk/client-ses
\n\n

TypeScript के साथ उदाहरण

\n
import { SESClient, SendEmailCommand } from '@aws-sdk/client-ses';\n\nconst sesClient = new SESClient({\n  region: 'us-east-1',\n  credentials: {\n    accessKeyId: process.env.AWS_ACCESS_KEY!,\n    secretAccessKey: process.env.AWS_SECRET_KEY!,\n  },\n});\n\ninterface SESEmailParams {\n  to: string[];\n  from: string;\n  subject: string;\n  bodyText: string;\n  bodyHtml?: string;\n}\n\nasync function sendViaSES(params: SESEmailParams): Promise<void> {\n  const command = new SendEmailCommand({\n    Destination: {\n      ToAddresses: params.to,\n    },\n                    

अनुशंसाएँ