Skip to content

Add transactional email service for auth and certificates (Nodemailer + templates)#234

Merged
portableDD merged 3 commits intoNexacore-Org:mainfrom
Jayrodri088:feat-email-notfication-service
Mar 29, 2026
Merged

Add transactional email service for auth and certificates (Nodemailer + templates)#234
portableDD merged 3 commits intoNexacore-Org:mainfrom
Jayrodri088:feat-email-notfication-service

Conversation

@Jayrodri088
Copy link
Copy Markdown
Contributor

@Jayrodri088 Jayrodri088 commented Mar 27, 2026

Summary

This PR implements a real backend email notification layer for ByteChain Academy and replaces the previous forgot-password stub behavior.

What was broken

The auth forgot-password flow generated a reset token but did not send an email, making password recovery non-functional in real deployments.

What this PR adds

  • Introduces a dedicated EmailModule and EmailService powered by Nodemailer.
  • Adds reusable HTML template builders for key transactional emails.
  • Wires welcome email delivery into user registration.
  • Wires password reset email delivery into forgot-password.
  • Wires certificate email delivery into automatic course certificate issuance.
  • Documents SMTP configuration in .env.example.
  • Implements graceful degradation: when SMTP_HOST is missing, email content is logged instead of throwing, so app behavior remains stable in local/dev/test environments.

Scope of Changes

New files

  • backend/src/email/email.module.ts
  • backend/src/email/email.service.ts
  • backend/src/email/templates/welcome.template.ts
  • backend/src/email/templates/password-reset.template.ts
  • backend/src/email/templates/certificate.template.ts
  • backend/src/email/templates/streak-reminder.template.ts

Updated files

  • backend/src/auth/auth.service.ts
  • backend/src/auth/auth.module.ts
  • backend/src/certificates/certificates.service.ts
  • backend/src/certificates/certificates.module.ts
  • backend/src/app.module.ts
  • backend/.env.example
  • backend/package.json
  • backend/package-lock.json

Implementation Details

Email service design

  • EmailService reads SMTP config from ConfigService:
    • SMTP_HOST
    • SMTP_PORT
    • SMTP_USER
    • SMTP_PASS
    • SMTP_FROM_NAME
    • SMTP_FROM_EMAIL
  • If SMTP_HOST is present, service creates a Nodemailer transport and sends real emails.
  • If SMTP_HOST is absent, service logs an email payload (to, subject, html) via Nest Logger.

Supported email methods

  • sendWelcomeEmail(to, username)
  • sendPasswordResetEmail(to, resetToken, resetUrl)
  • sendCertificateEmail(to, username, courseName, certificateHash, downloadUrl)
  • sendStreakReminderEmail(to, username, streak)

Template behavior

Each template returns { subject, html } and uses:

  • Inline CSS for email-client compatibility
  • ByteChain Academy branding
  • Green-themed palette
  • Mobile-safe table layout

Auth flow updates

  • register() now sends a welcome email after successful user creation.
  • forgotPassword() now:
    • Generates reset token
    • Builds a reset URL using CLIENT_URL fallback (http://localhost:3000)
    • Sends password reset email
    • No longer returns token in API response

Certificate flow updates

  • issueCertificateForCourse() now sends certificate email after issuance, including:
    • learner name
    • course title
    • certificate hash
    • certificate URL (CLIENT_URL fallback-based)

Config

Added to .env.example:

  • SMTP_HOST=
  • SMTP_PORT=587
  • SMTP_USER=
  • SMTP_PASS=
  • SMTP_FROM_NAME=ByteChain Academy
  • [email protected]

Backward Compatibility / Safety

  • No hard failure when SMTP is unset.
  • Existing app flows continue working in dev/test with logging fallback.
  • Keeps behavior production-ready once SMTP env is configured.

Validation / Test Plan

  • Register a new user and verify welcome email is sent (or logged when SMTP not set)
  • Trigger forgot-password and verify reset email includes reset link/token (or logged)
  • Complete a course and verify certificate email includes certificate hash/link (or logged)
  • Confirm app startup succeeds with no SMTP_HOST configured
  • Confirm app startup and mail delivery succeed with valid SMTP credentials configured
  • Run backend build and lint/test checks as part of CI

Closes: #207

@drips-wave
Copy link
Copy Markdown

drips-wave bot commented Mar 27, 2026

@Jayrodri088 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@portableDD
Copy link
Copy Markdown
Contributor

@Jayrodri088 please resolve conflicts

@Jayrodri088 Jayrodri088 force-pushed the feat-email-notfication-service branch from 4163132 to dd0e5ee Compare March 29, 2026 08:37
@Jayrodri088 Jayrodri088 reopened this Mar 29, 2026
@Jayrodri088
Copy link
Copy Markdown
Contributor Author

@portableDD Conflicts resolved

@portableDD portableDD merged commit 0a8aaf5 into Nexacore-Org:main Mar 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Email Notification Service — Nodemailer, HTML Templates, and Transactional Email Hooks

2 participants