Send email via Nodemailer in NestJs with html template that is uploaded in Mailgun

704 Views Asked by At

I have this very simple template uploaded to my HTML templates at mailgun

<!DOCTYPE html>
<html>
<head>
    <title>Your Email</title>
</head>
<body>
    Header
    {{content}}
    Footer
</body>
</html>

this is my template in mailgun

enter image description here

I want to use this template when sending emails via nestjs like this:

import { MailerService } from "@nestjs-modules/mailer";

    constructor(
        private readonly mailerService: MailerService,
    ) {
    }
    async sendEmail(email: string, subject: string, name: string, message: string, to: string) {
        const mail = await this.mailerService.sendMail({
            from: `${name} <${email}>`,
            to: to,
            subject: subject,
            template: // what should i add here?,
            context: // how can i pass the message HTML string to my {{content}} in the html template?
        })
            .then(response => ({ success: true }))
            .catch(error => {
                return ({ success: false, error: error });
            });
        return mail;
    }

So, how can i use my HTML template and pass to the variables HTML content?

I DONT WANT TO USE HANDLEBARS

I tried this but it is not working

import { MailerService } from "@nestjs-modules/mailer";

    constructor(
        private readonly mailerService: MailerService,
    ) {
    }
    async sendEmail(email: string, subject: string, name: string, message: string, to: string) {
        const mail = await this.mailerService.sendMail({
            from: `${name} <${email}>`,
            to: to,
            subject: subject,
            template: 'simplerespond',
            context: { content: '<p>test</p><p>test</p>' }
        })
            .then(response => ({ success: true }))
            .catch(error => {
                return ({ success: false, error: error });
            });
        return mail;
    }
0

There are 0 best solutions below