paint-brush
Kifurushi hiki cha NestJS Hubadilisha Usanidi wa Barua Pepe kutoka Ndoto ya Ndoto hadi No-Brainerkwa@axotion
233 usomaji

Kifurushi hiki cha NestJS Hubadilisha Usanidi wa Barua Pepe kutoka Ndoto ya Ndoto hadi No-Brainer

kwa Kamil Fronczak5m2025/01/21
Read on Terminal Reader

Ndefu sana; Kusoma

Kifurushi cha Nestjs-mailer ni zana ya kisasa ya kuunda violezo vinavyobadilika vya barua pepe na kutuma barua pepe kwa urahisi. Inaongeza nguvu za React na Nodemailer, na kuifanya kuwa zana inayomfaa msanidi programu.
featured image - Kifurushi hiki cha NestJS Hubadilisha Usanidi wa Barua Pepe kutoka Ndoto ya Ndoto hadi No-Brainer
Kamil Fronczak HackerNoon profile picture

Kutuma barua pepe ni utendakazi muhimu katika programu nyingi, iwe kwa arifa za watumiaji, masasisho ya miamala, au madhumuni ya uuzaji.


Walakini, kutekeleza masuluhisho ya barua pepe wakati mwingine kunaweza kuwa ngumu, kwani lazima ujumuishe mtumaji barua pepe na lugha ya kiolezo, angalia utegemezi...


Lakini!


Ukiwa na kifurushi cha @nestixis/nestjs-mailer , unaweza kurahisisha mchakato huu huku ukihakikisha kubadilika na kutegemewa.


Kifurushi hiki hutumia nguvu ya React na Nodemailer, na kuifanya kuwa zana ya kisasa na ya kirafiki ya msanidi programu kwa ajili ya kujenga violezo vinavyobadilika vya barua pepe na kutuma barua pepe kwa urahisi.


Wacha tuchunguze jinsi unavyoweza kuiweka na kuitumia :)

Kufunga Kifurushi

Ili kuanza, unahitaji kusakinisha kifurushi cha nestjs-mailer katika programu yako ya NestJS. Kifurushi hiki kinapatikana kupitia npm, na kufanya usakinishaji haraka na moja kwa moja. Endesha amri ifuatayo kwenye terminal yako:

 npm install @nestixis/nestjs-mailer

Kusanidi Moduli

Mara tu kifurushi kitakaposakinishwa, hatua inayofuata ni kusanidi MailerSdkModule katika programu yako.


Usanidi ni moja kwa moja, na kwa madhumuni ya majaribio, unaweza kutumia zana kama Barua pepe ili kunasa na kuhakiki barua pepe bila kuzituma kwa watumiaji halisi.


Hapa kuna mfano wa jinsi ya kuiweka:

 import { MailerSdkModule } from '@nestixis/nestjs-mailer'; import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; @Module({ imports: [ MailerSdkModule.register({ auth: { user: 'username', password: 'password', host: 'sandbox-smtp.mailcatch.app', port: 2525, ssl: false, }, from: '[email protected]', }), ], controllers: [AppController], providers: [AppService], }) export class AppModule {}

Kuunda Kiolezo cha Barua Pepe

Ili kufanya barua pepe zako zionekane na kuvutia zaidi, unaweza kuchanganya violezo na React, na kifurushi @react-email/components , hukuruhusu kubuni violezo kama hivyo vya barua pepe.


**Lakini kabla ya hapo, unapaswa kuita faili invite-admin-with-account-template.tsx na uweke
**

"jsx": "react"

katika tsconfig.json yako


Huu hapa ni mfano wa kiolezo cha kualika mtumiaji mpya wa msimamizi:

 import { Body, Container, Head, Html, Img, Link, Section, Text, } from '@react-email/components'; import * as React from 'react'; export default function InviteAdminWithAccountTemplate({ translation, language, invitationHref, passwordHref, logoUrl, }) { return ( <Html lang={language}> <Head> <style>{/* Your custom styles here */}</style> </Head> <Body style={{ fontFamily: 'Arial, sans-serif' }}> <Section> <Container> {logoUrl ? ( <Img src={logoUrl} alt="Logo" /> ) : ( <Text>{translation.titleInside}</Text> )} <Text>{translation.contentPart1}</Text> <Text>{translation.contentPart2}</Text> <Text> {translation.contentPart3.subpart1} <Link href={invitationHref}> {translation.contentPart3.subpart2} </Link> {translation.contentPart3.subpart3} </Text> <Text> {translation.contentPart4.subpart1} <Link href={passwordHref}> {translation.contentPart4.subpart2} </Link> </Text> </Container> </Section> </Body> </Html> ); }

Kuingiza Mtumaji Barua pepe

Baada ya kuunda kiolezo chako cha barua pepe, hatua inayofuata ni kutuma barua pepe. Ili kufanya hivyo, unaingiza mtumaji barua pepe kwenye huduma yako.

 import { EmailSenderInterface, MAILER_SDK_CLIENT, } from '@nestixis/nestjs-mailer'; import { Inject, Injectable } from '@nestjs/common'; import InviteAdminWithAccountTemplate from './invite-admin-with-account-template'; @Injectable() export class AppService { constructor( @Inject(MAILER_SDK_CLIENT) private readonly emailSender: EmailSenderInterface, ) {} async send(): Promise<void> { const translations = { titleInside: { subpart1: 'Welcome', subpart2: ' to the platform!' }, contentPart1: 'Hello', contentPart2: 'Your admin account has been created.', contentPart3: { subpart1: 'Click here to activate your account: ', subpart2: 'Activate', subpart3: '.', }, contentPart4: { subpart1: 'To set your password, click here: ', subpart2: 'Set password', }, }; const emailContent = await InviteAdminWithAccountTemplate({ translation: translations, language: 'en', invitationHref: 'xxx', passwordHref: 'xxx', logoUrl: 'logo.png', }); await this.emailSender.sendEmail( '[email protected]', 'Admin Invitation', emailContent, ); } }

Imekamilika!

Maelezo ya picha

Ni hayo tu! Umeunganisha kwa ufanisi nestjs-mailer kwenye programu yako.


Kwa maelezo zaidi na vipengele vya kina, angalia Hazina ya GitHub ya mtumaji wa NestJS .