TypeScript पर ईमेल भेजना: लाइब्रेरी और उपकरणों का अवलोकन
\n\nइलेक्ट्रॉनिक पत्र भेजना वेब एप्लिकेशन के प्रमुख कार्यों में से एक है। TypeScript (और इसके रनटाइम Node.js) पर कई परिपक्व समाधान मौजूद हैं। इस लेख में, हम सबसे लोकप्रिय लाइब्रेरी, उनकी सेटिंग और पूर्ण टाइपिंग के साथ उपयोग के उदाहरणों पर चर्चा करेंगे।
\n\n1. Nodemailer — सार्वभौमिक SMTP-क्लाइंट
\n\nNodemailer Node.js में ईमेल भेजने के लिए सबसे लोकप्रिय लाइब्रेरी है। यह SMTP, अंतर्निहित ट्रांसपोर्ट (Sendmail, SES) का समर्थन करता है और अंतर्निहित प्रकारों के कारण TypeScript के साथ पूरी तरह से संगत है।
\n\nस्थापना
\nnpm install nodemailer\nnpm install --save-dev @types/nodemailer\n\nGmail SMTP के माध्यम से भेजने का उदाहरण
\nimport 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अटैचमेंट के साथ काम करना
\nawait 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\n2. SendGrid — क्लाउड ईमेल सेवा
\n\nSendGrid (अब Twilio का हिस्सा) लेन-देन और मार्केटिंग ईमेल भेजने के लिए एक शक्तिशाली API प्रदान करता है। TypeScript के लिए तैयार प्रकारों के साथ एक आधिकारिक पैकेज है।
\n\nस्थापना
\nnpm install @sendgrid/mail\n\nटाइपिंग के साथ भेजने का उदाहरण
\nimport 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\n3. AWS SES — उच्च-लोड सिस्टम के लिए
\n\nAWS Simple Email Service (SES) बड़ी मात्रा में ईमेल भेजने के लिए एक एंटरप्राइज़ समाधान है। इसका उपयोग SDK @aws-sdk/client-ses के माध्यम से किया जाता है।
स्थापना
\nnpm install @aws-sdk/client-ses\n\nTypeScript के साथ उदाहरण
\nimport { 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