إرسال البريد الإلكتروني باستخدام TypeScript: نظرة عامة على المكتبات والأدوات
يُعد إرسال رسائل البريد الإلكتروني إحدى المهام الأساسية لتطبيقات الويب. في TypeScript (وبيئة تشغيلها Node.js) توجد عدة حلول ناضجة. في هذه المقالة، سنستعرض أشهر المكتبات، وإعداداتها، وأمثلة استخدامها مع التصنيف الكامل (Typing).
1. Nodemailer — عميل SMTP شامل
Nodemailer هي المكتبة الأكثر شيوعًا لإرسال البريد الإلكتروني في Node.js. تدعم SMTP، والناقلات المدمجة (Sendmail، SES)، ومتوافقة تمامًا مع TypeScript بفضل الأنواع المضمنة.
التثبيت
npm install nodemailernpm install --save-dev @types/nodemailerمثال للإرسال عبر Gmail SMTP
import nodemailer from 'nodemailer';
interface EmailOptions { to: string; subject: string; text: string; html?: string;}
async function sendEmail(options: EmailOptions): Promise<void> { const transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: process.env.EMAIL_USER, pass: process.env.EMAIL_PASS, }, });
await transporter.sendMail({ from: `"MyApp" <${process.env.EMAIL_USER}>`, to: options.to, subject: options.subject, text: options.text, html: options.html || options.text, });}
// الاستخدامsendEmail({ to: 'user@example.com', subject: 'مرحبًا من TypeScript!', text: 'هذه رسالة اختبارية.',}).catch(console.error);العمل مع المرفقات
await transporter.sendMail({ to: 'user@example.com', subject: 'ملف مرفق', text: 'انظر المستند المرفق.', attachments: [ { filename: 'report.pdf', path: './files/report.pdf', }, ],});2. SendGrid — خدمة بريد إلكتروني سحابية
SendGrid (الآن جزء من Twilio) توفر واجهة برمجة تطبيقات قوية لإرسال رسائل المعاملات والتسويق. يوجد حزمة رسمية لـ TypeScript مع أنواع جاهزة.
التثبيت
npm install @sendgrid/mailمثال للإرسال مع التصنيف
import sgMail from '@sendgrid/mail';
sgMail.setApiKey(process.env.SENDGRID_API_KEY!);
interface SendGridMessage { to: string; from: string; subject: string; text: string; html?: string;}
async function sendViaSendGrid(msg: SendGridMessage): Promise<void> { try { await sgMail.send({ to: msg.to, from: msg.from, subject: msg.subject, text: msg.text, html: msg.html, }); console.log('تم إرسال البريد الإلكتروني عبر SendGrid'); } catch (error) { console.error('خطأ SendGrid:', error); }}
// الاستخدامsendViaSendGrid({ to: 'client@example.com', from: 'noreply@myapp.com', subject: 'تأكيد التسجيل', text: 'مرحبًا بك!', html: '<h1>مرحبًا بك!</h1><p>شكرًا لتسجيلك</p>',});3. AWS SES — للأنظمة عالية التحميل
AWS Simple Email Service (SES) هو حل مؤسسي لإرسال كميات كبيرة من الرسائل. يُستخدم عبر SDK @aws-sdk/client-ses.
التثبيت
npm install @aws-sdk/client-sesمثال مع TypeScript
import { SESClient, SendEmailCommand } from '@aws-sdk/client-ses';
const sesClient = new SESClient({ region: 'us-east-1', credentials: { accessKeyId: process.env.AWS_ACCESS_KEY!, secretAccessKey: process.env.AWS_SECRET_KEY!, },});
interface SESEmailParams { to: string[]; from: string; subject: string; bodyText: string; bodyHtml?: string;}
async function sendViaSES(params: SESEmailParams): Promise<void> { const command = new SendEmailCommand({ Destination: { ToAddresses: params.to, },